diff --git a/assets/manifest.json b/assets/manifest.json index 7d0c6d3..129f5b0 100644 --- a/assets/manifest.json +++ b/assets/manifest.json @@ -8,7 +8,7 @@ "48": "icon.png", "96": "icon@2x.png" }, - "permissions": ["proxy", "webRequest", "webRequestBlocking", "webNavigation", ""], + "permissions": ["proxy", "webRequest", "webRequestBlocking", "webNavigation","menus", ""], "background": { "scripts": ["background.js"] }, diff --git a/src/contextMenu.ts b/src/contextMenu.ts new file mode 100644 index 0000000..4d5807d --- /dev/null +++ b/src/contextMenu.ts @@ -0,0 +1,31 @@ +import browser from "@lumeweb/webextension-polyfill"; +import type WebEngine from "./webEngine.js"; +import type { Menus, Tabs } from "./types.js"; +import IpfsProvider from "./contentProviders/ipfsProvider.js"; +import { cacheDb } from "./databases.js"; + +export default function setup(engine: WebEngine) { + browser.menus.create({ + title: "Clear Cache", + id: "clear-cache", + onclick: async (info: Menus.OnClickData, tab: Tabs.Tab) => { + // @ts-ignore + await cacheDb.items + .where("url") + .startsWithIgnoreCase( + `http://${new URL(info.pageUrl as string).hostname}` + ) + .delete(); + browser.tabs.reload(tab.id); + }, + }); + + browser.menus.onShown.addListener((details: Menus.OnShownInfoType) => { + const provider = engine.getDomainContentProvider( + new URL(details.pageUrl as string).hostname + ); + browser.menus.update("clear-cache", { + visible: provider instanceof IpfsProvider, + }); + }); +} diff --git a/src/main/background.ts b/src/main/background.ts index b91ed2f..133e423 100644 --- a/src/main/background.ts +++ b/src/main/background.ts @@ -35,6 +35,7 @@ import { setTimer, } from "./vars.js"; import browser from "@lumeweb/webextension-polyfill"; +import setupContextMenus from "../contextMenu.js"; function logLargeObjects() { let queriesLen = Object.keys(getQueries()).length; @@ -211,6 +212,7 @@ async function boot() { getKernelIframe().onload = init; document.body.appendChild(getKernelIframe()); + setupContextMenus(engine); await dnsReady(); } diff --git a/src/types.ts b/src/types.ts index da07e17..91b463c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,10 @@ -import { WebRequest, Proxy, Tabs, WebNavigation } from "webextension-polyfill"; +import { + WebRequest, + Proxy, + Tabs, + WebNavigation, + Menus, +} from "webextension-polyfill"; import OnHeadersReceivedDetailsType = WebRequest.OnHeadersReceivedDetailsType; import OnBeforeRequestDetailsType = WebRequest.OnBeforeRequestDetailsType; import OnBeforeSendHeadersDetailsType = WebRequest.OnBeforeSendHeadersDetailsType; @@ -24,4 +30,5 @@ export { HttpHeadersItemType, Tabs, OnBeforeNavigateDetailsType, + Menus, }; diff --git a/src/webEngine.ts b/src/webEngine.ts index 4f8e169..48c05b4 100644 --- a/src/webEngine.ts +++ b/src/webEngine.ts @@ -22,6 +22,7 @@ export default class WebEngine { private requests: Map = new Map(); private requestData: Map = new Map(); private navigations: Map> = new Map(); + private domainContentProvider: Map = new Map(); constructor() { browser.webRequest.onHeadersReceived.addListener( @@ -83,6 +84,7 @@ export default class WebEngine { } this.requests.set(details.requestId, handle); + this.domainContentProvider.set(new URL(details.url).hostname, handle); return this.processHandler(details, "handleProxy"); } @@ -285,20 +287,7 @@ export default class WebEngine { )}`; } - public static cancelRequest(tabId: number) { - const handler = (details: OnBeforeRequestDetailsType): BlockingResponse => { - if (details.tabId !== tabId) { - return {}; - } - browser.webRequest.onBeforeRequest.removeListener(handler); - - return { cancel: true }; - }; - - browser.webRequest.onBeforeRequest.addListener( - handler, - { urls: [""] }, - ["blocking"] - ); + public getDomainContentProvider(domain: string): BaseProvider | null { + return this.domainContentProvider.get(domain) ?? null; } }