diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2024-11-04 23:17:18 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2024-11-04 23:17:18 +0800 |
commit | c4c8903b148ce75dd2470203e9faf93e3a42b15b (patch) | |
tree | ba8684d5edaf0b897da85b1a70e1fededdefbaca /addon/index.js | |
parent | 9d9fb5652086dad99f9b0f4b32b57b49568a5ead (diff) | |
parent | 71810f684724245f17b5cee72be030f4a2467cca (diff) |
Merge branch 'addon'
Diffstat (limited to 'addon/index.js')
-rw-r--r-- | addon/index.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/addon/index.js b/addon/index.js new file mode 100644 index 0000000..2ce3423 --- /dev/null +++ b/addon/index.js | |||
@@ -0,0 +1,55 @@ | |||
1 | /* global browser */ | ||
2 | /* eslint-disable-next-line no-console */ | ||
3 | console.log('content script loaded') | ||
4 | |||
5 | const url = new URL(window.location) | ||
6 | const use = url.searchParams.get('use') | ||
7 | if (url.host === 'www.ptt.cc') { | ||
8 | const content = document.querySelector('#main-content') | ||
9 | Array.from(content.childNodes) | ||
10 | .filter(n => !(n instanceof window.HTMLElement)) | ||
11 | .forEach(text => { | ||
12 | const span = document.createElement('span') | ||
13 | span.innerText = text.textContent | ||
14 | text.replaceWith(span) | ||
15 | }) | ||
16 | } | ||
17 | |||
18 | const contentSelectors = { | ||
19 | 'developer.mozilla': ':has(.section-content)', | ||
20 | 'hackmd.io': '#doc', | ||
21 | 'www.ptt.cc': '#main-content', | ||
22 | 'prosemirror.net': '.ProseMirror', | ||
23 | } | ||
24 | const contentSelector = contentSelectors[url.host] | ||
25 | |||
26 | const simpleRender = globalThis.renderWith(config => ({ | ||
27 | use: use ?? 'Leaflet', | ||
28 | width: '100%', | ||
29 | height: '200px', | ||
30 | XYZ: 'https://tile.openstreetmap.jp/styles/osm-bright/512/{z}/{x}/{y}.png', | ||
31 | ...config, | ||
32 | aliases: { | ||
33 | use: globalThis.mapclayRenderer, | ||
34 | ...(config.aliases ?? {}), | ||
35 | }, | ||
36 | })) | ||
37 | |||
38 | const container = document.querySelector(contentSelector ?? 'main') ?? document.body | ||
39 | |||
40 | browser.runtime.onMessage.addListener((message, sender, sendResponse) => { | ||
41 | /* eslint-disable-next-line no-console */ | ||
42 | console.log('receive message', message) | ||
43 | sendResponse('received') | ||
44 | if (message.id === 'map-inline-add') { | ||
45 | globalThis.generateMaps(container, { | ||
46 | crs: url.searchParams.get('crs') ?? 'EPSG:4326', | ||
47 | render: simpleRender, | ||
48 | }) | ||
49 | return Promise.resolve('done') | ||
50 | } else if (message.id === 'map-inline-menu') { | ||
51 | container.dataset.menu = message.checked ? 'enabled' : 'disabled' | ||
52 | return Promise.resolve('done') | ||
53 | } | ||
54 | return false | ||
55 | }) | ||