Skip to content

Commit fdb89fd

Browse files
authored
Download the markdown from the content script (#65)
* Download the markdown from the content script There are complexities that arise from passing the content to the background script, especially when moving to Manifest V3. Avoid this by just triggering the download directly from the content script. * Simplify the message handler
1 parent 99c8808 commit fdb89fd

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

src/background_scripts/background.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,8 @@ import {
55
tabs as _tabs,
66
} from "webextension-polyfill";
77

8-
function saveAs(text) {
9-
const blob = new Blob([text], { type: "text/markdown;charset=utf-8" });
10-
11-
return downloads.download({
12-
url: URL.createObjectURL(blob),
13-
filename: "README.md",
14-
saveAs: true,
15-
});
16-
}
17-
188
runtime.onMessage.addListener((data, sender) => {
199
switch (data.action) {
20-
case "saveAs":
21-
saveAs(data.text).catch((err) => {
22-
console.error("Failed to save content", err);
23-
});
24-
break;
25-
2610
case "showPageAction":
2711
pageAction.show(sender.tab.id);
2812
break;

src/content_scripts/to-markdown.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ function expandHrefs(article) {
7979
}
8080
}
8181

82+
function downloadBlob(blob) {
83+
const downloadLink = document.createElement('a');
84+
downloadLink.href = URL.createObjectURL(blob);
85+
downloadLink.download = 'README.md';
86+
document.body.appendChild(downloadLink);
87+
downloadLink.click();
88+
document.body.removeChild(downloadLink);
89+
URL.revokeObjectURL(downloadLink.href);
90+
}
91+
8292
function capturePage() {
8393
const newDoc = document.createDocumentFragment();
8494
const titleElement = document.createElement("h1");
@@ -115,17 +125,13 @@ function capturePage() {
115125
newDoc.appendChild(article);
116126
}
117127

118-
return generateMarkdown(newDoc);
128+
const text = generateMarkdown(newDoc);
129+
const blob = new Blob([text], { type: "text/markdown;charset=utf-8" });
130+
downloadBlob(blob);
119131
}
120132

121133
runtime.onMessage.addListener((data) => {
122134
if (data.action === "capturePage") {
123-
const markdown = capturePage();
124-
return runtime
125-
.sendMessage({
126-
action: "saveAs",
127-
text: markdown,
128-
})
129-
.catch((err) => console.error("Failed to send 'saveAs' message", err));
135+
capturePage();
130136
}
131137
});

webpack.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ module.exports = {
1313
path: path.resolve(__dirname, "addon"),
1414
filename: "[name]/index.js",
1515
},
16+
optimization: {
17+
minimize: false,
18+
},
1619
};

0 commit comments

Comments
 (0)