Skip to content

Commit

Permalink
feat: WIP implement turning the ext off/on on page
Browse files Browse the repository at this point in the history
  • Loading branch information
nobkd committed Apr 1, 2023
1 parent 9c0dcad commit 14dad79
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
19 changes: 3 additions & 16 deletions src/bg/action.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
import { browserAction, tabs, type Tabs } from 'webextension-polyfill';
import { getDisabled, setDisabled } from './utils/storage';
import { invertDisabled } from './utils/storage';

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

console.log(hostname);

// TODO

if (!tab.url) return;
await invertDisabled(tab.url);
tabs.reload(tab.id);
});

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

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

const patterns: string[] = [
'http://*/maps/embed?*',
Expand All @@ -14,10 +15,12 @@ const matcher: RegExp = new RegExp(
);

function redirect(req: WebRequest.OnBeforeRequestDetailsType): WebRequest.BlockingResponse {
if (req.url.match(matcher)) {
if (req.originUrl && req.url.match(matcher)) {
//if (!getDisabled(req.originUrl)) { // TODO: resolve promise async, but response of fuc has to be sync
return {
redirectUrl: runtime.getURL('map.html?' + req.url.split('?')[1]),
};
//}
}
return {};
}
Expand Down
22 changes: 14 additions & 8 deletions src/bg/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ 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 getDisabled(url: string): Promise<boolean> {
const hostname = getHostname(url);
let res = await storage.local.get(hostname);
return res[hostname] ?? false;
}

export async function setDisabled(hostname: string, status: boolean) {
//
export async function invertDisabled(url: string) {
await storage.local.set({
[getHostname(url)]: !(await getDisabled(url)),
});
}

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

function getHostname(url: string): string {
url = url.replace(/^\w+:\/\//, '');
url = url.split(/[\/#]/, 1)[0];
return url;
}

0 comments on commit 14dad79

Please sign in to comment.