From 67602dc3086bb48cae78b618a2791898cc6c6c63 Mon Sep 17 00:00:00 2001 From: yanirmr Date: Sun, 26 Mar 2023 14:29:20 +0300 Subject: [PATCH 1/3] KAD (U+0857) to KD and deal with broken hyperlinks --- content.js | 68 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/content.js b/content.js index 8a78ca6..7fb8c6b 100644 --- a/content.js +++ b/content.js @@ -1,31 +1,48 @@ function replaceMandaicCharacters() { - const mandaicLetters = "\u{0840}\u{0841}\u{0842}\u{0843}\u{0844}\u{0845}\u{0846}\u{0847}\u{0848}\u{0849}\u{084A}\u{084B}\u{084C}\u{084D}\u{084E}\u{084F}\u{0850}\u{0851}\u{0852}\u{0853}\u{0854}\u{0855}"; + const mandaicLetters = + "\u{0840}\u{0841}\u{0842}\u{0843}\u{0844}\u{0845}\u{0846}\u{0847}\u{0848}\u{0849}\u{084A}\u{084B}\u{084C}\u{084D}\u{084E}\u{084F}\u{0850}\u{0851}\u{0852}\u{0853}\u{0854}\u{0855}"; const hebrewLetters = "אבגדחוזהטיכלמנסעפצקרשת"; - const targetTags = ["b", "td"]; + const targetTags = ["b", "a"]; // Replace Mandaic characters in specific HTML tags for (const tag of targetTags) { const elements = document.getElementsByTagName(tag); for (const element of elements) { const originalText = element.textContent; - const regex = new RegExp(`[${mandaicLetters}\u{0856}\u{0857}]`, "g"); - const newText = originalText.replace(regex, (match) => { - if (match === '\u{0856}') { - return '\u{05D3}\u{05B7}'; + if (tag === "a") { + const originalText = element.childNodes[0]?.nodeValue || ""; + const regex = new RegExp(`[${mandaicLetters}\u{0856}\u{0857}]`, "g"); + const newText = originalText.replace(regex, (match) => { + if (match === "\u{0856}") { + return "\u{05D3}\u{05B7}"; + } else if (match === "\u{0857}") { + return "\u{05DB}\u{05D9}"; + } + const index = mandaicLetters.indexOf(match); + return hebrewLetters.charAt(index); + }); + if (newText !== originalText) { + element.childNodes[0].nodeValue = newText; } - else if (match === '\u{0857}'){ - return '\u{05DB}\u{05D9}'; - } - const index = mandaicLetters.indexOf(match); - return hebrewLetters.charAt(index); - }); - // const regex = new RegExp(`[${mandaicLetters}]`, "g"); - // const newText = originalText.replace(regex, (match) => { - // const index = mandaicLetters.indexOf(match); + } else { + const regex = new RegExp(`[${mandaicLetters}\u{0856}\u{0857}]`, "g"); + const newText = originalText.replace(regex, (match) => { + if (match === "\u{0856}") { + return "\u{05D3}\u{05B7}"; + } else if (match === "\u{0857}") { + return "\u{05DB}\u{05D9}"; + } + const index = mandaicLetters.indexOf(match); + return hebrewLetters.charAt(index); + }); + // const regex = new RegExp(`[${mandaicLetters}]`, "g"); + // const newText = originalText.replace(regex, (match) => { + // const index = mandaicLetters.indexOf(match); //return hebrewLetters.charAt(index); - // }); - if (newText !== originalText) { - element.textContent = newText; + // }); + if (newText !== originalText) { + element.textContent = newText; + } } } } @@ -41,10 +58,15 @@ function replaceMandaicCharacters() { for (let i = 0; i < textNodes.snapshotLength; i++) { const textNode = textNodes.snapshotItem(i); const originalText = textNode.textContent; - const regex = new RegExp(`[${mandaicLetters}]`, "g"); - const newText = originalText.replace(regex, (match) => { - const index = mandaicLetters.indexOf(match); - return hebrewLetters.charAt(index); + const regex = new RegExp(`[${mandaicLetters}\u{0856}\u{0857}]`, "g"); + const newText = originalText.replace(regex, (match) => { + if (match === "\u{0856}") { + return "\u{05D3}\u{05B7}"; + } else if (match === "\u{0857}") { + return "\u{05DB}\u{05D3}"; + } + const index = mandaicLetters.indexOf(match); + return hebrewLetters.charAt(index); }); if (newText !== originalText) { textNode.textContent = newText; @@ -54,4 +76,4 @@ function replaceMandaicCharacters() { // Call replaceMandaicCharacters() whenever the DOM is updated const observer = new MutationObserver(replaceMandaicCharacters); -observer.observe(document, { subtree: true, childList: true }); \ No newline at end of file +observer.observe(document, { subtree: true, childList: true }); From a90bb4da292b49a9b39a5db8443fe8c249065d12 Mon Sep 17 00:00:00 2001 From: yanirmr Date: Sun, 26 Mar 2023 15:10:18 +0300 Subject: [PATCH 2/3] attempt to deal with internal link --- content.js | 75 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 15 deletions(-) diff --git a/content.js b/content.js index 7fb8c6b..8a40bff 100644 --- a/content.js +++ b/content.js @@ -1,4 +1,4 @@ -function replaceMandaicCharacters() { +function replaceMandaicCharacters(contentDocument) { const mandaicLetters = "\u{0840}\u{0841}\u{0842}\u{0843}\u{0844}\u{0845}\u{0846}\u{0847}\u{0848}\u{0849}\u{084A}\u{084B}\u{084C}\u{084D}\u{084E}\u{084F}\u{0850}\u{0851}\u{0852}\u{0853}\u{0854}\u{0855}"; const hebrewLetters = "אבגדחוזהטיכלמנסעפצקרשת"; @@ -6,7 +6,7 @@ function replaceMandaicCharacters() { // Replace Mandaic characters in specific HTML tags for (const tag of targetTags) { - const elements = document.getElementsByTagName(tag); + const elements = contentDocument.getElementsByTagName(tag); for (const element of elements) { const originalText = element.textContent; if (tag === "a") { @@ -48,9 +48,9 @@ function replaceMandaicCharacters() { } // Replace Mandaic characters in all text nodes on the webpage - const textNodes = document.evaluate( + const textNodes = contentDocument.evaluate( "//text()", - document, + contentDocument, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null @@ -59,14 +59,14 @@ function replaceMandaicCharacters() { const textNode = textNodes.snapshotItem(i); const originalText = textNode.textContent; const regex = new RegExp(`[${mandaicLetters}\u{0856}\u{0857}]`, "g"); - const newText = originalText.replace(regex, (match) => { - if (match === "\u{0856}") { - return "\u{05D3}\u{05B7}"; - } else if (match === "\u{0857}") { - return "\u{05DB}\u{05D3}"; - } - const index = mandaicLetters.indexOf(match); - return hebrewLetters.charAt(index); + const newText = originalText.replace(regex, (match) => { + if (match === "\u{0856}") { + return "\u{05D3}\u{05B7}"; + } else if (match === "\u{0857}") { + return "\u{05DB}\u{05D3}"; + } + const index = mandaicLetters.indexOf(match); + return hebrewLetters.charAt(index); }); if (newText !== originalText) { textNode.textContent = newText; @@ -74,6 +74,51 @@ function replaceMandaicCharacters() { } } -// Call replaceMandaicCharacters() whenever the DOM is updated -const observer = new MutationObserver(replaceMandaicCharacters); -observer.observe(document, { subtree: true, childList: true }); +(function () { + replaceMandaicCharacters(document); +})(); + + +const observer = new MutationObserver((mutations) => { + for (const mutation of mutations) { + if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { + replaceMandaicCharacters(document); + } + } +}); + +observer.observe(document, { + childList: true, + subtree: true, +}); + + +let lastContentHash = ''; + +function checkForContentChanges() { + const iframe = document.querySelector('iframe'); + if (!iframe || !iframe.contentDocument) { + return; + } + + const currentContent = iframe.contentDocument.body.innerHTML; + const currentHash = hashString(currentContent); + + if (currentHash !== lastContentHash) { + replaceMandaicCharacters(iframe.contentDocument); + lastContentHash = currentHash; + } + +} + +function hashString(str) { + let hash = 0; + for (let i = 0; i < str.length; i++) { + const charCode = str.charCodeAt(i); + hash = (hash << 5) - hash + charCode; + hash |= 0; // Convert to 32-bit integer + } + return hash; +} + +setInterval(checkForContentChanges, 1000); // Check for changes every second From 3cc03239024b26b1ebd8cbafb58173ed1b3ab18f Mon Sep 17 00:00:00 2001 From: yanirmr Date: Sun, 26 Mar 2023 15:43:33 +0300 Subject: [PATCH 3/3] delete redundant code --- content.js | 81 ++++++++++++++++++++---------------------------------- 1 file changed, 30 insertions(+), 51 deletions(-) diff --git a/content.js b/content.js index 8a40bff..6a6d4e8 100644 --- a/content.js +++ b/content.js @@ -4,42 +4,31 @@ function replaceMandaicCharacters(contentDocument) { const hebrewLetters = "אבגדחוזהטיכלמנסעפצקרשת"; const targetTags = ["b", "a"]; - // Replace Mandaic characters in specific HTML tags + const replaceText = (originalText) => { + const regex = new RegExp(`[${mandaicLetters}\u{0856}\u{0857}]`, "g"); + return originalText.replace(regex, (match) => { + if (match === "\u{0856}") { + return "\u{05D3}\u{05B7}"; + } else if (match === "\u{0857}") { + return "\u{05DB}\u{05D9}"; + } + const index = mandaicLetters.indexOf(match); + return hebrewLetters.charAt(index); + }); + }; + for (const tag of targetTags) { const elements = contentDocument.getElementsByTagName(tag); for (const element of elements) { - const originalText = element.textContent; if (tag === "a") { const originalText = element.childNodes[0]?.nodeValue || ""; - const regex = new RegExp(`[${mandaicLetters}\u{0856}\u{0857}]`, "g"); - const newText = originalText.replace(regex, (match) => { - if (match === "\u{0856}") { - return "\u{05D3}\u{05B7}"; - } else if (match === "\u{0857}") { - return "\u{05DB}\u{05D9}"; - } - const index = mandaicLetters.indexOf(match); - return hebrewLetters.charAt(index); - }); + const newText = replaceText(originalText); if (newText !== originalText) { element.childNodes[0].nodeValue = newText; } } else { - const regex = new RegExp(`[${mandaicLetters}\u{0856}\u{0857}]`, "g"); - const newText = originalText.replace(regex, (match) => { - if (match === "\u{0856}") { - return "\u{05D3}\u{05B7}"; - } else if (match === "\u{0857}") { - return "\u{05DB}\u{05D9}"; - } - const index = mandaicLetters.indexOf(match); - return hebrewLetters.charAt(index); - }); - // const regex = new RegExp(`[${mandaicLetters}]`, "g"); - // const newText = originalText.replace(regex, (match) => { - // const index = mandaicLetters.indexOf(match); - //return hebrewLetters.charAt(index); - // }); + const originalText = element.textContent; + const newText = replaceText(originalText); if (newText !== originalText) { element.textContent = newText; } @@ -47,7 +36,6 @@ function replaceMandaicCharacters(contentDocument) { } } - // Replace Mandaic characters in all text nodes on the webpage const textNodes = contentDocument.evaluate( "//text()", contentDocument, @@ -58,45 +46,36 @@ function replaceMandaicCharacters(contentDocument) { for (let i = 0; i < textNodes.snapshotLength; i++) { const textNode = textNodes.snapshotItem(i); const originalText = textNode.textContent; - const regex = new RegExp(`[${mandaicLetters}\u{0856}\u{0857}]`, "g"); - const newText = originalText.replace(regex, (match) => { - if (match === "\u{0856}") { - return "\u{05D3}\u{05B7}"; - } else if (match === "\u{0857}") { - return "\u{05DB}\u{05D3}"; - } - const index = mandaicLetters.indexOf(match); - return hebrewLetters.charAt(index); - }); + const newText = replaceText(originalText); if (newText !== originalText) { textNode.textContent = newText; } } } + (function () { replaceMandaicCharacters(document); + addLinkEventListeners(); })(); -const observer = new MutationObserver((mutations) => { - for (const mutation of mutations) { - if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { - replaceMandaicCharacters(document); - } +function addLinkEventListeners() { + const links = document.querySelectorAll("a"); + for (const link of links) { + link.addEventListener("click", () => { + setTimeout(() => { + replaceMandaicCharacters(document); + }, 500); + }); } -}); - -observer.observe(document, { - childList: true, - subtree: true, -}); +} let lastContentHash = ''; function checkForContentChanges() { - const iframe = document.querySelector('iframe'); + const iframe = document.querySelector("iframe"); if (!iframe || !iframe.contentDocument) { return; } @@ -106,9 +85,9 @@ function checkForContentChanges() { if (currentHash !== lastContentHash) { replaceMandaicCharacters(iframe.contentDocument); + addLinkEventListeners(); // lastContentHash = currentHash; } - } function hashString(str) {