Skip to content

Commit

Permalink
feat: disable replace for page (atm no indicator change or settings p…
Browse files Browse the repository at this point in the history
…age)
  • Loading branch information
nobkd committed Apr 9, 2023
1 parent 7db1f29 commit 8b2469f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/bg/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { invertDisabled } from './utils/storage';
browserAction.onClicked.addListener(async (tab: Tabs.Tab) => {
if (!tab.url) return;
await invertDisabled(tab.url);
tabs.reload(tab.id);
tabs.reload(tab.id, { bypassCache: true });
});
27 changes: 17 additions & 10 deletions src/bg/bg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { runtime, webRequest, type WebRequest } from 'webextension-polyfill';
//import { getDisabled } from './utils/storage';
import { storage, runtime, webRequest, type WebRequest } from 'webextension-polyfill';
import { getAllDisabled, getHostname, KEY_DISABLED } from './utils/storage';

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

/*async*/ function redirect(
req: WebRequest.OnBeforeRequestDetailsType
): /*Promise<*/ WebRequest.BlockingResponse /*>*/ {
let disabledUrls: string[] = await getAllDisabled();
storage.local.onChanged.addListener((changes) => {
if (KEY_DISABLED in changes) {
console.log(changes)
disabledUrls = changes[KEY_DISABLED].newValue;
}
});

function redirect(req: WebRequest.OnBeforeRequestDetailsType): WebRequest.BlockingResponse {
if (req.originUrl && req.url.match(matcher)) {
//if (!await getDisabled(req.originUrl)) {
return {
redirectUrl: runtime.getURL('map.html?' + req.url.split('?')[1]),
};
//}
console.log(req.originUrl);
if (!disabledUrls.includes(getHostname(req.originUrl))) {
return {
redirectUrl: runtime.getURL('map.html?' + req.url.split('?')[1]),
};
}
}
return {};
}
Expand Down
27 changes: 16 additions & 11 deletions src/bg/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@ export type HostnameStatus = {
[key: string]: boolean;
};

export async function getDisabled(url: string): Promise<boolean> {
const hostname = getHostname(url);
let res = await storage.local.get(hostname);
return res[hostname] ?? false;
export const KEY_DISABLED = 'disabled_urls';

export async function getAllDisabled(): Promise<string[]> {
return (await storage.local.get(KEY_DISABLED))[KEY_DISABLED] ?? [];
}

export async function invertDisabled(url: string) {
export async function invertDisabled(url: string): Promise<void> {
url = getHostname(url);
const disabledUrls = await getAllDisabled();

if (disabledUrls.includes(url)) {
disabledUrls.splice(disabledUrls.indexOf(url), 1);
} else {
disabledUrls.push(url);
}

await storage.local.set({
[getHostname(url)]: !(await getDisabled(url)),
[KEY_DISABLED]: disabledUrls,
});
}

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

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

0 comments on commit 8b2469f

Please sign in to comment.