*Track the provider used for a domain via a map

*Add a context menu to clear the cache if the provider is ipfs
This commit is contained in:
Derrick Hammer 2022-08-15 09:51:43 -04:00
parent dfed76acc5
commit a105c9471d
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 46 additions and 17 deletions

View File

@ -8,7 +8,7 @@
"48": "icon.png",
"96": "icon@2x.png"
},
"permissions": ["proxy", "webRequest", "webRequestBlocking", "webNavigation", "<all_urls>"],
"permissions": ["proxy", "webRequest", "webRequestBlocking", "webNavigation","menus", "<all_urls>"],
"background": {
"scripts": ["background.js"]
},

31
src/contextMenu.ts Normal file
View File

@ -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,
});
});
}

View File

@ -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();
}

View File

@ -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,
};

View File

@ -22,6 +22,7 @@ export default class WebEngine {
private requests: Map<string, BaseProvider> = new Map();
private requestData: Map<string, {}> = new Map();
private navigations: Map<string, Promise<any>> = new Map();
private domainContentProvider: Map<string, BaseProvider> = 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: ["<all_urls>"] },
["blocking"]
);
public getDomainContentProvider(domain: string): BaseProvider | null {
return this.domainContentProvider.get(domain) ?? null;
}
}