2021-10-03 09:03:56 +02:00
|
|
|
import ExtendedRequest from "./extended-request";
|
2021-11-21 18:19:58 +01:00
|
|
|
import { getshorthost, makeThrottle } from "./util";
|
2021-10-03 09:03:56 +02:00
|
|
|
import { EventEmitter } from "events";
|
2021-10-03 16:33:23 +02:00
|
|
|
import { RequestCluster } from "./request-cluster";
|
2021-10-03 09:03:56 +02:00
|
|
|
|
2021-11-07 13:57:24 +01:00
|
|
|
export default class Memory extends EventEmitter {
|
2021-11-06 21:48:25 +01:00
|
|
|
origin_to_history = {} as Record<string, Record<string, RequestCluster>>;
|
2021-11-21 18:19:58 +01:00
|
|
|
private throttle = makeThrottle(200);
|
2021-10-03 09:03:56 +02:00
|
|
|
async register(request: ExtendedRequest) {
|
2021-10-04 18:51:51 +02:00
|
|
|
await request.init();
|
2021-11-21 22:38:32 +01:00
|
|
|
// console.log("registering request for", request.origin);
|
2021-11-06 21:48:25 +01:00
|
|
|
if (!request.isThirdParty()) {
|
|
|
|
return;
|
2021-10-03 09:03:56 +02:00
|
|
|
}
|
2021-11-07 19:03:00 +01:00
|
|
|
if (!this.origin_to_history[request.origin]) {
|
|
|
|
this.origin_to_history[request.origin] = {};
|
2021-11-06 21:48:25 +01:00
|
|
|
}
|
|
|
|
const shorthost = getshorthost(new URL(request.url).host);
|
2021-11-07 19:03:00 +01:00
|
|
|
if (!this.origin_to_history[request.origin][shorthost]) {
|
2021-11-06 21:48:25 +01:00
|
|
|
const cluster = new RequestCluster(shorthost);
|
2021-11-07 19:03:00 +01:00
|
|
|
this.origin_to_history[request.origin][shorthost] = cluster;
|
2021-11-06 21:48:25 +01:00
|
|
|
}
|
2021-11-07 19:03:00 +01:00
|
|
|
this.origin_to_history[request.origin][shorthost].add(request);
|
2021-11-06 21:48:25 +01:00
|
|
|
this.emit("change");
|
2021-10-03 09:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2021-11-26 20:58:31 +01:00
|
|
|
|
|
|
|
browser.webRequest.onBeforeRequest.addListener(
|
|
|
|
async (request) => {
|
|
|
|
new ExtendedRequest(request);
|
|
|
|
},
|
|
|
|
{ urls: ["<all_urls>"] },
|
|
|
|
["requestBody"]
|
|
|
|
);
|
2021-10-03 09:03:56 +02:00
|
|
|
browser.webRequest.onBeforeSendHeaders.addListener(
|
|
|
|
async (request) => {
|
2021-11-26 20:58:31 +01:00
|
|
|
const extendedRequest = ExtendedRequest.by_id[
|
|
|
|
request.requestId
|
|
|
|
].addHeaders(request.requestHeaders || []);
|
|
|
|
this.register(extendedRequest);
|
2021-10-03 09:03:56 +02:00
|
|
|
},
|
|
|
|
{ urls: ["<all_urls>"] },
|
|
|
|
["requestHeaders"]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-21 18:19:58 +01:00
|
|
|
emit(eventName: string, immediate = false) {
|
|
|
|
try {
|
|
|
|
if (immediate) {
|
|
|
|
super.emit(eventName);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
this.throttle(() => super.emit(eventName));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
debugger;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 21:48:25 +01:00
|
|
|
getClustersForOrigin(origin: string): Record<string, RequestCluster> {
|
|
|
|
return this.origin_to_history[origin] || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeCookiesFor(origin: string, shorthost?: string): Promise<void> {
|
2021-11-07 09:17:19 +01:00
|
|
|
if (shorthost) {
|
|
|
|
const cookies = await browser.cookies.getAll({ domain: shorthost });
|
|
|
|
for (const cookie of cookies) {
|
|
|
|
console.log("removing cookie", cookie.name, "from", cookie.domain);
|
|
|
|
await browser.cookies.remove({
|
|
|
|
name: cookie.name,
|
|
|
|
url: `https://${cookie.domain}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const clusters = this.getClustersForOrigin(origin);
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
Object.values(clusters)
|
|
|
|
.filter((cluster) => !shorthost || cluster.id === shorthost)
|
|
|
|
.map((cluster) => this.removeCookiesFor(origin, cluster.id))
|
|
|
|
);
|
|
|
|
}
|
2021-11-06 21:48:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async removeRequestsFor(origin: string) {
|
|
|
|
this.origin_to_history[origin] = {};
|
2021-10-03 09:03:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 13:57:24 +01:00
|
|
|
export function init() {
|
|
|
|
const memory = new Memory();
|
2021-10-03 09:03:56 +02:00
|
|
|
|
2021-11-07 13:57:24 +01:00
|
|
|
(window as any).memory = memory;
|
|
|
|
}
|
2021-11-07 17:23:48 +01:00
|
|
|
|
|
|
|
export function getMemory(): Memory {
|
|
|
|
return (browser.extension.getBackgroundPage().window as any).memory as Memory;
|
|
|
|
}
|