Skip to content

Commit 0e4a1bd

Browse files
committed
Small fixes and updates to use browser-polyfill
1 parent 80e0a22 commit 0e4a1bd

File tree

8 files changed

+66
-102
lines changed

8 files changed

+66
-102
lines changed

background/background.js

+18-44
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
if (chrome) {
2-
chrome.runtime.onMessage.addListener(notify);
3-
}else {
4-
browser.runtime.onMessage.addListener(notify);
5-
}
1+
browser.runtime.onMessage.addListener(notify);
62

73
function createReadableVersion(dom) {
84
var reader = new Readability(dom);
@@ -14,7 +10,7 @@ function convertArticleToMarkdown(article) {
1410
var turndownService = new TurndownService()
1511
var markdown = turndownService.turndown(article.content);
1612

17-
//add article titel as header
13+
//add article title as header
1814
markdown = "# " + article.title + "\n" + markdown;
1915

2016
//add summary if exist
@@ -36,40 +32,23 @@ function downloadMarkdown(markdown, title) {
3632
type: "text/markdown;charset=utf-8"
3733
});
3834
var url = URL.createObjectURL(blob);
39-
if (chrome) {
40-
chrome.downloads.download({
41-
url: url,
42-
filename: generateValidFileName(title) + ".md",
43-
saveAs: true
44-
}, function(id) {
45-
chrome.downloads.onChanged.addListener((delta ) => {
46-
//release the url for the blob
47-
if (delta.state && delta.state.current == "complete") {
48-
if (delta.id === id) {
49-
window.URL.revokeObjectURL(url);
50-
}
51-
}
52-
});
53-
});
54-
}else {
55-
browser.downloads.download({
56-
url: url,
57-
filename: generateValidFileName(title) + ".md",
58-
incognito: true,
59-
saveAs: true
60-
}).then((id) => {
61-
browser.downloads.onChanged.addListener((delta ) => {
62-
//release the url for the blob
63-
if (delta.state && delta.state.current == "complete") {
64-
if (delta.id === id) {
65-
window.URL.revokeObjectURL(url);
66-
}
35+
36+
browser.downloads.download({
37+
url: url,
38+
filename: generateValidFileName(title) + ".md",
39+
saveAs: true
40+
}).then((id) => {
41+
browser.downloads.onChanged.addListener((delta ) => {
42+
//release the url for the blob
43+
if (delta.state && delta.state.current == "complete") {
44+
if (delta.id === id) {
45+
window.URL.revokeObjectURL(url);
6746
}
68-
});
69-
}).catch((err) => {
70-
console.error("Download failed" + err)
47+
}
7148
});
72-
}
49+
}).catch((err) => {
50+
console.error("Download failed" + err)
51+
});
7352
}
7453

7554

@@ -84,12 +63,7 @@ function notify(message) {
8463

8564
var article = createReadableVersion(dom);
8665
var markdown = convertArticleToMarkdown(article);
87-
//downloadMarkdown(markdown, article);
88-
if (chrome) {
89-
chrome.runtime.sendMessage({type: "display.md", markdown: markdown, article: article});
90-
} else {
91-
browser.runtime.sendMessage({ type: "display.md", markdown: markdown, article: article });
92-
}
66+
browser.runtime.sendMessage({ type: "display.md", markdown: markdown, article: article });
9367
}
9468
else if (message.type == "download") {
9569
downloadMarkdown(message.markdown, message.title);

browser-polyfill.min.js

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contentScript/pageScraper.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function notifyExtension() {
2+
//var serializer = new XMLSerializer();
3+
//var content = serializer.serializeToString(document);
4+
var content = document.documentElement.outerHTML;
5+
browser.runtime.sendMessage({ type: "clip", "dom": content});
6+
}
7+
notifyExtension();

contentScript/pageScrapper.js

-11
This file was deleted.

manifest.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22

33
"manifest_version": 2,
4-
"name": "markdown-clipper",
4+
"name": "MarkDownload - Markdown Web Clipper",
55
"version": "2.0",
66
"author": "Enrico Kaack/Gordon Pedsersen",
77

@@ -31,9 +31,7 @@
3131
},
3232

3333
"background": {
34-
"scripts": ["background/turndown.js", "/background/Readability.js", "background/background.js"]
34+
"scripts": ["browser-polyfill.min.js", "background/turndown.js", "/background/Readability.js", "background/background.js"]
3535
}
36-
37-
3836

3937
}

popup/clipSite.html

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
<meta charset="utf-8">
77
<style>
88
*{margin:0;padding:0;}
9+
body {
10+
width:350px;
11+
height:500px;
12+
border-radius: 0.5em;
13+
overflow: hidden;
14+
}
915
@keyframes spinner {
1016
to {transform: rotate(360deg);}
11-
}
12-
17+
}
1318
#spinner:before {
1419
content: '';
1520
box-sizing: border-box;
@@ -25,11 +30,6 @@
2530
border-right: 2px solid transparent;
2631
animation: spinner .6s linear infinite;
2732
}
28-
body {
29-
width:350px;
30-
height:500px;
31-
border-radius: 0.5em;
32-
}
3333
textarea#md {
3434
height:calc(100% - 3em);
3535
width:calc(100% - 1em);
@@ -40,6 +40,7 @@
4040
width: 100%;
4141
height: 100%;
4242
display:none;
43+
flex-direction: column;
4344
}
4445
a#download {
4546
display:block;
@@ -61,7 +62,8 @@
6162
<a href="#" id="download">Download</a>
6263
</div>
6364
<div id="spinner"></div>
64-
<script src="clipSite.js"></script>
65+
<script type="application/javascript" src="../browser-polyfill.min.js"></script>
66+
<script type="application/javascript" src="clipSite.js"></script>
6567
</body>
6668

