Skip to content

Commit

Permalink
Tried to delegate to tab the script execution, it was too slow
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomazPom committed Sep 29, 2024
1 parent a515a73 commit cb591f9
Show file tree
Hide file tree
Showing 12 changed files with 617 additions and 424 deletions.
568 changes: 291 additions & 277 deletions background.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion imageWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ console.log("Image Service worker started")

var uDark={
background_match:/background|sprite|(?<![a-z])(bg|box|panel|fond|fundo|bck)(?![a-z])/i,
logo_match: /nav|avatar|logo|icon|alert|notif|cart|menu|tooltip|dropdown|control/i,
logo_match: /nav|avatar|logo|icon|alert|notif|cart|menu|tooltip|dropdown|control/i,
RGBToLightness: (r, g, b) => {
return (Math.max(r, g, b) + Math.min(r, g, b)) / 2;
},
Expand Down
41 changes: 41 additions & 0 deletions other-tools-and-tests/benchs/MurMurHash3bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function generateRandomString(minLen = 1000, maxLen = 2000) {
const length = Math.floor(Math.random() * (maxLen - minLen + 1)) + minLen;
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}

function benchmark_fMurmurHash3Hash(numTests = 100) {
let totalTime = 0;

for (let i = 0; i < numTests; i++) {
// Generate a random string of 1000-2000 characters
const randomString = generateRandomString();

// Start the timer
const startTime = performance.now();

// Execute the hashing function
const result = fMurmurHash3Hash(randomString);

// Stop the timer
const endTime = performance.now();

// Calculate the time taken for this iteration in milliseconds
const timeTaken = endTime - startTime;

totalTime += timeTaken;
}

// Calculate the average time
const avgTime = totalTime / numTests;

console.log(`Avg time for ${numTests} executions: ${avgTime.toFixed(2)} ms`);
console.log(`Total time for ${numTests} executions: ${totalTime.toFixed(2)} ms`);
}

// Assuming fMurmurHash3Hash is already defined in your scope, run the benchmark
benchmark_fMurmurHash3Hash(1000);
88 changes: 88 additions & 0 deletions other-tools-and-tests/benchs/mapCacheVSMurmurForCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
function generateRandomString(minLen = 5000, maxLen = 1000) {
const length = Math.floor(Math.random() * (maxLen - minLen + 1)) + minLen;
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}

function generateRandomValues(numValues = 100) {
let values = [];
for (let i = 0; i < numValues; i++) {
values.push(Math.random()); // Random float between 0 and 1
}
return values;
}

// Existing cache system using fMurmurHash3Hash function
function benchmarkExistingCache(strings, values, numTests = 100) {
let cache = {}; // Simulating the cache as an object
let totalTime = 0;

for (let i = 0; i < numTests; i++) {
let key = strings[i];
let value = values[i];

// Measure insertion time (hashing + storing)
const startInsert = performance.now();
const hashKey = fMurmurHash3Hash(key);
cache[hashKey] = value;
const endInsert = performance.now();

totalTime += (endInsert - startInsert);

// Measure retrieval time
const startRetrieve = performance.now();
const retrievedValue = cache[hashKey];
const endRetrieve = performance.now();

totalTime += (endRetrieve - startRetrieve);
}

const avgTime = totalTime / numTests;
console.log(`Existing Cache - Avg time for ${numTests} executions: ${avgTime.toFixed(2)} ms`);
console.log(`Existing Cache - Total time for ${numTests} executions: ${totalTime.toFixed(2)} ms`);
}

// New cache system using Map with no explicit hashing
function benchmarkNewCache(strings, values, numTests = 100) {
let map = new Map();
let totalTime = 0;

for (let i = 0; i < numTests; i++) {
let key = strings[i];
let value = values[i];

// Measure insertion time (storing in Map)
const startInsert = performance.now();
map.set(key, value);
const endInsert = performance.now();

totalTime += (endInsert - startInsert);

// Measure retrieval time
const startRetrieve = performance.now();
const retrievedValue = map.get(key);
const endRetrieve = performance.now();

totalTime += (endRetrieve - startRetrieve);
}

const avgTime = totalTime / numTests;
console.log(`New Cache (Map) - Avg time for ${numTests} executions: ${avgTime.toFixed(2)} ms`);
console.log(`New Cache (Map) - Total time for ${numTests} executions: ${totalTime.toFixed(2)} ms`);
}

// Pre-initialize random strings and values
const numTests = 10000;
const randomStrings = Array.from({ length: numTests }, () => generateRandomString());
const randomValues = generateRandomValues(numTests);

// Run the benchmarks
console.log("Benchmarking Existing Cache with Hashing Function:");
benchmarkExistingCache(randomStrings, randomValues, numTests);

console.log("\nBenchmarking New Cache with Map:");
benchmarkNewCache(randomStrings, randomValues, numTests);
Empty file.
143 changes: 143 additions & 0 deletions other-tools-and-tests/delegated_to_website_edit_str
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@

{ // Was too slow and needed overly complex code to write and also to debug.
handleCSSChunk(data, verify, details, filter) {
let str = details.rejectedValues;
if(data){ str += details.decoder.decode(data,{stream:true}); }


let options = { chunk:uDark.idk_cache.get(str) };

if (options.chunk) {
uDark.idk_cache.delete(str);
uDark.idk_cache.has(details.tabId) && uDark.idk_cache.get(details.tabId).delete(str);
filter.write(details.encoder.encode(options.chunk));

details.countDone+=1;
return true;
}


let edit_str_promise = globalThis.browser.tabs.executeScript(details.tabId,{code:
`
(function(){
let details =${JSON.stringify(
{
url:details.url,
tabId:details.tabId,
frameId:details.frameId,
originUrl:details.originUrl,
documentUrl:details.documentUrl,
requestId:details.requestId,
dataCount:details.dataCount,
transientCache: details.transientCache

}
)};
let options=${JSON.stringify(options)};
options.cant_resolve_vars_yet=true;
let str=uDark.edit_str(${JSON.stringify(str)},false,${JSON.stringify(verify)},details ,false,options);
if(options.unresolvableStylesheet){
if(options.hasUnresolvedVars_idk_vars){
console.log("Unresolvable idk vars",options.unresolvableStylesheet.rules);
}
let rulesArray = [...options.unresolvableStylesheet.cssRules].map(r=>({cssText:r.cssText}));
options.unresolvableStylesheet={cssRules:rulesArray};
}
delete options.cssStyleSheet;
return {str,details ,options};
})();
`
,frameId:details.frameId,allFrames:false,matchAboutBlank:true,runAt:"document_start"})
.then(promise=>promise).then(result=>result[0]);


// options.chunk = uDark.edit_str( str, false, verify, details, false, options);

return edit_str_promise.then(result=>{
options = result.options;

details.countDone+=1;
details.unresolvableChunks=details.unresolvableChunks||result.details||false;
options.chunk=result.str;
details.transientCache = result.details.transientCache;
if (options.chunk.message) {
details.rejectedValues = str; // Keep rejected values for later use
return;
} else {
details.rejectedValues = "";
if (options.chunk.rejected) {
details.rejectedValues = options.chunk.rejected;
options.chunk = options.chunk.str;
}
}
options.str_key_cache = str;
dark_object.misc.chunk_manage_idk_direct(details, options, filter);
filter.write(details.encoder.encode(options.chunk));

})

},
editBeforeRequestStyleSheet: function(details) {
let options = {};
options.isCorsRequest = dark_object.misc.isCorsRequest(details);

// console.log("Loading CSS", details.url, details.requestId, details.fromCache)

// Util 2024 jan 02 we were checking details.documentUrl, or details.url to know if a stylesheet was loaded in a excluded page
// Since only CS ports that matches blaclist and whitelist are connected, we can simply check if this resource has a corresponding CS port
if (!uDark.getPort(details)) {
// console.log("CSS", "No port found for", details.url, "loaded by webpage:", details.originUrl, "Assuming it is not an eligible webpage, or even blocked by another extension");
// console.log("If i'm lacking of knowledge, here is what i know about this request", details.tabId, details.frameId);
return {}
}

let filter = globalThis.browser.webRequest.filterResponseData(details.requestId); // After this instruction, browser espect us to write data to the filter and close it

let n = details.responseHeaders.length;
details.headersLow = {}
while (n--) {
details.headersLow[details.responseHeaders[n].name.toLowerCase()] = details.responseHeaders[n].value;
}
console.log("Will darken", details.url, details.requestId, details.fromCache,details)

details.charset = ((details.headersLow["content-type"] || "").match(/charset=([0-9A-Z-]+)/i) || ["", "utf-8"])[1]
details.decoder = new TextDecoder(details.charset)
details.encoder = new TextEncoder();
details.dataCount = 0;
details.countDone = 0;
details.rejectedValues = "";



// // ondata event handler
filter.ondata = async event => {
details.dataCount++;
dark_object.misc.handleCSSChunk(event.data, true, details, filter);
};

// onstop event handler

// onstop event handler
filter.onstop = event => {
if (details.rejectedValues.length > 0) {
let handleResult = dark_object.misc.handleCSSChunk(null, false, details, filter);
// if(handleResult.then){console.log("Closing filter 1"); return handleResult.then(x=>filter.disconnect())}
}

let intervalClose=setInterval(()=>{
if(details.countDone>=details.dataCount){
clearInterval(intervalClose);
filter.disconnect(); // Ensure disconnection after completion of last chunk
}
else{
console.log("Not closing filter 2",details.countDone,"/",details.dataCount,"for",details.url,details.requestId);
}
},100)

};
// return {redirectUrl:details.url};
// return {responseHeaders:[{name:"Vary",value:"*"},{name:"Location",value:details.url}]};
// return {};
// must not return this closes filter//
}
}
51 changes: 0 additions & 51 deletions other-tools-and-tests/demo.js

This file was deleted.

Binary file added other-tools-and-tests/q3yhoc3c.bmp
Binary file not shown.
52 changes: 52 additions & 0 deletions other-tools-and-tests/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tableau 255x255 avec Dégradé</title>
<style>
table {
border-collapse: collapse;
border: 1px solid black;
}

td {
border: 1px solid black;
width: 3px;
height: 3px;
}
</style>
</head>
<body>

<script>
document.addEventListener('DOMContentLoaded', function () {
// Créer un élément de tableau
var table = document.createElement('table');

// Boucle pour créer les lignes
for (var i = 0; i < 255; i++) {
var row = document.createElement('tr');

// Boucle pour créer les cellules dans chaque ligne
for (var j = 0; j < 255; j++) {
var cell = document.createElement('td');

// Calculer une couleur du spectre complet en fonction de la position horizontale
var hue = (i + j) % 360;
cell.style.backgroundColor = 'hsl(' + hue + ', 100%, 50%)';

row.appendChild(cell);
}

// Ajouter la ligne au tableau
table.appendChild(row);
}

// Ajouter le tableau à la page
document.body.appendChild(table);
});
</script>

</body>
</html>
Loading

0 comments on commit cb591f9

Please sign in to comment.