2022-01-27 22:30:57 +01:00
|
|
|
import ExtendedRequest from './extended-request';
|
|
|
|
import { getshorthost, makeThrottle } from './util';
|
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import { RequestCluster } from './request-cluster';
|
2021-10-03 09:03:56 +02:00
|
|
|
|
2022-04-13 11:44:59 +02:00
|
|
|
function setDomainsNumber(counter: number, tabId: number) {
|
2022-04-22 13:00:02 +02:00
|
|
|
browser.browserAction.setBadgeText({ text: counter < 0 ? '0' : counter.toString(), tabId });
|
2022-04-13 11:44:59 +02:00
|
|
|
browser.browserAction.setTitle({
|
2022-04-15 10:34:29 +02:00
|
|
|
title: 'Rentgen',
|
2022-04-13 11:44:59 +02:00
|
|
|
tabId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-11-07 13:57:24 +01:00
|
|
|
export default class Memory extends EventEmitter {
|
2022-01-27 22:30:57 +01:00
|
|
|
origin_to_history = {} as Record<string, Record<string, RequestCluster>>;
|
2022-02-07 15:28:01 +01:00
|
|
|
private throttle = makeThrottle(100);
|
2022-01-27 22:30:57 +01:00
|
|
|
async register(request: ExtendedRequest) {
|
|
|
|
await request.init();
|
|
|
|
if (!request.isThirdParty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.origin_to_history[request.origin]) {
|
|
|
|
this.origin_to_history[request.origin] = {};
|
|
|
|
}
|
|
|
|
const shorthost = getshorthost(new URL(request.url).host);
|
|
|
|
if (!this.origin_to_history[request.origin][shorthost]) {
|
|
|
|
const cluster = new RequestCluster(shorthost);
|
|
|
|
this.origin_to_history[request.origin][shorthost] = cluster;
|
|
|
|
}
|
|
|
|
this.origin_to_history[request.origin][shorthost].add(request);
|
2022-02-07 15:28:01 +01:00
|
|
|
this.emit('change', false, shorthost, 'registered request(shorthost emit)');
|
2022-04-13 11:44:59 +02:00
|
|
|
|
2022-04-15 10:34:29 +02:00
|
|
|
Object.values(this.getClustersForOrigin(request.origin)).some((cluster) =>
|
|
|
|
cluster.hasCookies()
|
|
|
|
)
|
|
|
|
? browser.browserAction.setBadgeBackgroundColor({ color: '#ff726b' })
|
|
|
|
: browser.browserAction.setBadgeBackgroundColor({ color: '#ffb900' });
|
|
|
|
|
2022-04-13 11:44:59 +02:00
|
|
|
setDomainsNumber(
|
|
|
|
Object.values(this.getClustersForOrigin(request.origin)).length,
|
|
|
|
request.tabId
|
|
|
|
);
|
2021-10-03 09:03:56 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 22:30:57 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
2021-11-26 20:58:31 +01:00
|
|
|
|
2022-01-27 22:30:57 +01:00
|
|
|
browser.webRequest.onBeforeRequest.addListener(
|
|
|
|
async (request) => {
|
|
|
|
new ExtendedRequest(request);
|
|
|
|
},
|
|
|
|
{ urls: ['<all_urls>'] },
|
|
|
|
['requestBody']
|
|
|
|
);
|
|
|
|
browser.webRequest.onBeforeSendHeaders.addListener(
|
|
|
|
async (request) => {
|
2022-02-07 15:28:01 +01:00
|
|
|
const extendedRequest = ExtendedRequest.by_id[request.requestId].addHeaders(
|
|
|
|
request.requestHeaders || []
|
|
|
|
);
|
2022-01-27 22:30:57 +01:00
|
|
|
this.register(extendedRequest);
|
|
|
|
},
|
|
|
|
{ urls: ['<all_urls>'] },
|
|
|
|
['requestHeaders']
|
|
|
|
);
|
|
|
|
}
|
2021-10-03 09:03:56 +02:00
|
|
|
|
2022-04-22 13:00:02 +02:00
|
|
|
private originalEmit(type: string, ...args: unknown[]) {
|
|
|
|
var doError = type === 'error';
|
|
|
|
|
|
|
|
var events = (this as any)._events;
|
|
|
|
if (events !== undefined) doError = doError && events.error === undefined;
|
|
|
|
else if (!doError) return false;
|
|
|
|
|
|
|
|
// If there is no 'error' event listener then throw.
|
|
|
|
if (doError) {
|
|
|
|
var er;
|
|
|
|
if (args.length > 0) er = args[0];
|
|
|
|
if (er instanceof Error) {
|
|
|
|
// Note: The comments on the `throw` lines are intentional, they show
|
|
|
|
// up in Node's output if this results in an unhandled exception.
|
|
|
|
throw er; // Unhandled 'error' event
|
2022-01-27 22:30:57 +01:00
|
|
|
}
|
2022-04-22 13:00:02 +02:00
|
|
|
// At least give some kind of context to the user
|
|
|
|
var err = new Error('Unhandled error.' + (er ? ' (' + (er as any).message + ')' : ''));
|
|
|
|
(err as any).context = er;
|
|
|
|
throw err; // Unhandled 'error' event
|
2022-01-27 22:30:57 +01:00
|
|
|
}
|
2022-04-22 13:00:02 +02:00
|
|
|
|
|
|
|
var handler = events[type];
|
|
|
|
|
|
|
|
if (handler === undefined) return false;
|
|
|
|
|
|
|
|
if (typeof handler === 'function') {
|
|
|
|
try {
|
|
|
|
Reflect.apply(handler, this, args);
|
|
|
|
} catch (error) {
|
|
|
|
events[type] = undefined;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// var len = handler.length;
|
|
|
|
var listeners = [...handler];
|
|
|
|
|
|
|
|
listeners
|
|
|
|
.filter((e) => {
|
|
|
|
try {
|
|
|
|
e.call;
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.forEach((listener) => {
|
|
|
|
try {
|
|
|
|
Reflect.apply(listener, this, args);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
debugger;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit(eventName: string, immediate = false, data = 'any', reason: string): boolean {
|
|
|
|
setTimeout(() => this.originalEmit(eventName, data), 0);
|
|
|
|
return;
|
2021-11-21 18:19:58 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 22:30:57 +01:00
|
|
|
getClustersForOrigin(origin: string): Record<string, RequestCluster> {
|
|
|
|
return this.origin_to_history[origin] || {};
|
|
|
|
}
|
2021-11-06 21:48:25 +01:00
|
|
|
|
2022-01-27 22:30:57 +01:00
|
|
|
async removeCookiesFor(origin: string, shorthost?: string): Promise<void> {
|
|
|
|
if (shorthost) {
|
|
|
|
const cookies = await browser.cookies.getAll({ domain: shorthost });
|
|
|
|
for (const cookie of cookies) {
|
2022-02-07 15:28:01 +01:00
|
|
|
console.log('removing cookie', cookie.name, 'from', cookie.domain);
|
2022-01-27 22:30:57 +01:00
|
|
|
await browser.cookies.remove({
|
|
|
|
name: cookie.name,
|
|
|
|
url: `https://${cookie.domain}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const clusters = this.getClustersForOrigin(origin);
|
2021-11-07 09:17:19 +01:00
|
|
|
|
2022-01-27 22:30:57 +01:00
|
|
|
await Promise.all(
|
|
|
|
Object.values(clusters)
|
|
|
|
.filter((cluster) => !shorthost || cluster.id === shorthost)
|
|
|
|
.map((cluster) => this.removeCookiesFor(origin, cluster.id))
|
|
|
|
);
|
|
|
|
}
|
2021-11-07 09:17:19 +01:00
|
|
|
}
|
2021-11-06 21:48:25 +01:00
|
|
|
|
2022-01-27 22:30:57 +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() {
|
2022-01-27 22:30:57 +01:00
|
|
|
const memory = new Memory();
|
2021-10-03 09:03:56 +02:00
|
|
|
|
2022-01-27 22:30:57 +01:00
|
|
|
(window as any).memory = memory;
|
2021-11-07 13:57:24 +01:00
|
|
|
}
|
2021-11-07 17:23:48 +01:00
|
|
|
|
|
|
|
export function getMemory(): Memory {
|
2022-02-07 15:28:01 +01:00
|
|
|
return (browser.extension.getBackgroundPage().window as any).memory as Memory;
|
2021-11-07 17:23:48 +01:00
|
|
|
}
|