aboutsummaryrefslogtreecommitdiffhomepage
path: root/addon/index.js
blob: fcc0cc768bf9334945fe5c4f4f4bb833efef65c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* global browser */
/* eslint-disable-next-line no-console */
console.log('content script loaded')

const url = new URL(window.location)
const use = url.searchParams.get('use')
if (url.host === 'www.ptt.cc') {
  const content = document.querySelector('#main-content')
  Array.from(content.childNodes)
    .filter(n => !(n instanceof window.HTMLElement))
    .forEach(text => {
      const span = document.createElement('span')
      span.innerText = text.textContent
      text.replaceWith(span)
    })
}

const contentSelectors = {
  'developer.mozilla': ':has(.section-content)',
  'hackmd.io': '#doc',
  'www.ptt.cc': '#main-content',
  'prosemirror.net': '.ProseMirror',
  'www.openstreetmap.org': '.content-body',
  'markdown-it.github.io': '.result-html',
}
const contentSelector = contentSelectors[url.host]

const simpleRender = globalThis.renderWith(config => ({
  use: use ?? 'Leaflet',
  width: '100%',
  height: '200px',
  XYZ: 'https://tile.openstreetmap.jp/styles/osm-bright/512/{z}/{x}/{y}.png',
  ...config,
  aliases: {
    use: globalThis.mapclayRenderers,
    ...(config.aliases ?? {}),
  },
}))

const container = document.querySelector(contentSelector ?? 'main') ?? document.body

browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
  /* eslint-disable-next-line no-console */
  console.log('receive message', message)
  sendResponse('received')
  if (message.id === 'map-inline-add') {
    globalThis.generateMaps(container, {
      crs: url.searchParams.get('crs') ?? 'EPSG:4326',
      render: simpleRender,
    })
    return Promise.resolve('done')
  } else if (message.id === 'map-inline-menu') {
    container.dataset.menu = message.checked ? 'enabled' : 'disabled'
    return Promise.resolve('done')
  }
  return false
})