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 { 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<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;
}
}
}
}
import { RequestCluster } from "./request-cluster";
class Memory extends EventEmitter {
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 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 (
<div>
<h2>
@ -74,10 +69,7 @@ const StolenDataRow = ({
<table>
<tbody>
{cluster
.getCookiesContent()
.map(parseCookie)
.map((o) => Object.entries(o))
.reduce((a, b) => a.concat(b), [])
.getCookiesContent({ minValueLength })
.map(([cookie_name, cookie_value]) => (
<tr>
<th style={{ maxWidth: "200px", wordWrap: "break-word" }}>
@ -95,9 +87,11 @@ const StolenDataRow = ({
const StolenData = ({
pickedTab,
refreshToken,
minValueLength,
}: {
pickedTab: number | null;
refreshToken: number;
minValueLength: number;
}) => {
const [tab, setTab] = useState<Tab | null>(null);
useEffect(() => {
@ -120,6 +114,7 @@ const StolenData = ({
shorthost={cluster.id}
key={cluster.id}
refreshToken={refreshToken}
minValueLength={minValueLength}
/>
))}
</div>
@ -130,6 +125,7 @@ const StolenData = ({
const Sidebar = () => {
console.log("rendering!");
const [pickedTab, setPickedTab] = useState<number | null>(null);
const [minValueLength, setMinValueLength] = useState<number | null>(3);
const counter = useEmitter(memory);
return (
<>
@ -142,7 +138,16 @@ const Sidebar = () => {
Wybierz aktywną kartę{" "}
</button>
</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}
/>
</>
);
};