Add filter for value length

This commit is contained in:
Kuba Orlik 2021-10-03 16:33:23 +02:00
parent 9441ddd98e
commit 4d19a77a61
3 changed files with 78 additions and 61 deletions

View File

@ -1,52 +1,7 @@
import ExtendedRequest from "./extended-request"; import ExtendedRequest from "./extended-request";
import { getshorthost } from "./util"; import { getshorthost } from "./util";
import { EventEmitter } from "events"; import { EventEmitter } from "events";
import { RequestCluster } from "./request-cluster";
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<string>();
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;
}
}
}
}
class Memory extends EventEmitter { class Memory extends EventEmitter {
tab_to_history = {} as Record<string, Record<string, RequestCluster>>; tab_to_history = {} as Record<string, Record<string, RequestCluster>>;

57
request-cluster.ts Normal file
View File

@ -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<string>();
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;
}
}
}
}

View File

@ -1,7 +1,8 @@
import React, { useEffect, useState } from "react"; import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom"; import ReactDOM from "react-dom";
import memory, { RequestCluster } from "./memory"; import memory from "./memory";
import { parseCookie, Tab, useEmitter } from "./util"; import { RequestCluster } from "./request-cluster";
import { Tab, useEmitter } from "./util";
async function getTabByID(id: number) { async function getTabByID(id: number) {
const tabs = await browser.tabs.query({ currentWindow: true }); const tabs = await browser.tabs.query({ currentWindow: true });
@ -51,20 +52,14 @@ const StolenDataRow = ({
tabID, tabID,
shorthost, shorthost,
refreshToken, refreshToken,
minValueLength,
}: { }: {
tabID: number; tabID: number;
shorthost: string; shorthost: string;
refreshToken: number; refreshToken: number;
minValueLength: number;
}) => { }) => {
const cluster = memory.getClustersForTab(tabID)[shorthost]; const cluster = memory.getClustersForTab(tabID)[shorthost];
console.log(
"!!",
cluster
.getCookiesContent()
.map(parseCookie)
.map((o) => Object.entries(o))
.reduce((a, b) => a.concat(b), [])
);
return ( return (
<div> <div>
<h2> <h2>
@ -74,10 +69,7 @@ const StolenDataRow = ({
<table> <table>
<tbody> <tbody>
{cluster {cluster
.getCookiesContent() .getCookiesContent({ minValueLength })
.map(parseCookie)
.map((o) => Object.entries(o))
.reduce((a, b) => a.concat(b), [])
.map(([cookie_name, cookie_value]) => ( .map(([cookie_name, cookie_value]) => (
<tr> <tr>
<th style={{ maxWidth: "200px", wordWrap: "break-word" }}> <th style={{ maxWidth: "200px", wordWrap: "break-word" }}>
@ -95,9 +87,11 @@ const StolenDataRow = ({
const StolenData = ({ const StolenData = ({
pickedTab, pickedTab,
refreshToken, refreshToken,
minValueLength,
}: { }: {
pickedTab: number | null; pickedTab: number | null;
refreshToken: number; refreshToken: number;
minValueLength: number;
}) => { }) => {
const [tab, setTab] = useState<Tab | null>(null); const [tab, setTab] = useState<Tab | null>(null);
useEffect(() => { useEffect(() => {
@ -120,6 +114,7 @@ const StolenData = ({
shorthost={cluster.id} shorthost={cluster.id}
key={cluster.id} key={cluster.id}
refreshToken={refreshToken} refreshToken={refreshToken}
minValueLength={minValueLength}
/> />
))} ))}
</div> </div>
@ -130,6 +125,7 @@ const StolenData = ({
const Sidebar = () => { const Sidebar = () => {
console.log("rendering!"); console.log("rendering!");
const [pickedTab, setPickedTab] = useState<number | null>(null); const [pickedTab, setPickedTab] = useState<number | null>(null);
const [minValueLength, setMinValueLength] = useState<number | null>(3);
const counter = useEmitter(memory); const counter = useEmitter(memory);
return ( return (
<> <>
@ -142,7 +138,16 @@ const Sidebar = () => {
Wybierz aktywną kartę{" "} Wybierz aktywną kartę{" "}
</button> </button>
</div> </div>
<StolenData pickedTab={pickedTab} refreshToken={counter} /> <input
type="number"
value={minValueLength}
onChange={(e) => setMinValueLength(parseInt(e.target.value))}
/>
<StolenData
pickedTab={pickedTab}
refreshToken={counter}
minValueLength={minValueLength}
/>
</> </>
); );
}; };