Skip to content

Commit

Permalink
Add a base element to the DOM with current URL
Browse files Browse the repository at this point in the history
This makes the DOM parse with a correct base URI instead of the extension's base URI, which fixes #1
  • Loading branch information
gordonped committed May 14, 2020
1 parent 2778288 commit 07327ed
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
17 changes: 11 additions & 6 deletions background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ browser.runtime.onMessage.addListener(notify);

// creates the readable article object from Readability
function createReadableVersion(dom) {
var reader = new Readability(dom);
var reader = new Readability(dom, { debug: true });
var article = reader.parse();
return article;
}

// convert the article content to markdown using Turndown
function convertArticleToMarkdown(article, url) {
function convertArticleToMarkdown(article) {
var turndownService = new TurndownService()
var markdown = turndownService.turndown(article.content);

Expand All @@ -21,9 +21,6 @@ function convertArticleToMarkdown(article, url) {
markdown = "> " + article.excerpt + "\n\n" + markdown;
}

//add url
markdown = url + "\n\n" + markdown;

return markdown;
}

Expand Down Expand Up @@ -63,14 +60,22 @@ function downloadMarkdown(markdown, title) {
function notify(message) {
// message for initial clipping of the dom
if (message.type == "clip") {

// parse the dom
var parser = new DOMParser();
var dom = parser.parseFromString(message.dom, "text/html");
if (dom.documentElement.nodeName == "parsererror") {
console.error("error while parsing");
}

// make markdown document from the dom
var article = createReadableVersion(dom);
var markdown = convertArticleToMarkdown(article, message.url);
var markdown = convertArticleToMarkdown(article);

// add url to the top of the markdown
markdown = dom.baseURI + "\n\n" + markdown;

// send a message to display the markdown
browser.runtime.sendMessage({ type: "display.md", markdown: markdown, article: article });
}
// message for triggering download
Expand Down
16 changes: 13 additions & 3 deletions contentScript/pageScraper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
function notifyExtension() {
//var serializer = new XMLSerializer();
//var content = serializer.serializeToString(document);
// if the document doesn't have a "base" element make one
// this allows the DOM parser in future steps to fix relative uris
if (document.head.getElementsByTagName('base').length == 0) {
let baseEl = document.createElement('base');
// use the current uri
baseEl.setAttribute('href', window.location.href);
document.head.append(baseEl);
}

// get the content of the page as a string
var content = document.documentElement.outerHTML;
browser.runtime.sendMessage({ type: "clip", dom: content, url:window.location.href});

// send a message that the content should be clipped
browser.runtime.sendMessage({ type: "clip", dom: content});
}
notifyExtension();

0 comments on commit 07327ed

Please sign in to comment.