From 4d19a77a61d8ddd0b5c5fa9a4685c5a44c08c870 Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Sun, 3 Oct 2021 16:33:23 +0200 Subject: [PATCH] Add filter for value length --- memory.ts | 47 +------------------------------------- request-cluster.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++ sidebar.tsx | 35 ++++++++++++++++------------ 3 files changed, 78 insertions(+), 61 deletions(-) create mode 100644 request-cluster.ts diff --git a/memory.ts b/memory.ts index 696d098..f4eec54 100644 --- a/memory.ts +++ b/memory.ts @@ -1,52 +1,7 @@ import ExtendedRequest from "./extended-request"; import { getshorthost } from "./util"; import { EventEmitter } from "events"; - -export class RequestCluster extends EventEmitter { - public requests: ExtendedRequest[] = []; - constructor(public id: string) { - super(); - } - add(request: ExtendedRequest) { - this.requests.push(request); - this.emit("change"); - } - - hasCookies() { - for (const request of this.requests) { - if (request.hasCookie()) { - return true; - } - } - return false; - } - - getCookiesContent(): string[] { - const cookieValues = new Set(); - for (const request of this.requests) { - if (request.hasCookie()) { - cookieValues.add(request.getCookie()); - } - } - return Array.from(cookieValues.values()); - } - - static sortCompare(a: RequestCluster, b: RequestCluster) { - if (a.hasCookies() == b.hasCookies()) { - if (a.id < b.id) { - return -1; - } else { - return 1; - } - } else { - if (a.hasCookies()) { - return -1; - } else { - return 1; - } - } - } -} +import { RequestCluster } from "./request-cluster"; class Memory extends EventEmitter { tab_to_history = {} as Record>; diff --git a/request-cluster.ts b/request-cluster.ts new file mode 100644 index 0000000..ef13518 --- /dev/null +++ b/request-cluster.ts @@ -0,0 +1,57 @@ +import { EventEmitter } from "events"; +import ExtendedRequest from "./extended-request"; +import { parseCookie } from "./util"; + +export class RequestCluster extends EventEmitter { + public requests: ExtendedRequest[] = []; + constructor(public id: string) { + super(); + } + add(request: ExtendedRequest) { + this.requests.push(request); + this.emit("change"); + } + + hasCookies() { + for (const request of this.requests) { + if (request.hasCookie()) { + return true; + } + } + return false; + } + + getCookiesContent({ + minValueLength, + }: { + minValueLength: number; + }): [string, string][] { + const cookieValues = new Set(); + for (const request of this.requests) { + if (request.hasCookie()) { + cookieValues.add(request.getCookie()); + } + } + return Array.from(cookieValues.values()) + .map(parseCookie) + .map((o) => Object.entries(o)) + .reduce((a, b) => a.concat(b), []) + .filter(([_, value]) => value.length >= minValueLength); + } + + static sortCompare(a: RequestCluster, b: RequestCluster) { + if (a.hasCookies() == b.hasCookies()) { + if (a.id < b.id) { + return -1; + } else { + return 1; + } + } else { + if (a.hasCookies()) { + return -1; + } else { + return 1; + } + } + } +} diff --git a/sidebar.tsx b/sidebar.tsx index 4f57d77..d54f0e1 100644 --- a/sidebar.tsx +++ b/sidebar.tsx @@ -1,7 +1,8 @@ import React, { useEffect, useState } from "react"; import ReactDOM from "react-dom"; -import memory, { RequestCluster } from "./memory"; -import { parseCookie, Tab, useEmitter } from "./util"; +import memory from "./memory"; +import { RequestCluster } from "./request-cluster"; +import { Tab, useEmitter } from "./util"; async function getTabByID(id: number) { const tabs = await browser.tabs.query({ currentWindow: true }); @@ -51,20 +52,14 @@ const StolenDataRow = ({ tabID, shorthost, refreshToken, + minValueLength, }: { tabID: number; shorthost: string; refreshToken: number; + minValueLength: number; }) => { const cluster = memory.getClustersForTab(tabID)[shorthost]; - console.log( - "!!", - cluster - .getCookiesContent() - .map(parseCookie) - .map((o) => Object.entries(o)) - .reduce((a, b) => a.concat(b), []) - ); return (

@@ -74,10 +69,7 @@ const StolenDataRow = ({ {cluster - .getCookiesContent() - .map(parseCookie) - .map((o) => Object.entries(o)) - .reduce((a, b) => a.concat(b), []) + .getCookiesContent({ minValueLength }) .map(([cookie_name, cookie_value]) => (
@@ -95,9 +87,11 @@ const StolenDataRow = ({ const StolenData = ({ pickedTab, refreshToken, + minValueLength, }: { pickedTab: number | null; refreshToken: number; + minValueLength: number; }) => { const [tab, setTab] = useState(null); useEffect(() => { @@ -120,6 +114,7 @@ const StolenData = ({ shorthost={cluster.id} key={cluster.id} refreshToken={refreshToken} + minValueLength={minValueLength} /> ))} @@ -130,6 +125,7 @@ const StolenData = ({ const Sidebar = () => { console.log("rendering!"); const [pickedTab, setPickedTab] = useState(null); + const [minValueLength, setMinValueLength] = useState(3); const counter = useEmitter(memory); return ( <> @@ -142,7 +138,16 @@ const Sidebar = () => { Wybierz aktywnÄ… kartÄ™{" "} - + setMinValueLength(parseInt(e.target.value))} + /> + ); };