6769
</html>

popup/clipSite.js

+21-35
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
1-
if (chrome) {
2-
chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
3-
var id = tabs[0].id;
4-
var url = tabs[0].url;
5-
chrome.tabs.executeScript(id, {
6-
file: "/contentScript/pageScrapper.js"
7-
}, function() {
8-
console.log("Successfully injected");
9-
});
10-
11-
});
12-
} else {
13-
browser.tabs.query({currentWindow: true, active: true})
14-
.then((tabs) => {
15-
var id = tabs[0].id;
16-
var url = tabs[0].url;
17-
browser.tabs.executeScript(id, {
18-
file: "/contentScript/pageScrapper.js"
19-
}).then( () => {
20-
console.log("Successfully injected");
21-
}).catch( (error) => {
22-
console.error(error);
1+
2+
browser.tabs.query({currentWindow: true, active: true})
3+
.then((tabs) => {
4+
var id = tabs[0].id;
5+
var url = tabs[0].url;
6+
browser.tabs.executeScript(id, {
7+
file: "/browser-polyfill.min.js"
8+
}).then(() => {
9+
return browser.tabs.executeScript(id, {
10+
file: "/contentScript/pageScraper.js"
2311
});
24-
});
25-
}
12+
}).then( () => {
13+
console.log("Successfully injected");
14+
}).catch( (error) => {
15+
console.error(error);
16+
});
17+
});
2618

27-
if (chrome) {
28-
chrome.runtime.onMessage.addListener(notify);
29-
} else {
30-
browser.runtime.onMessage.addListener(notify);
31-
}
19+
20+
browser.runtime.onMessage.addListener(notify);
3221

3322
function download(e) {
3423
e.preventDefault();
@@ -37,11 +26,8 @@ function download(e) {
3726
markdown: document.getElementById("md").value,
3827
title: document.getElementById("md").title
3928
};
40-
if (chrome) {
41-
chrome.runtime.sendMessage(message);
42-
} else {
43-
browser.runtime.sendMessage(message);
44-
}
29+
30+
browser.runtime.sendMessage(message);
4531
}
4632

4733
//function that handles messages from the injected script into the site
@@ -50,7 +36,7 @@ function notify(message) {
5036
document.getElementById("md").value = message.markdown;
5137
document.getElementById("md").title = message.article.title;
5238
document.getElementById("download").addEventListener("click", download);
53-
document.getElementById("container").style.display = 'block';
39+
document.getElementById("container").style.display = 'flex';
5440
document.getElementById("spinner").style.display = 'none';
5541
}
5642
}
-63.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)