*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:
parent
dfed76acc5
commit
a105c9471d
|
@ -8,7 +8,7 @@
|
||||||
"48": "icon.png",
|
"48": "icon.png",
|
||||||
"96": "icon@2x.png"
|
"96": "icon@2x.png"
|
||||||
},
|
},
|
||||||
"permissions": ["proxy", "webRequest", "webRequestBlocking", "webNavigation", "<all_urls>"],
|
"permissions": ["proxy", "webRequest", "webRequestBlocking", "webNavigation","menus", "<all_urls>"],
|
||||||
"background": {
|
"background": {
|
||||||
"scripts": ["background.js"]
|
"scripts": ["background.js"]
|
||||||
},
|
},
|
||||||
|
|
|
@ -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,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
|
@ -35,6 +35,7 @@ import {
|
||||||
setTimer,
|
setTimer,
|
||||||
} from "./vars.js";
|
} from "./vars.js";
|
||||||
import browser from "@lumeweb/webextension-polyfill";
|
import browser from "@lumeweb/webextension-polyfill";
|
||||||
|
import setupContextMenus from "../contextMenu.js";
|
||||||
|
|
||||||
function logLargeObjects() {
|
function logLargeObjects() {
|
||||||
let queriesLen = Object.keys(getQueries()).length;
|
let queriesLen = Object.keys(getQueries()).length;
|
||||||
|
@ -211,6 +212,7 @@ async function boot() {
|
||||||
getKernelIframe().onload = init;
|
getKernelIframe().onload = init;
|
||||||
document.body.appendChild(getKernelIframe());
|
document.body.appendChild(getKernelIframe());
|
||||||
|
|
||||||
|
setupContextMenus(engine);
|
||||||
await dnsReady();
|
await dnsReady();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 OnHeadersReceivedDetailsType = WebRequest.OnHeadersReceivedDetailsType;
|
||||||
import OnBeforeRequestDetailsType = WebRequest.OnBeforeRequestDetailsType;
|
import OnBeforeRequestDetailsType = WebRequest.OnBeforeRequestDetailsType;
|
||||||
import OnBeforeSendHeadersDetailsType = WebRequest.OnBeforeSendHeadersDetailsType;
|
import OnBeforeSendHeadersDetailsType = WebRequest.OnBeforeSendHeadersDetailsType;
|
||||||
|
@ -24,4 +30,5 @@ export {
|
||||||
HttpHeadersItemType,
|
HttpHeadersItemType,
|
||||||
Tabs,
|
Tabs,
|
||||||
OnBeforeNavigateDetailsType,
|
OnBeforeNavigateDetailsType,
|
||||||
|
Menus,
|
||||||
};
|
};
|
||||||
|
|
|
@ -22,6 +22,7 @@ export default class WebEngine {
|
||||||
private requests: Map<string, BaseProvider> = new Map();
|
private requests: Map<string, BaseProvider> = new Map();
|
||||||
private requestData: Map<string, {}> = new Map();
|
private requestData: Map<string, {}> = new Map();
|
||||||
private navigations: Map<string, Promise<any>> = new Map();
|
private navigations: Map<string, Promise<any>> = new Map();
|
||||||
|
private domainContentProvider: Map<string, BaseProvider> = new Map();
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
browser.webRequest.onHeadersReceived.addListener(
|
browser.webRequest.onHeadersReceived.addListener(
|
||||||
|
@ -83,6 +84,7 @@ export default class WebEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.requests.set(details.requestId, handle);
|
this.requests.set(details.requestId, handle);
|
||||||
|
this.domainContentProvider.set(new URL(details.url).hostname, handle);
|
||||||
|
|
||||||
return this.processHandler(details, "handleProxy");
|
return this.processHandler(details, "handleProxy");
|
||||||
}
|
}
|
||||||
|
@ -285,20 +287,7 @@ export default class WebEngine {
|
||||||
)}`;
|
)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static cancelRequest(tabId: number) {
|
public getDomainContentProvider(domain: string): BaseProvider | null {
|
||||||
const handler = (details: OnBeforeRequestDetailsType): BlockingResponse => {
|
return this.domainContentProvider.get(domain) ?? null;
|
||||||
if (details.tabId !== tabId) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
browser.webRequest.onBeforeRequest.removeListener(handler);
|
|
||||||
|
|
||||||
return { cancel: true };
|
|
||||||
};
|
|
||||||
|
|
||||||
browser.webRequest.onBeforeRequest.addListener(
|
|
||||||
handler,
|
|
||||||
{ urls: ["<all_urls>"] },
|
|
||||||
["blocking"]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue