Skip to content

Commit

Permalink
chore: only include needed imports from polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
nobkd committed Apr 1, 2023
1 parent c4062d7 commit 9c0dcad
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
27 changes: 16 additions & 11 deletions src/bg/action.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import browser from 'webextension-polyfill';
import { browserAction, tabs, type Tabs } from 'webextension-polyfill';
import { getDisabled, setDisabled } from './utils/storage';

browser.browserAction.onClicked.addListener(async (tab: browser.Tabs.Tab) => {
console.log(tab);
browserAction.onClicked.addListener(async (tab: Tabs.Tab) => {
const hostname = getHostname(tab.url);
if (!hostname) return;

/*
let disabled: any = await browser.storage.managed.get('disabled');
disabled = !(typeof disabled === 'boolean' ? disabled : false);
console.log(hostname);

console.log(disabled);
// TODO

browser.storage.managed.set({ disabled: disabled });
*/

browser.tabs.reload(tab.id);
tabs.reload(tab.id);
});

function getHostname(url?: string): string | undefined {
if (!url) return;

url = url.replace(/^\w+:\/\//, '');
url = url.split(/[\/#]/, 1)[0];
return url;
}
10 changes: 4 additions & 6 deletions src/bg/bg.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import browser from 'webextension-polyfill';
import { runtime, webRequest, type WebRequest } from 'webextension-polyfill';

const patterns: string[] = [
'http://*/maps/embed?*',
Expand All @@ -13,18 +13,16 @@ const matcher: RegExp = new RegExp(
`^(https?:\/\/)?(maps\.google\.(${gLocales})\/maps\?.*output=embed|(www\.)?google\.(${gLocales})\/maps\/embed\?)`
);

function redirect(
req: browser.WebRequest.OnBeforeRequestDetailsType
): browser.WebRequest.BlockingResponse {
function redirect(req: WebRequest.OnBeforeRequestDetailsType): WebRequest.BlockingResponse {
if (req.url.match(matcher)) {
return {
redirectUrl: browser.runtime.getURL('map.html?' + req.url.split('?')[1]),
redirectUrl: runtime.getURL('map.html?' + req.url.split('?')[1]),
};
}
return {};
}

browser.webRequest.onBeforeRequest.addListener(
webRequest.onBeforeRequest.addListener(
redirect,
{
urls: patterns,
Expand Down
21 changes: 21 additions & 0 deletions src/bg/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { storage } from 'webextension-polyfill';

export type HostnameStatus = {
[key: string]: boolean;
};

export async function getDisabled(hostname: string): Promise<boolean> {
await storage.local.get(hostname);

// what is the result???

return false;
}

export async function setDisabled(hostname: string, status: boolean) {
//
}

export async function getAll(): Promise<HostnameStatus> {
return await storage.local.get();
}

0 comments on commit 9c0dcad

Please sign in to comment.