From 66272fa31824019e8915d04846167f9eb08015fb Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Sun, 7 Nov 2021 13:57:24 +0100 Subject: [PATCH] Delegate memory to background process --- .gitignore | 1 + background.ts | 5 +++++ manifest.json | 3 +++ memory.ts | 8 +++++--- package.json | 2 +- report-window/report-window.html | 11 +++++++++++ report-window/report-window.tsx | 15 +++++++++++++++ request-cluster.ts | 6 +++--- sidebar/sidebar.html | 4 ++-- sidebar/sidebar.tsx | 5 ++--- sidebar/stolen-data-cluster.tsx | 6 +++--- sidebar/stolen-data.tsx | 20 +++++++++++++++----- util.ts | 5 +++++ 13 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 background.ts create mode 100644 report-window/report-window.html create mode 100644 report-window/report-window.tsx diff --git a/.gitignore b/.gitignore index 26d5e85..8cf7aa4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules sidebar.js /web-ext-artifacts/ +lib/* diff --git a/background.ts b/background.ts new file mode 100644 index 0000000..9707bd5 --- /dev/null +++ b/background.ts @@ -0,0 +1,5 @@ +import { init } from "./memory"; + +init(); + +alert("memory initialized!"); diff --git a/manifest.json b/manifest.json index 1d346e8..f9086cc 100644 --- a/manifest.json +++ b/manifest.json @@ -10,6 +10,9 @@ "default_panel": "sidebar/sidebar.html", "default_icon": "sidebar_icon.png" }, + "background": { + "scripts": ["lib/background.js"] + }, "icons": { "48": "icons/border-48.png" }, diff --git a/memory.ts b/memory.ts index a05f937..7d92213 100644 --- a/memory.ts +++ b/memory.ts @@ -3,7 +3,7 @@ import { getshorthost } from "./util"; import { EventEmitter } from "events"; import { RequestCluster } from "./request-cluster"; -class Memory extends EventEmitter { +export default class Memory extends EventEmitter { origin_to_history = {} as Record>; async register(request: ExtendedRequest) { await request.init(); @@ -64,6 +64,8 @@ class Memory extends EventEmitter { } } -const memory = new Memory(); +export function init() { + const memory = new Memory(); -export default memory; + (window as any).memory = memory; +} diff --git a/package.json b/package.json index 22f09ce..c79b856 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "", "main": "email-template-harsh.js", "scripts": { - "build": "npx esbuild sidebar.tsx --bundle --outfile=./lib/sidebar.js", + "build": "npx esbuild sidebar/sidebar.tsx report-window/report-window.tsx --bundle background.ts --bundle --outdir=./lib", "watch": "npm run build -- --watch" }, "repository": { diff --git a/report-window/report-window.html b/report-window/report-window.html new file mode 100644 index 0000000..50dd91b --- /dev/null +++ b/report-window/report-window.html @@ -0,0 +1,11 @@ + + + + + Wygeneruj maila co zgłoszenia + + +
+ + + diff --git a/report-window/report-window.tsx b/report-window/report-window.tsx new file mode 100644 index 0000000..69f890d --- /dev/null +++ b/report-window/report-window.tsx @@ -0,0 +1,15 @@ +import React from "react"; +import ReactDOM from "react-dom"; +import { Memory } from "../memory"; + +const memory = (window as any).memory as Memory; + +function Report() { + return ( +
+

Raport

+
+ ); +} + +ReactDOM.render(, document.getElementById("app")); diff --git a/request-cluster.ts b/request-cluster.ts index ace0c68..0849a96 100644 --- a/request-cluster.ts +++ b/request-cluster.ts @@ -4,7 +4,7 @@ import ExtendedRequest from "./extended-request"; export type Sources = "cookie" | "pathname" | "queryparams" | "header"; import { TCString, TCModel } from "@iabtcf/core"; -import { isJSONObject, isURL, parseToObject } from "./util"; +import { getMemory, isJSONObject, isURL, parseToObject } from "./util"; import memory from "./memory"; const id = (function* id() { @@ -87,7 +87,7 @@ export class StolenDataEntry { addMark(key: string) { this.markedKeys.push(key); - memory.emit("change"); // to trigger rerender + getMemory().emit("change"); // to trigger rerender } hasMark(key: string) { @@ -96,7 +96,7 @@ export class StolenDataEntry { removeMark(key: string) { this.markedKeys = this.markedKeys.filter((e) => e != key); - memory.emit("change"); // to trigger rerender + getMemory().emit("change"); // to trigger rerender } toggleMark(key: string) { diff --git a/sidebar/sidebar.html b/sidebar/sidebar.html index c14104e..a54937b 100644 --- a/sidebar/sidebar.html +++ b/sidebar/sidebar.html @@ -39,7 +39,7 @@
- - + + diff --git a/sidebar/sidebar.tsx b/sidebar/sidebar.tsx index 354becd..8c10db4 100644 --- a/sidebar/sidebar.tsx +++ b/sidebar/sidebar.tsx @@ -1,9 +1,8 @@ import React, { useEffect, useState } from "react"; import ReactDOM from "react-dom"; -import memory from "../memory"; import Options from "../options"; import { StolenData } from "./stolen-data"; -import { useEmitter } from "../util"; +import { getMemory, useEmitter } from "../util"; async function getCurrentTab() { const [tab] = await browser.tabs.query({ @@ -17,7 +16,7 @@ const Sidebar = () => { const [origin, setOrigin] = useState(null); const [minValueLength, setMinValueLength] = useState(7); const [cookiesOnly, setCookiesOnly] = useState(false); - const [counter, setCounter] = useEmitter(memory); + const [counter, setCounter] = useEmitter(getMemory()); useEffect(() => { const listener = async (data) => { diff --git a/sidebar/stolen-data-cluster.tsx b/sidebar/stolen-data-cluster.tsx index d7517d1..1f62f5d 100644 --- a/sidebar/stolen-data-cluster.tsx +++ b/sidebar/stolen-data-cluster.tsx @@ -1,7 +1,7 @@ import React from "react"; import memory from "../memory"; import { MergedStolenDataEntry, Sources } from "../request-cluster"; -import { hyphenate } from "../util"; +import { getMemory, hyphenate } from "../util"; function StolenDataValueTable({ entry, @@ -81,7 +81,7 @@ export default function StolenDataCluster({ minValueLength: number; cookiesOnly: boolean; }) { - const cluster = memory.getClustersForOrigin(origin)[shorthost]; + const cluster = getMemory().getClustersForOrigin(origin)[shorthost]; const icons: Record = { cookie: "🍪", pathname: "🛣", @@ -96,7 +96,7 @@ export default function StolenDataCluster({ memory.removeCookiesFor(origin, shorthost)} + onClick={() => getMemory().removeCookiesFor(origin, shorthost)} > Wyczyść cookiesy diff --git a/sidebar/stolen-data.tsx b/sidebar/stolen-data.tsx index aa00a09..b6e4e1a 100644 --- a/sidebar/stolen-data.tsx +++ b/sidebar/stolen-data.tsx @@ -1,8 +1,7 @@ import React from "react"; -import memory from "../memory"; import { RequestCluster } from "../request-cluster"; import StolenDataCluster from "./stolen-data-cluster"; -import { getshorthost } from "../util"; +import { getMemory, getshorthost } from "../util"; export function StolenData({ origin, @@ -20,7 +19,7 @@ export function StolenData({ if (!origin) { return
; } - const clusters = Object.values(memory.getClustersForOrigin(origin)).sort( + const clusters = Object.values(getMemory().getClustersForOrigin(origin)).sort( RequestCluster.sortCompare ); return ( @@ -32,7 +31,7 @@ export function StolenData({ + {clusters .filter((cluster) => !cookiesOnly || cluster.hasCookies()) diff --git a/util.ts b/util.ts index 5e4128b..f2edf41 100644 --- a/util.ts +++ b/util.ts @@ -1,5 +1,6 @@ import { EventEmitter } from "events"; import { Dispatch, SetStateAction, useEffect, useState } from "react"; +import Memory from "./memory"; export type Unpromisify = T extends Promise ? R : T; export type Unarray = T extends Array ? R : T; @@ -76,3 +77,7 @@ export function isURL(str: unknown): str is string { export function hyphenate(str: string): string { return str.replace(/[_\[A-Z]/g, `${String.fromCharCode(173)}$&`); } + +export function getMemory(): Memory { + return (browser.extension.getBackgroundPage().window as any).memory as Memory; +}