Working popup

This commit is contained in:
Kuba Orlik 2021-05-21 22:28:19 +02:00
parent 750b68fa24
commit 4070fac3e0
4 changed files with 59 additions and 10 deletions

View File

@ -16,7 +16,7 @@
"browser_action": { "browser_action": {
"default_icon": "border-48.png", "default_icon": "border-48.png",
"default_title": "Pokaż problematyczne requesty", "default_title": "Pokaż problematyczne requesty",
"default_popup": "popup/choose_beast.html" "default_popup": "popup.html"
}, },
"icons": { "icons": {
"48": "icons/border-48.png" "48": "icons/border-48.png"

3
popup.html Normal file
View File

@ -0,0 +1,3 @@
<button id="clean">clean memory</button>
<div id="output"></div>
<script src="popup.js"></script>

29
popup.js Normal file
View File

@ -0,0 +1,29 @@
output.innerHTML = "loading...";
let tabid = null;
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;
}
});
function render(memory = {}) {
let output_txt = "";
if (!memory?.[tabid]) {
output_txt = "No data for this tab";
}
Object.keys(memory[tabid]).forEach(
(host) => (output_txt += /* HTML */ `${host}</br>`)
);
output.innerHTML = output_txt;
}
chrome.runtime.sendMessage({ msg: "get_memory" }, (memory) => {
render(memory);
});
clean.onclick = () => {
chrome.runtime.sendMessage({ msg: "clear_memory" }, render);
};

View File

@ -1,5 +1,7 @@
console.log("PROBLEMATIC REQUESTS"); console.log("PROBLEMATIC REQUESTS");
let memory = {};
// const isThirdParty = (arg) => arg.urlClassification.thirdParty.length > 0; // const isThirdParty = (arg) => arg.urlClassification.thirdParty.length > 0;
async function isThirdParty(request) { async function isThirdParty(request) {
const request_url = new URL(request.url); const request_url = new URL(request.url);
@ -18,7 +20,7 @@ const getReferer = (arg) =>
const getOrigin = async (arg) => { const getOrigin = async (arg) => {
let url; let url;
if (arg.tabId) { if (arg.tabId && arg.tabId >= 0) {
const tab = await browser.tabs.get(arg.tabId); const tab = await browser.tabs.get(arg.tabId);
url = tab.url; url = tab.url;
} else { } else {
@ -40,16 +42,31 @@ browser.webRequest.onBeforeSendHeaders.addListener(
(await exposesOrigin(request)) (await exposesOrigin(request))
) { ) {
const has_cookie = hasCookie(request); const has_cookie = hasCookie(request);
fn = has_cookie ? console.warn : console.log; if (!memory[request.tabId]) {
fn( memory[request.tabId] = {};
"Leaked referrer! Has cookie:", }
hasCookie(request), const shorthost = new URL(request.url).host
request.url, .match(/((\.[^.]+){2}$)/)[0]
"referer was", .slice(1);
getReferer(request) if (!memory[request.tabId][shorthost]) {
); memory[request.tabId][shorthost] = [];
}
memory[request.tabId][shorthost].push({ url: request.url, has_cookie });
} }
}, },
{ urls: ["<all_urls>"] }, { urls: ["<all_urls>"] },
["requestHeaders"] ["requestHeaders"]
); );
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (sender.tab) {
return;
}
console.log("got message!", request);
if (request?.msg === "get_memory") {
sendResponse(memory);
} else if (request?.msg === "clear_memory") {
console.log("memory cleared");
memory = {};
}
});