Skip to content

Commit 479e55e

Browse files
committed
add back webextension polyfill
1 parent c909c70 commit 479e55e

File tree

7 files changed

+29
-15
lines changed

7 files changed

+29
-15
lines changed

bun.lock

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"dependencies": {
77
"leaflet": "^1.9.4",
88
"leaflet-fullscreen": "^1.0.2",
9+
"webextension-polyfill": "^0.12.0",
910
},
1011
"devDependencies": {
1112
"npm-run-all": "^4.1.5",
@@ -267,6 +268,8 @@
267268

268269
"validate-npm-package-license": ["[email protected]", "", { "dependencies": { "spdx-correct": "^3.0.0", "spdx-expression-parse": "^3.0.0" } }, "sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew=="],
269270

271+
"webextension-polyfill": ["[email protected]", "", {}, "sha512-97TBmpoWJEE+3nFBQ4VocyCdLKfw54rFaJ6EVQYLBCXqCIpLSZkwGgASpv4oPt9gdKCJ80RJlcmNzNn008Ag6Q=="],
272+
270273
"which": ["[email protected]", "", { "dependencies": { "isexe": "^2.0.0" }, "bin": { "which": "./bin/which" } }, "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ=="],
271274

272275
"which-boxed-primitive": ["[email protected]", "", { "dependencies": { "is-bigint": "^1.1.0", "is-boolean-object": "^1.2.1", "is-number-object": "^1.1.1", "is-string": "^1.1.1", "is-symbol": "^1.1.1" } }, "sha512-TbX3mj8n0odCBFVlY8AxkqcHASw3L60jIuF8jFP78az3C2YhmGvqbHBpAjTRH2/xqYunrJ9g1jSyjCjpoWzIAA=="],

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
},
2929
"dependencies": {
3030
"leaflet": "^1.9.4",
31-
"leaflet-fullscreen": "^1.0.2"
31+
"leaflet-fullscreen": "^1.0.2",
32+
"webextension-polyfill": "^0.12.0"
3233
},
3334
"release-it": {
3435
"git": {

src/bg/action.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { tabs, browserAction, /*webNavigation*/ } from 'webextension-polyfill'
2+
13
import { getHostname, invertHostState } from './utils/storage.js'
24
//import { matcher as mapsUrlMatcher, runtimeMapUrl } from './bg';
35

@@ -19,15 +21,15 @@ async function actionClick(tab) {
1921

2022
/*
2123
// TODO: try to only reload necessary parts!!!
22-
let frames = (await browser.webNavigation.getAllFrames({ tabId: tab.id })) ?? [];
24+
let frames = (await webNavigation.getAllFrames({ tabId: tab.id })) ?? [];
2325
2426
if (
2527
frames.some((frame) => {
2628
frame.url.match(replacedUrlMatcher) || frame.url.match(mapsUrlMatcher);
2729
})
2830
)
2931
*/
30-
browser.tabs.reload(tab.id, { bypassCache: true })
32+
tabs.reload(tab.id, { bypassCache: true })
3133
}
3234

33-
browser.browserAction.onClicked.addListener(actionClick)
35+
browserAction.onClicked.addListener(actionClick)

src/bg/bg.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { runtime, webRequest, tabs, windows } from 'webextension-polyfill'
2+
13
import { disabledHosts, getHostname } from './utils/storage.js'
24
import { updateActiveTabIcon } from './utils/actionIcon.js'
35
import domainEnds from './utils/domainEnds.json' with { type: 'json' }
@@ -7,7 +9,7 @@ export const matcher = new RegExp(
79
// TODO: fix regex to fit more patterns
810
`^(https?:\/\/)?(maps\.google\.(${gLocales})\/maps.*\?.*output=embed|(www\.)?google\.(${gLocales})\/maps\/embed.*\?)`
911
)
10-
export const runtimeMapUrl = browser.runtime.getURL('map.html')
12+
export const runtimeMapUrl = runtime.getURL('map.html')
1113

1214
/**
1315
* Checks if `frames` send a request to Maps.
@@ -29,7 +31,7 @@ function redirect(req) {
2931
}
3032

3133
// Listens to web requests from frames, redirects when fitting `matcher`
32-
browser.webRequest.onBeforeRequest.addListener(
34+
webRequest.onBeforeRequest.addListener(
3335
redirect,
3436
{
3537
urls: ['<all_urls>'],
@@ -39,13 +41,13 @@ browser.webRequest.onBeforeRequest.addListener(
3941
)
4042

4143
// listen to tab URL changes
42-
browser.tabs.onUpdated.addListener(updateActiveTabIcon)
44+
tabs.onUpdated.addListener(updateActiveTabIcon)
4345

4446
// listen to tab switching
45-
browser.tabs.onActivated.addListener(updateActiveTabIcon)
47+
tabs.onActivated.addListener(updateActiveTabIcon)
4648

4749
// listen for window switching
48-
browser.windows.onFocusChanged.addListener(updateActiveTabIcon)
50+
windows.onFocusChanged.addListener(updateActiveTabIcon)
4951

5052
// update icon at startup
5153
updateActiveTabIcon()

src/bg/utils/actionIcon.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { browserAction, tabs } from 'webextension-polyfill'
2+
13
import { disabledHosts, getHostname } from './storage.js'
24

35
/**
@@ -7,7 +9,7 @@ import { disabledHosts, getHostname } from './storage.js'
79
export function updateIcon(hostname) {
810
let disabled = disabledHosts.includes(hostname)
911

10-
browser.browserAction.setIcon({
12+
browserAction.setIcon({
1113
path: !disabled ? {
1214
48: '/icons/48-icon.png',
1315
96: '/icons/96-icon.png',
@@ -22,7 +24,7 @@ export function updateIcon(hostname) {
2224
* Async function to update the icon of the currently active tab. Uses `updateIcon` internally
2325
*/
2426
export async function updateActiveTabIcon() {
25-
let browserTabs = await browser.tabs.query({ active: true, currentWindow: true })
27+
let browserTabs = await tabs.query({ active: true, currentWindow: true })
2628

2729
let tab = browserTabs[0]
2830
if (tab && tab.url) {

src/bg/utils/storage.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import { storage } from 'webextension-polyfill'
2+
13
import { updateIcon } from './actionIcon.js'
24

35
export const KEY_DISABLED_HOSTS = 'disabled_hosts'
46

57
// Listens to changes on the storage. Updates disabled hosts list, if stored list changes
68
/** @type {string[]} */
79
export let disabledHosts = await getDisabledHosts()
8-
browser.storage.local.onChanged.addListener((changes) => {
10+
storage.local.onChanged.addListener((changes) => {
911
if (KEY_DISABLED_HOSTS in changes) {
1012
disabledHosts = changes[KEY_DISABLED_HOSTS].newValue ?? []
1113
}
@@ -16,7 +18,7 @@ browser.storage.local.onChanged.addListener((changes) => {
1618
* @returns {Promise<string[]>} List of disabled hostnames
1719
*/
1820
async function getDisabledHosts() {
19-
return (await browser.storage.local.get(KEY_DISABLED_HOSTS))[KEY_DISABLED_HOSTS] ?? []
21+
return (await storage.local.get(KEY_DISABLED_HOSTS))[KEY_DISABLED_HOSTS] ?? []
2022
}
2123

2224
/**
@@ -31,7 +33,7 @@ export async function invertHostState(hostname) {
3133
disabledHosts.push(hostname)
3234
}
3335

34-
await browser.storage.local.set({
36+
await storage.local.set({
3537
[KEY_DISABLED_HOSTS]: disabledHosts,
3638
})
3739

src/options/options.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { storage } from 'webextension-polyfill'
2+
13
import {
24
KEY_DISABLED_HOSTS,
35
disabledHosts,
@@ -78,7 +80,7 @@ function getIndex(button) {
7880
return index
7981
}
8082

81-
browser.storage.local.onChanged.addListener((changes) => {
83+
storage.local.onChanged.addListener((changes) => {
8284
if (KEY_DISABLED_HOSTS in changes) {
8385
buildEntries()
8486
}

0 commit comments

Comments
 (0)