From 9f8fd6973d852d8c2cbe3d5adfcf6edd73280dbf Mon Sep 17 00:00:00 2001 From: ThomazPom Date: Wed, 18 Sep 2024 07:48:03 +0200 Subject: [PATCH] PreRelease 1.5.61 --- .github/workflows/main.yml | 1 + background.js | 66 +++++++++++++++++++++++++++++--------- manifest.json | 2 +- resolveIDKVars.js | 25 +++++++++++++++ 4 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 resolveIDKVars.js diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 83f9e04..a40898b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,6 +28,7 @@ jobs: imageWorker.js bg.jpg background.js + resolveIDKVars.js icons/ steps: diff --git a/background.js b/background.js index c1b40e0..bed6210 100644 --- a/background.js +++ b/background.js @@ -2352,6 +2352,8 @@ background: { uDark.resolvedIDKVars_action_timeout = 400; // edit_str from 2024 january was ok with 210 for both editing and messaging, 250 Should be enough for now uDark.fixedRandom = Math.random(); + globalThis.browser.webRequest.onSendHeaders.removeListener(dark_object.misc.setEligibleRequestBeforeData); + globalThis.browser.webRequest.onHeadersReceived.removeListener(dark_object.misc.editBeforeData); globalThis.browser.webRequest.onHeadersReceived.removeListener(dark_object.misc.editBeforeData); globalThis.browser.webRequest.onHeadersReceived.removeListener(dark_object.misc.editBeforeRequestStyleSheet); globalThis.browser.webRequest.onBeforeRequest.removeListener(dark_object.misc.editBeforeRequestImage); @@ -2368,6 +2370,11 @@ background: { } if (!res.disable_webext && uDark.userSettings.properWhiteList.length) { + globalThis.browser.webRequest.onSendHeaders.addListener(dark_object.misc.setEligibleRequestBeforeData, { + urls: uDark.userSettings.properWhiteList, + types: ["main_frame", "sub_frame"] + }, ); + globalThis.browser.webRequest.onHeadersReceived.addListener(dark_object.misc.editBeforeData, { urls: uDark.userSettings.properWhiteList, types: ["main_frame", "sub_frame"] @@ -2494,8 +2501,6 @@ background: { let portKey = `port-from-cs-${connectedPort.sender.tab.id}-${connectedPort.sender.frameId}` - console.log(portKey, connectedPort, uDark.connected_cs_ports[portKey]) - uDark.connected_cs_ports[portKey] = connectedPort; connectedPort.onDisconnect.addListener(p => { console.log("Disconnected", p.sender.url, p.sender.contextId); @@ -3195,7 +3200,6 @@ background: { editBeforeRequestStyleSheet: function(details) { let options = {}; options.isCorsRequest = dark_object.misc.isCorsRequest(details); - // let stylesheetURL=(new URL(details.url)); // console.log("Loading CSS", details.url, details.requestId, details.fromCache) @@ -3206,7 +3210,7 @@ background: { console.log("If i'm lacking of knowledge, here is what i know about this request", details.tabId, details.frameId); return {} } - // console.log("Will darken", details.urlF, details.requestId, details.fromCache) + console.log("Will darken", details.url, details.requestId, details.fromCache) let filter = globalThis.browser.webRequest.filterResponseData(details.requestId); // After this instruction, browser espect us to write data to the filter and close it @@ -3284,9 +3288,10 @@ background: { chunk_manage_idk_direct(details,options,filter) { + if (!uDark.disable_remote_idk_css_edit && details.unresolvableChunks) { if (!options.unresolvableStylesheet.cssRules.length) { - // console.log("No unresolvable rules found for",details.url,"chunk",details.dataCount) + console.log("No unresolvable rules found for",details.url,"chunk",details.dataCount) return; } @@ -3330,7 +3335,7 @@ background: { let rules = [...options.unresolvableStylesheet.cssRules].map(r => r.cssText); let chunk_variables = rules.join("\n"); chunk_variables = chunk_variables.unprotect_simple("--ud-ptd-"); - globalThis.browser.tabs.executeScript(details.tabId, { + globalThis.browser.tabs.executeScript(details.tabId, { // This is not the content_script context. file:"/resolveIDKVars.js", frameId:details.frameId, runAt:"document_start", @@ -3409,9 +3414,48 @@ background: { // return details; // }, + + setEligibleRequestBeforeData(details){ + details.unEligibleRequest=(details.documentUrl || details.url).match(uDark.userSettings.exclude_regex); + details.eligibleRequest=!details.unEligibleRequest; + // Here we have to check the url or the documentUrl to know if this webpage is excluded + // It already has passed the whitelist check, this is why we only check the blacklist + // However this code executes before the content script is connected, so we can't check if it will connect or not + // Even if we could do this, like sending some bytes and waiting for he content script to connect, + // and it would be not so musch costly in terms of time, some pages as YouTube as the time i write this, somehow manages + // to send in this very first request tabID -1 and frameID 0, which is not a valid combination, and the content script will never be found + // stackoverflow says it might be related to worker threads. It's probably true with serviceWorkers + + console.log("Will check",details.url,"made by",details.documentUrl) + console.log("Is eligible for uDark",details.eligibleRequest) + if (!details.eligibleRequest) { + + // console.log("Excluding", details.url,"made by",details.documentUrl) + delete uDark.connected_cs_ports["port-from-cs-" + details.tabId + "-" + details.frameId]; + // As bellow is marking as arriving soon + // It is possible to have a page that starts loading, we mark it as arriving soon + // loading stops, for whatever reason, and the content script does not connect and therefore does not disconnects and get not deleted. + // In this case, the port will not be erased, and all resources will darkened, even if the page is not eligible for uDark + // It is testable by disablising the content script, assignation and line above; loading a darkened page, in a tab, to set the arriving soon flag, + // then loading an uneligible page in the same tab, and see if it not dakening. + // A simple delete when the page is not eligible is enough and very low cost. + // In the end we need to be in this if to avoid darkening the page, we wont be lazy and delete the port. + return {}; + + } + if (details.tabId != -1) { + // Lets be the MVP here, sometimes the content script is not connected yet, and the CSS will arrive in few milliseconds. + // This page is eligible for uDark + // console.log("I'm telling the world that",details.url,"is eligible for uDark", "on", details.tabId,details.frameId) + console.log("ERASING",details.url,"made by",details.documentUrl,uDark.connected_cs_ports["port-from-cs-" + details.tabId + "-" + details.frameId]) + uDark.connected_cs_ports["port-from-cs-" + details.tabId + "-" + details.frameId] = "ARRIVING_SOON"; + + } + + }, + editBeforeData: function(details) { - // random condition return {} and log details.url & details to see what is happening if (details.tabId == -1 && uDark.connected_options_ports_count || uDark.connected_cs_ports["port-from-popup-" + details.tabId]) { // -1 Happens sometimes, like on https://www.youtube.com/ at the time i write this, stackoverflow talks about worker threads @@ -3452,12 +3496,6 @@ background: { uDark.connected_cs_ports["port-from-cs-" + details.tabId + "-" + details.frameId] = "ARRIVING_SOON"; } - - // if( details.url=="https://www.patreon.com/ArgusVRC" ) - // { - // console.log("Data",details.url,details) - // return {} - // } var n = details.responseHeaders.length; details.headersLow = {} while (n--) { @@ -3465,7 +3503,6 @@ background: { } if (!(details.headersLow["content-type"] || "text/html").includes("text/html")) return {} details.charset = ((details.headersLow["content-type"] || "").match(/charset=([0-9A-Z-]+)/i) || ["", "utf-8"])[1] - // console.log(details.charset) details.responseHeaders = details.responseHeaders.filter(x => { var a_filter = uDark.headersDo[x.name.toLowerCase()]; @@ -3483,7 +3520,6 @@ background: { details.writeEnd += decoder.decode(event.data, { stream: true }); - // must not return this closes filter// } filter.onstop = event => { details.dataCount = 1; diff --git a/manifest.json b/manifest.json index d2ec404..1c8a929 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "UltimaDark", - "version": "1.5.60", + "version": "1.5.61", "description": "The extension uses agressive techniques to get a dark mode everywhere on internet\nThis is still highly experimental so it can also ruin your internet experience", "homepage_url": "https://github.com/ThomazPom/Moz-Ext-UltimaDark", "icons": { diff --git a/resolveIDKVars.js b/resolveIDKVars.js new file mode 100644 index 0000000..bd890e5 --- /dev/null +++ b/resolveIDKVars.js @@ -0,0 +1,25 @@ +function resolveIDKVars_direct(data) +{ + let expectedValueForResolvableColor = "rgba(255, 254, 253, 0.55)"; + let readable_variable_check_value = `rgba(255,254,253,var(--chunk_is_readable_${data.details.requestId}_${data.details.dataCount}))`; + let option = new Option(); // Option must be in the loop because once the color is set to an unkonwn variable it stays at empty string + document.head.appendChild(option); + option.style.floodColor = readable_variable_check_value; + let floodColor = getComputedStyle(option).floodColor; + option.remove(); + console.log(readable_variable_check_value); + if (floodColor != expectedValueForResolvableColor) { + return {}; + } + + console.log("Received data", data); + // The variables we are looking for might be in data.chunk we have to ensure they are "readable" to make them available to props_and_var_only_color_idk. + // This is the whole point of waiting for the chunk to be written before reading the variables out of it. + let ikd_chunk_resolved = uDark.edit_str(data.chunk, false, false, false, true); + let props_and_var_only_color_idk = uDark.edit_str(data.chunk_variables, false, false, false, "partial_idk"); + data.resolved = true; + data.chunk = ikd_chunk_resolved; + data.chunk_variables = props_and_var_only_color_idk + return data +} +console.log("Resolve IDK Vars is ready",resolveIDKVars_direct,window); \ No newline at end of file