|
1 | | -const browser = require("webextension-polyfill"); |
2 | | -const gfm = require('turndown-plugin-gfm').gfm; |
3 | | -const TurndownService = require('turndown').default; |
4 | | - |
5 | | -function stripHyphens (str) { |
6 | | - const regex = /^--- (.+) ---$/; |
7 | | - return str.replace(regex, (match, p1) => { |
8 | | - return p1; |
9 | | - }); |
| 1 | +import { runtime } from "webextension-polyfill"; |
| 2 | +import { gfm } from "turndown-plugin-gfm"; |
| 3 | +import TurndownService from "turndown"; |
| 4 | + |
| 5 | +function stripHyphens(str) { |
| 6 | + const regex = /^--- (.+) ---$/; |
| 7 | + return str.replace(regex, (match, p1) => { |
| 8 | + return p1; |
| 9 | + }); |
10 | 10 | } |
11 | 11 |
|
12 | | -const newDoc = document.createDocumentFragment(); |
13 | | -const titleElement = document.createElement('h1'); |
14 | | -newDoc.appendChild(titleElement); |
| 12 | +function capturePage() { |
| 13 | + const newDoc = document.createDocumentFragment(); |
| 14 | + const titleElement = document.createElement("h1"); |
| 15 | + newDoc.appendChild(titleElement); |
15 | 16 |
|
16 | | -const link = document.createElement('a'); |
17 | | -const href = window.location.origin + window.location.pathname |
18 | | -link.href = href; |
19 | | -link.innerText = href; |
20 | | -newDoc.appendChild(link); |
| 17 | + const link = document.createElement("a"); |
| 18 | + const href = window.location.origin + window.location.pathname; |
| 19 | + link.href = href; |
| 20 | + link.innerText = href; |
| 21 | + newDoc.appendChild(link); |
21 | 22 |
|
22 | | -const descriptionHeader = document.createElement('h2'); |
23 | | -descriptionHeader.innerText = 'Description'; |
24 | | -newDoc.appendChild(descriptionHeader); |
| 23 | + const descriptionHeader = document.createElement("h2"); |
| 24 | + descriptionHeader.innerText = "Description"; |
| 25 | + newDoc.appendChild(descriptionHeader); |
25 | 26 |
|
26 | | -const articleElements = document.querySelectorAll('article'); |
27 | | -const articleElementsLength = articleElements.length; |
28 | | -for (let index = 0; index < articleElementsLength; ++index) { |
| 27 | + const articleElements = document.querySelectorAll("article"); |
| 28 | + const articleElementsLength = articleElements.length; |
| 29 | + for (let index = 0; index < articleElementsLength; ++index) { |
29 | 30 | const article = articleElements[index].cloneNode(true); |
30 | | - const headingElement = article.querySelector('h2'); |
| 31 | + const headingElement = article.querySelector("h2"); |
31 | 32 | let heading = stripHyphens(headingElement.innerText); |
32 | 33 |
|
33 | 34 | if (index === 0) { |
34 | | - titleElement.innerText = heading; |
35 | | - heading = 'Part One'; |
| 35 | + titleElement.innerText = heading; |
| 36 | + heading = "Part One"; |
36 | 37 | } |
37 | 38 |
|
38 | | - const newHeadingElement = document.createElement('h3'); |
| 39 | + const newHeadingElement = document.createElement("h3"); |
39 | 40 | newHeadingElement.innerText = heading; |
40 | 41 | headingElement.replaceWith(newHeadingElement); |
41 | 42 |
|
42 | 43 | newDoc.appendChild(article); |
43 | | -} |
| 44 | + } |
44 | 45 |
|
45 | | -const turndownService = new TurndownService({ |
46 | | - headingStyle: 'atx' |
47 | | -}); |
48 | | -turndownService.use(gfm); |
49 | | -turndownService.keep(['span']); |
| 46 | + const turndownService = new TurndownService({ |
| 47 | + headingStyle: "atx", |
| 48 | + }); |
| 49 | + turndownService.use(gfm); |
| 50 | + turndownService.keep(["span"]); |
50 | 51 |
|
51 | | -const markdown = turndownService.turndown(newDoc).concat('\n'); |
| 52 | + return turndownService.turndown(newDoc).concat("\n"); |
| 53 | +} |
52 | 54 |
|
53 | | -browser.runtime.sendMessage({ |
54 | | - action: "saveAs", |
55 | | - text: markdown, |
| 55 | +runtime.onMessage.addListener((data) => { |
| 56 | + if (data.action === "capturePage") { |
| 57 | + const markdown = capturePage(); |
| 58 | + return runtime |
| 59 | + .sendMessage({ |
| 60 | + action: "saveAs", |
| 61 | + text: markdown, |
| 62 | + }) |
| 63 | + .catch((err) => console.error("Failed to send 'saveAs' message", err)); |
| 64 | + } |
56 | 65 | }); |
0 commit comments