rentgen/popup.js

91 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-05-21 22:28:19 +02:00
output.innerHTML = "loading...";
let tabid = null;
2021-05-22 09:40:47 +02:00
let tab = null;
let memory = null;
2021-05-21 22:28:19 +02:00
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
var currTab = tabs[0];
if (currTab) {
// Sanity check
/* document.write(JSON.stringify(currTab)); */
tabid = currTab.id;
2021-05-22 09:40:47 +02:00
tab = currTab;
2021-05-21 22:28:19 +02:00
}
});
2021-05-22 09:40:47 +02:00
function atLeastOneCookiedRequest(requests) {
return requests.some((request) => request.has_cookie);
}
function sortByShorthost(tabdata) {
return Object.entries(tabdata).sort(([shorthost1], [shorthost2]) => {
return shorthost1 > shorthost2;
});
}
2021-06-29 21:07:50 +02:00
function extractAllCookies(requests) {
return Array.from(new Set(requests.map((request) => request.cookie))).filter(
(cookie) => cookie !== undefined
);
}
2021-05-21 22:28:19 +02:00
function render(memory = {}) {
let output_txt = "";
if (!memory?.[tabid]) {
output_txt = "No data for this tab";
2021-05-22 09:40:47 +02:00
output.innerHTML = output_txt;
return;
2021-05-21 22:28:19 +02:00
}
2021-06-29 21:07:50 +02:00
output_txt = /* HTML */ `<h2>
Część Twojej historii przeglądania została wysłana przez stronę ${tab.url}
do:
</h2>
<ul></ul>`;
2021-05-22 09:40:47 +02:00
sortByShorthost(memory[tabid]).forEach(([shorthost, requests]) => {
2021-06-29 21:07:50 +02:00
output_txt += /* HTML */ `
<li>
${shorthost}
${atLeastOneCookiedRequest(requests)
? /* HTML */ `🍪 <ul>
${extractAllCookies(requests)
.map((cookie) => `<li><code>${cookie}</code></li>`)
.join("\n")}
</ul>`
: ""}
</li>
`;
2021-05-22 09:40:47 +02:00
});
2021-06-29 21:07:50 +02:00
output_txt += "</ul>";
2021-05-21 22:28:19 +02:00
output.innerHTML = output_txt;
}
2021-05-22 09:40:47 +02:00
chrome.runtime.sendMessage({ msg: "get_memory" }, (_memory) => {
memory = _memory;
2021-05-21 22:28:19 +02:00
render(memory);
});
clean.onclick = () => {
2021-05-22 09:40:47 +02:00
chrome.runtime.sendMessage({ msg: "clear_memory" }, (memory_) => {
memory = memory_;
render(memory);
});
};
function getDate() {
return new Date().toISOString().split("T")[0];
}
2021-06-29 21:07:50 +02:00
copy_harsh.onclick = () => {
const text_html = harsh_email_template();
navigator.clipboard.write([
new ClipboardItem({ "text/plain": text_html, "text/html": text_html }),
]);
};
2021-05-22 09:40:47 +02:00
2021-06-29 21:07:50 +02:00
copy_polite.onclick = () => {
const text_html = polite_email_template();
2021-05-22 09:40:47 +02:00
navigator.clipboard.write([
new ClipboardItem({ "text/plain": text_html, "text/html": text_html }),
]);
2021-05-21 22:28:19 +02:00
};