From 52b3ae39ea38dddb1c2f4ed3876a40cc51e428d9 Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Sun, 7 Nov 2021 13:01:01 +0100 Subject: [PATCH] Separate directory for sidebar views --- manifest.json | 2 +- popup.html | 9 -- popup.js | 90 ------------------- request-cluster.ts | 41 +++++++-- sidebar.html => sidebar/sidebar.html | 3 +- sidebar.tsx => sidebar/sidebar.tsx | 6 +- .../stolen-data-cluster.tsx | 45 +++++++--- stolen-data.tsx => sidebar/stolen-data.tsx | 6 +- tab-dropdown.tsx => sidebar/tab-dropdown.tsx | 0 9 files changed, 79 insertions(+), 123 deletions(-) delete mode 100644 popup.html delete mode 100644 popup.js rename sidebar.html => sidebar/sidebar.html (93%) rename sidebar.tsx => sidebar/sidebar.tsx (94%) rename stolen-data-cluster.tsx => sidebar/stolen-data-cluster.tsx (67%) rename stolen-data.tsx => sidebar/stolen-data.tsx (92%) rename tab-dropdown.tsx => sidebar/tab-dropdown.tsx (100%) diff --git a/manifest.json b/manifest.json index c1220db..1d346e8 100644 --- a/manifest.json +++ b/manifest.json @@ -7,7 +7,7 @@ "sidebar_action": { "default_title": "ICD Skaner", - "default_panel": "sidebar.html", + "default_panel": "sidebar/sidebar.html", "default_icon": "sidebar_icon.png" }, "icons": { diff --git a/popup.html b/popup.html deleted file mode 100644 index 689799c..0000000 --- a/popup.html +++ /dev/null @@ -1,9 +0,0 @@ - - - - -
- - - - diff --git a/popup.js b/popup.js deleted file mode 100644 index 367df1b..0000000 --- a/popup.js +++ /dev/null @@ -1,90 +0,0 @@ -output.innerHTML = "loading..."; -let tabid = null; -let tab = null; -let memory = null; - -chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { - var currTab = tabs[0]; - if (currTab) { - // Sanity check - /* document.write(JSON.stringify(currTab)); */ - tabid = currTab.id; - tab = currTab; - } -}); - -function atLeastOneCookiedRequest(requests) { - return requests.some((request) => request.has_cookie); -} - -function sortByShorthost(tabdata) { - return Object.entries(tabdata).sort(([shorthost1], [shorthost2]) => { - return shorthost1 > shorthost2; - }); -} - -function extractAllCookies(requests) { - return Array.from(new Set(requests.map((request) => request.cookie))).filter( - (cookie) => cookie !== undefined - ); -} - -function render(memory = {}) { - let output_txt = ""; - if (!memory?.[tabid]) { - output_txt = "No data for this tab"; - output.innerHTML = output_txt; - return; - } - output_txt = /* HTML */ `

- Część Twojej historii przeglądania została wysłana przez stronę ${tab.url} - do: -

- `; - sortByShorthost(memory[tabid]).forEach(([shorthost, requests]) => { - output_txt += /* HTML */ ` -
  • - ${shorthost} - ${atLeastOneCookiedRequest(requests) - ? /* HTML */ `🍪 ` - : ""} -
  • - `; - }); - output_txt += ""; - output.innerHTML = output_txt; -} - -chrome.runtime.sendMessage({ msg: "get_memory" }, (_memory) => { - memory = _memory; - render(memory); -}); - -clean.onclick = () => { - chrome.runtime.sendMessage({ msg: "clear_memory" }, (memory_) => { - memory = memory_; - render(memory); - }); -}; - -function getDate() { - return new Date().toISOString().split("T")[0]; -} - -copy_harsh.onclick = () => { - const text_html = harsh_email_template(); - navigator.clipboard.write([ - new ClipboardItem({ "text/plain": text_html, "text/html": text_html }), - ]); -}; - -copy_polite.onclick = () => { - const text_html = polite_email_template(); - navigator.clipboard.write([ - new ClipboardItem({ "text/plain": text_html, "text/html": text_html }), - ]); -}; diff --git a/request-cluster.ts b/request-cluster.ts index 9d5a659..ace0c68 100644 --- a/request-cluster.ts +++ b/request-cluster.ts @@ -5,6 +5,7 @@ export type Sources = "cookie" | "pathname" | "queryparams" | "header"; import { TCString, TCModel } from "@iabtcf/core"; import { isJSONObject, isURL, parseToObject } from "./util"; +import memory from "./memory"; const id = (function* id() { let i = 0; @@ -77,17 +78,33 @@ export class StolenDataEntry { getParsedValue(key_path: string): string | Record { let object = StolenDataEntry.parseValue(this.value); - console.log("key_path", key_path); for (const key of key_path.split(".")) { if (key === "") continue; - console.log(key, object[key]); object = StolenDataEntry.parseValue(object[key]); } return object; } - addMarkedValue(key: string) { + addMark(key: string) { this.markedKeys.push(key); + memory.emit("change"); // to trigger rerender + } + + hasMark(key: string) { + return this.markedKeys.some((k) => k == key); + } + + removeMark(key: string) { + this.markedKeys = this.markedKeys.filter((e) => e != key); + memory.emit("change"); // to trigger rerender + } + + toggleMark(key: string) { + if (this.hasMark(key)) { + this.removeMark(key); + } else { + this.addMark(key); + } } } @@ -131,8 +148,22 @@ export class MergedStolenDataEntry { ); } - addMarkedValue(key: string) { - this.entries.forEach((entry) => entry.addMarkedValue(key)); + addMark(key: string) { + this.entries.forEach((entry) => entry.addMark(key)); + } + + getMarkedValues() { + return this.entries + .map((entry) => entry.markedKeys) + .reduce((a, b) => a.concat(b), []); + } + + hasMark(key: string): boolean { + return this.entries.some((entry) => entry.hasMark(key)); + } + + toggleMark(key: string): void { + this.entries.forEach((entry) => entry.toggleMark(key)); } } diff --git a/sidebar.html b/sidebar/sidebar.html similarity index 93% rename from sidebar.html rename to sidebar/sidebar.html index ec8c448..c14104e 100644 --- a/sidebar.html +++ b/sidebar/sidebar.html @@ -39,6 +39,7 @@
    - + + diff --git a/sidebar.tsx b/sidebar/sidebar.tsx similarity index 94% rename from sidebar.tsx rename to sidebar/sidebar.tsx index d7f96f3..354becd 100644 --- a/sidebar.tsx +++ b/sidebar/sidebar.tsx @@ -1,9 +1,9 @@ import React, { useEffect, useState } from "react"; import ReactDOM from "react-dom"; -import memory from "./memory"; -import Options from "./options"; +import memory from "../memory"; +import Options from "../options"; import { StolenData } from "./stolen-data"; -import { useEmitter } from "./util"; +import { useEmitter } from "../util"; async function getCurrentTab() { const [tab] = await browser.tabs.query({ diff --git a/stolen-data-cluster.tsx b/sidebar/stolen-data-cluster.tsx similarity index 67% rename from stolen-data-cluster.tsx rename to sidebar/stolen-data-cluster.tsx index 6678d0f..d7517d1 100644 --- a/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 memory from "../memory"; +import { MergedStolenDataEntry, Sources } from "../request-cluster"; +import { hyphenate } from "../util"; function StolenDataValueTable({ entry, @@ -15,7 +15,14 @@ function StolenDataValueTable({ {Object.keys(entry.getParsedValues(prefixKey)[0]).map((key) => ( - {hyphenate(key)} + { + entry.toggleMark(prefixKey); + e.stopPropagation(); + }} + > + {hyphenate(key)} + ; + body = <>; + } else if (typeof value === "string") { + body = ( +
    + {entry.getParsedValues(prefixKey)[0] as string} +
    + ); + } else { + body = ; } - if (typeof value === "string") { - return <>{entry.getParsedValues(prefixKey)[0] as string}; - } - return ; + return ( +
    { + entry.toggleMark(prefixKey); + e.stopPropagation(); + }} + data-marks={entry.getMarkedValues().join(", ")} + > + {body} +
    + ); } export default function StolenDataCluster({ @@ -68,8 +91,8 @@ export default function StolenDataCluster({ return (

    - {cluster.id} {cluster.hasCookies() ? "🍪" : ""} x - {cluster.requests.length}{" "} + {cluster.id}{" "} + {cluster.hasCookies() ? "🍪" : ""} x{cluster.requests.length}{" "}