Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 74 additions & 22 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,75 @@
return replacement;
};

const replaceText = (el) => {
if (el.nodeType === Node.TEXT_NODE) {
if (regex.test(el.textContent)) {
el.textContent = el.textContent.replace(regex, (matched) => {
const replacement = getReplacementText(matched);
if (
replacement !== matched &&
!replacedSet.has(matched.toLowerCase())
) {
replacedWords.push({ original: matched, replacement: replacement });
replacedSet.add(matched.toLowerCase());
}

createTooltip(el, matched);
return replacement;
});
class BloomFilter {
constructor(size, numHashFunctions) {
if (size <= 0 || numHashFunctions <= 0) {
throw new Error("Size and number of hash functions must be positive integers.");
}
} else {
for (let child of el.childNodes) {
replaceText(child);
this.size = size;
this.numHashFunctions = numHashFunctions;
this.bitArray = new Array(size).fill(false);
}

// Improved hash function with better distribution
hash(value, i) {
const hash1 = this.simpleHash(value, i);
const hash2 = this.simpleHash(value.split("").reverse().join(""), i + 1);
return (hash1 + i * hash2) % this.size;
}

// Simple hash function for demonstration
simpleHash(value, salt) {
let hash = 0;
for (let char of value) {
hash = (hash * salt + char.charCodeAt(0)) % this.size;
}
return hash;
}
};

// Add an item to the Bloom filter
add(value) {
for (let i = 0; i < this.numHashFunctions; i++) {
const index = this.hash(value, i);
this.bitArray[index] = true;
}
}

// Check if an item might be in the Bloom filter
contains(value) {
for (let i = 0; i < this.numHashFunctions; i++) {
const index = this.hash(value, i);
if (!this.bitArray[index]) {
return false; // Must be in all positions to return true
}
}
return false
}
}

const replaceText = (el,bloom) => {
if (el.nodeType === Node.TEXT_NODE) {
const words = el.textContent.split(regex);
const updatedText = words
.map((word) => {
const key = word.toLowerCase();

if (!bloom.contains(key)) {
if(textToChange[key]){
createTooltip(el,word)
return textToChange[key]
}
}
return word;
})
.join("");
el.textContent = updatedText;
} else {
for (let child of el.childNodes) {
replaceText(child,bloom);
}
}
};

const anyChildOfBody = "/html/body//";
// const doesNotContainAncestorWithRoleTextbox =
Expand All @@ -134,6 +180,10 @@
if (regex == null || typeof regex === "undefined") {
return;
}
const times = [];
// Initialize the Bloom Filter
const bloom= new BloomFilter(1440,1) // Adjust size and number of hash functions
Object.keys(textToChange || {}).forEach((word) => bloom.add(word));

const result = document.evaluate(
xpathExpression,
Expand All @@ -144,8 +194,10 @@
);
console.log(result);
for (let i = 0; i < result.snapshotLength; i++) {
replaceText(result.snapshotItem(i));
// Call the replaceText function
replaceText(result.snapshotItem(i),bloom);
}

chrome.storage.local.set({
replacedWords: replacedWords,
replacedSet: replacedSet,
Expand Down Expand Up @@ -275,4 +327,4 @@
newElement.style.visibility = "hidden";
});
};
})();
})();