Skip to content

Commit

Permalink
Merge pull request #15 from yanirmr/fix_hyper_links_breaks
Browse files Browse the repository at this point in the history
Support CAL dictionary links and minor bug
  • Loading branch information
yanirmr authored Mar 26, 2023
2 parents c1c2c76 + 3cc0323 commit 4dd2d09
Showing 1 changed file with 80 additions and 34 deletions.
114 changes: 80 additions & 34 deletions content.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,103 @@
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}";
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 = "אבגדחוזהטיכלמנסעפצקרשת";
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}';
}
else if (match === '\u{0857}'){
return '\u{05DB}\u{05D9}';
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);
});
// 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;
});
};

for (const tag of targetTags) {
const elements = contentDocument.getElementsByTagName(tag);
for (const element of elements) {
if (tag === "a") {
const originalText = element.childNodes[0]?.nodeValue || "";
const newText = replaceText(originalText);
if (newText !== originalText) {
element.childNodes[0].nodeValue = newText;
}
} else {
const originalText = element.textContent;
const newText = replaceText(originalText);
if (newText !== originalText) {
element.textContent = newText;
}
}
}
}

// 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
);
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 newText = replaceText(originalText);
if (newText !== originalText) {
textNode.textContent = newText;
}
}
}

// Call replaceMandaicCharacters() whenever the DOM is updated
const observer = new MutationObserver(replaceMandaicCharacters);
observer.observe(document, { subtree: true, childList: true });

(function () {
replaceMandaicCharacters(document);
addLinkEventListeners();
})();


function addLinkEventListeners() {
const links = document.querySelectorAll("a");
for (const link of links) {
link.addEventListener("click", () => {
setTimeout(() => {
replaceMandaicCharacters(document);
}, 500);
});
}
}


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);
addLinkEventListeners(); //
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

0 comments on commit 4dd2d09

Please sign in to comment.