Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 07327ed

Browse files
committedMay 14, 2020
Add a base element to the DOM with current URL
This makes the DOM parse with a correct base URI instead of the extension's base URI, which fixes deathau#1
1 parent 2778288 commit 07327ed

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed
 

‎background/background.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ browser.runtime.onMessage.addListener(notify);
33

44
// creates the readable article object from Readability
55
function createReadableVersion(dom) {
6-
var reader = new Readability(dom);
6+
var reader = new Readability(dom, { debug: true });
77
var article = reader.parse();
88
return article;
99
}
1010

1111
// convert the article content to markdown using Turndown
12-
function convertArticleToMarkdown(article, url) {
12+
function convertArticleToMarkdown(article) {
1313
var turndownService = new TurndownService()
1414
var markdown = turndownService.turndown(article.content);
1515

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

24-
//add url
25-
markdown = url + "\n\n" + markdown;
26-
2724
return markdown;
2825
}
2926

@@ -63,14 +60,22 @@ function downloadMarkdown(markdown, title) {
6360
function notify(message) {
6461
// message for initial clipping of the dom
6562
if (message.type == "clip") {
63+
64+
// parse the dom
6665
var parser = new DOMParser();
6766
var dom = parser.parseFromString(message.dom, "text/html");
6867
if (dom.documentElement.nodeName == "parsererror") {
6968
console.error("error while parsing");
7069
}
7170

71+
// make markdown document from the dom
7272
var article = createReadableVersion(dom);
73-
var markdown = convertArticleToMarkdown(article, message.url);
73+
var markdown = convertArticleToMarkdown(article);
74+
75+
// add url to the top of the markdown
76+
markdown = dom.baseURI + "\n\n" + markdown;
77+
78+
// send a message to display the markdown
7479
browser.runtime.sendMessage({ type: "display.md", markdown: markdown, article: article });
7580
}
7681
// message for triggering download

‎contentScript/pageScraper.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
function notifyExtension() {
2-
//var serializer = new XMLSerializer();
3-
//var content = serializer.serializeToString(document);
2+
// if the document doesn't have a "base" element make one
3+
// this allows the DOM parser in future steps to fix relative uris
4+
if (document.head.getElementsByTagName('base').length == 0) {
5+
let baseEl = document.createElement('base');
6+
// use the current uri
7+
baseEl.setAttribute('href', window.location.href);
8+
document.head.append(baseEl);
9+
}
10+
11+
// get the content of the page as a string
412
var content = document.documentElement.outerHTML;
5-
browser.runtime.sendMessage({ type: "clip", dom: content, url:window.location.href});
13+
14+
// send a message that the content should be clipped
15+
browser.runtime.sendMessage({ type: "clip", dom: content});
616
}
717
notifyExtension();

0 commit comments

Comments
 (0)
Please sign in to comment.