rentgen/sidebar.tsx

177 lines
4.2 KiB
TypeScript
Raw Normal View History

2021-10-03 09:03:56 +02:00
import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";
2021-10-03 16:33:23 +02:00
import memory from "./memory";
2021-10-04 18:51:51 +02:00
import { RequestCluster, Sources } from "./request-cluster";
2021-10-03 16:33:23 +02:00
import { Tab, useEmitter } from "./util";
2021-10-03 09:03:56 +02:00
async function getTabByID(id: number) {
const tabs = await browser.tabs.query({ currentWindow: true });
return tabs.find((tab) => tab.id == id);
}
async function getCurrentTab() {
const [tab] = await browser.tabs.query({
active: true,
windowId: browser.windows.WINDOW_ID_CURRENT,
});
return tab.id;
}
const TabDropdown = ({
setPickedTab,
pickedTab,
}: {
setPickedTab: (tab_id: number) => void;
pickedTab: number;
}) => {
const [tabs, setTabs] = useState([]);
useEffect(() => {
browser.tabs.query({ currentWindow: true }).then(setTabs);
}, []);
return (
<select
id="tab_dropdown"
value={pickedTab}
onChange={async (e) => {
setPickedTab(parseInt(e.target.value));
}}
>
{tabs.map((tab) => (
<option value={tab.id} key={tab.id}>
{tab.title}
</option>
))}
</select>
);
};
const StolenDataRow = ({
tabID,
shorthost,
refreshToken,
2021-10-03 16:33:23 +02:00
minValueLength,
2021-10-03 09:03:56 +02:00
}: {
tabID: number;
shorthost: string;
refreshToken: number;
2021-10-03 16:33:23 +02:00
minValueLength: number;
2021-10-03 09:03:56 +02:00
}) => {
const cluster = memory.getClustersForTab(tabID)[shorthost];
2021-10-04 18:51:51 +02:00
const icons: Record<Sources, string> = {
cookie: "🍪",
pathname: "🛣",
queryparams: "🅿",
header: "H",
2021-10-04 18:51:51 +02:00
};
2021-10-03 09:03:56 +02:00
return (
<div>
<h2>
{cluster.id} {cluster.hasCookies() ? "🍪" : ""} x
{cluster.requests.length}
</h2>
<table>
<tbody>
2021-10-03 20:13:36 +02:00
{cluster.getStolenData({ minValueLength }).map((entry) => (
<tr>
<th style={{ maxWidth: "200px", wordWrap: "break-word" }}>
{entry.name}
</th>
2021-10-04 18:51:51 +02:00
<td>{icons[entry.source]}</td>
<td style={{ wordWrap: "anywhere" as any }}>
{entry.value} {entry.isIAB ? "!!!!! IAB" : ""}
</td>
2021-10-03 20:13:36 +02:00
</tr>
))}
2021-10-03 09:03:56 +02:00
</tbody>
</table>
</div>
);
};
const StolenData = ({
pickedTab,
refreshToken,
2021-10-03 16:33:23 +02:00
minValueLength,
2021-10-03 09:03:56 +02:00
}: {
pickedTab: number | null;
refreshToken: number;
2021-10-03 16:33:23 +02:00
minValueLength: number;
2021-10-03 09:03:56 +02:00
}) => {
const [tab, setTab] = useState<Tab | null>(null);
useEffect(() => {
getTabByID(pickedTab).then(setTab);
}, [pickedTab]);
if (!pickedTab || !tab) {
return <div></div>;
}
const clusters = Object.values(memory.getClustersForTab(pickedTab)).sort(
RequestCluster.sortCompare
);
return (
<div style={{ padding: "5px" }}>
{" "}
<div>
2021-10-03 20:13:36 +02:00
<h1>
<img src={tab.favIconUrl} width="20" height="20" /> {tab.title}
</h1>
2021-10-03 09:03:56 +02:00
{clusters.map((cluster) => (
<StolenDataRow
tabID={pickedTab}
shorthost={cluster.id}
key={cluster.id}
refreshToken={refreshToken}
2021-10-03 16:33:23 +02:00
minValueLength={minValueLength}
2021-10-03 09:03:56 +02:00
/>
))}
</div>
</div>
);
};
2021-10-03 20:13:36 +02:00
const Options = ({ minValueLength, setMinValueLength }) => {
return (
<fieldset>
<h3>Zaawansowane ustawienia</h3>
<label htmlFor="minValueLength">
Pokazuj tylko wartości o długości co najmniej{" "}
</label>
<input
type="number"
id="minValueLength"
value={minValueLength}
onChange={(e) => setMinValueLength(parseInt(e.target.value))}
/>
</fieldset>
);
};
2021-10-03 09:03:56 +02:00
const Sidebar = () => {
const [pickedTab, setPickedTab] = useState<number | null>(null);
2021-10-04 18:51:51 +02:00
const [minValueLength, setMinValueLength] = useState<number | null>(7);
2021-10-03 09:03:56 +02:00
const counter = useEmitter(memory);
return (
<>
<div id="selector">
<TabDropdown setPickedTab={setPickedTab} pickedTab={pickedTab} />
<button
id="get_current_tab_button"
onClick={async () => setPickedTab(await getCurrentTab())}
>
Wybierz aktywną kartę{" "}
</button>
</div>
2021-10-03 20:13:36 +02:00
<Options
minValueLength={minValueLength}
setMinValueLength={setMinValueLength}
2021-10-03 16:33:23 +02:00
/>
<StolenData
pickedTab={pickedTab}
refreshToken={counter}
minValueLength={minValueLength}
/>
2021-10-03 09:03:56 +02:00
</>
);
};
ReactDOM.render(<Sidebar />, document.getElementById("app"));