rentgen/sidebar.tsx

244 lines
6.0 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-11-06 21:48:25 +01:00
import { getshorthost, useEmitter } from "./util";
2021-10-03 09:03:56 +02:00
async function getCurrentTab() {
const [tab] = await browser.tabs.query({
active: true,
windowId: browser.windows.WINDOW_ID_CURRENT,
});
2021-11-06 21:48:25 +01:00
return tab;
2021-10-03 09:03:56 +02:00
}
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 = ({
2021-11-06 21:48:25 +01:00
origin,
2021-10-03 09:03:56 +02:00
shorthost,
2021-10-03 16:33:23 +02:00
minValueLength,
2021-11-06 20:02:02 +01:00
cookiesOnly,
2021-10-03 09:03:56 +02:00
}: {
2021-11-06 21:48:25 +01:00
origin: string;
2021-10-03 09:03:56 +02:00
shorthost: string;
refreshToken: number;
2021-10-03 16:33:23 +02:00
minValueLength: number;
2021-11-06 20:02:02 +01:00
cookiesOnly: boolean;
2021-10-03 09:03:56 +02:00
}) => {
2021-11-06 21:48:25 +01:00
const cluster = memory.getClustersForOrigin(origin)[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
2021-11-06 21:48:25 +01:00
{cluster.requests.length}{" "}
<a
href="#"
style={{ fontSize: "10px" }}
onClick={() => cluster.removeAllCookies()}
>
Wyczyść cookiesy
</a>
2021-10-03 09:03:56 +02:00
</h2>
<table>
<tbody>
2021-11-06 20:02:02 +01:00
{cluster
.getStolenData({ minValueLength, cookiesOnly })
.map((entry) => (
2021-11-06 21:48:25 +01:00
<tr
key={origin + cluster.id + entry.getUniqueKey()}
data-key={origin + cluster.id + entry.getUniqueKey()}
2021-11-06 21:48:25 +01:00
>
2021-11-06 20:02:02 +01:00
<th style={{ maxWidth: "200px", wordWrap: "break-word" }}>
{entry.getNames().join(",")}
2021-11-06 20:02:02 +01:00
</th>
<td>{entry.getSources().map((source) => icons[source])}</td>
2021-11-06 20:02:02 +01:00
<td style={{ wordWrap: "anywhere" as any }}>
{entry.getValues()[0]}
2021-11-06 20:02:02 +01:00
</td>
</tr>
))}
2021-10-03 09:03:56 +02:00
</tbody>
</table>
</div>
);
};
const StolenData = ({
2021-11-06 21:48:25 +01:00
origin,
2021-10-03 16:33:23 +02:00
minValueLength,
2021-11-06 21:48:25 +01:00
refreshToken,
2021-11-06 20:02:02 +01:00
cookiesOnly,
2021-10-03 09:03:56 +02:00
}: {
2021-11-06 21:48:25 +01:00
origin: string;
2021-10-03 09:03:56 +02:00
refreshToken: number;
2021-10-03 16:33:23 +02:00
minValueLength: number;
2021-11-06 20:02:02 +01:00
cookiesOnly: boolean;
2021-10-03 09:03:56 +02:00
}) => {
2021-11-06 21:48:25 +01:00
if (!origin) {
2021-10-03 09:03:56 +02:00
return <div></div>;
}
2021-11-06 21:48:25 +01:00
const clusters = Object.values(memory.getClustersForOrigin(origin)).sort(
2021-10-03 09:03:56 +02:00
RequestCluster.sortCompare
);
return (
<div style={{ padding: "5px" }}>
{" "}
<div>
2021-10-03 20:13:36 +02:00
<h1>
2021-11-06 21:48:25 +01:00
{origin}
<button
style={{ marginLeft: "1rem" }}
onClick={() =>
memory.removeCookiesFor(
origin,
getshorthost(new URL(origin).host)
)
}
>
Wyczyść cookiesy 1st party
</button>
<button
style={{ marginLeft: "1rem" }}
onClick={() => memory.removeRequestsFor(origin)}
>
Wyczyść pamięć
</button>
2021-10-03 20:13:36 +02:00
</h1>
2021-11-06 20:02:02 +01:00
{clusters
.filter((cluster) => !cookiesOnly || cluster.hasCookies())
2021-11-06 21:48:25 +01:00
.map((cluster) => {
return (
<StolenDataRow
origin={origin}
shorthost={cluster.id}
key={cluster.id + origin}
refreshToken={refreshToken}
minValueLength={minValueLength}
cookiesOnly={cookiesOnly}
/>
);
})}
2021-10-03 09:03:56 +02:00
</div>
</div>
);
};
2021-11-06 20:02:02 +01:00
const Options = ({
minValueLength,
setMinValueLength,
cookiesOnly,
setCookiesOnly,
}) => {
2021-10-03 20:13:36 +02:00
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))}
/>
2021-11-06 20:02:02 +01:00
<br />
<input
type="checkbox"
id="cookiesOnly"
value={cookiesOnly}
onChange={(e) => setCookiesOnly(e.target.checked)}
/>
<label htmlFor="cookiesOnly">Pokazuj tylko dane z cookiesów</label>
2021-10-03 20:13:36 +02:00
</fieldset>
);
};
2021-10-03 09:03:56 +02:00
const Sidebar = () => {
2021-11-06 21:48:25 +01:00
const [origin, setOrigin] = useState<string | null>(null);
2021-10-04 18:51:51 +02:00
const [minValueLength, setMinValueLength] = useState<number | null>(7);
2021-11-06 20:02:02 +01:00
const [cookiesOnly, setCookiesOnly] = useState<boolean>(false);
2021-10-03 09:03:56 +02:00
const counter = useEmitter(memory);
2021-11-06 21:48:25 +01:00
useEffect(() => {
const listener = async (data) => {
console.log("tab change!");
const tab = await getCurrentTab();
const url = new URL(tab.url);
console.log(
"NEW ORIGIN",
url.origin,
url.origin.startsWith("moz-extension")
);
if (url.origin.startsWith("moz-extension")) {
return;
}
setOrigin(url.origin);
};
browser.tabs.onUpdated.addListener(listener);
return () => {
browser.tabs.onUpdated.removeListener(listener);
};
});
2021-10-03 09:03:56 +02:00
return (
<>
2021-11-06 21:48:25 +01:00
{/* <div id="selector">
2021-10-03 09:03:56 +02:00
<TabDropdown setPickedTab={setPickedTab} pickedTab={pickedTab} />
<button
id="get_current_tab_button"
onClick={async () => setPickedTab(await getCurrentTab())}
>
Wybierz aktywną kartę{" "}
</button>
2021-11-06 21:48:25 +01:00
</div> */}
2021-10-03 20:13:36 +02:00
<Options
minValueLength={minValueLength}
setMinValueLength={setMinValueLength}
2021-11-06 20:02:02 +01:00
cookiesOnly={cookiesOnly}
setCookiesOnly={setCookiesOnly}
2021-10-03 16:33:23 +02:00
/>
<StolenData
2021-11-06 21:48:25 +01:00
origin={origin}
2021-10-03 16:33:23 +02:00
refreshToken={counter}
minValueLength={minValueLength}
2021-11-06 20:02:02 +01:00
cookiesOnly={cookiesOnly}
2021-10-03 16:33:23 +02:00
/>
2021-10-03 09:03:56 +02:00
</>
);
};
ReactDOM.render(<Sidebar />, document.getElementById("app"));