Skip to content
Snippets Groups Projects
Commit 84495c16 authored by David Byers's avatar David Byers
Browse files

Refactored. Compose support in Firefox.

parent ddc0c70f
No related branches found
No related tags found
1 merge request!8Beta
Pipeline #33052 passed
...@@ -31,6 +31,5 @@ function addLinkPopup(link) { ...@@ -31,6 +31,5 @@ function addLinkPopup(link) {
link.addEventListener('mouseleave', scheduleHidePopup, {passive: true}); link.addEventListener('mouseleave', scheduleHidePopup, {passive: true});
} }
mutationHandler();
fixAllTheLinks();
enableMutationObserver(); enableMutationObserver();
...@@ -25,30 +25,42 @@ ...@@ -25,30 +25,42 @@
let mutationObserver = null; let mutationObserver = null;
/**
* Handle mutation events. Attempts to detect a composition pane and
* remove nasty links from it.
*/
function mutationHandler(mutationsList, observer) {
let contentNode = (document.getElementById('divRplyFwdMsg')
|| document.getElementById('x_divRplyFwdMsg'))?.nextElementSibling;
while (contentNode) {
removeAllTheLinks(contentNode);
contentNode = contentNode.nextElementSibling;
}
fixAllTheLinks(document.body);
}
/** /**
* Enable the mutation observer, creating it is necessary. * Enable the mutation observer, creating it is necessary.
*/ */
function enableMutationObserver() { function enableMutationObserver() {
console.log('enter enableMutationObserver');
if (!mutationObserver) { if (!mutationObserver) {
mutationObserver = new MutationObserver(withoutMutationObserver(fixAllTheLinks)); mutationObserver = new MutationObserver(withoutMutationObserver(mutationHandler));
} }
mutationObserver.observe(document.body, { mutationObserver.observe(document.body, {
childList: true, childList: true,
subtree: true subtree: true
}); });
console.log('exit enableMutationObserver');
} }
/** /**
* Disable the mutation observer if it is enabled * Disable the mutation observer if it is enabled
*/ */
function disableMutationObserver() { function disableMutationObserver() {
console.log('enter disableMutationObserver');
if (mutationObserver) { if (mutationObserver) {
mutationObserver.disconnect(); mutationObserver.disconnect();
} }
console.log('exit disableMutationObserver');
} }
/** /**
......
...@@ -56,7 +56,6 @@ function untangleLink(link) { ...@@ -56,7 +56,6 @@ function untangleLink(link) {
return decodeURIComponent(url); return decodeURIComponent(url);
} }
catch (e) { catch (e) {
console.log(e);
return url; return url;
} }
}); });
...@@ -101,17 +100,36 @@ function getTextNodes(elem) { ...@@ -101,17 +100,36 @@ function getTextNodes(elem) {
/** /**
* Fix all the links in the document. * Fix all the links in the document.
* @param {Element} root - DOM element in which to fix links.
*/ */
function fixAllTheLinks() { function fixAllTheLinks(root) {
for (const link of document.links) { for (const link of root.getElementsByTagName('a')) {
// Untangle link text if (link.href) {
for (const node of getTextNodes(link)) { // Untangle link text
node.textContent = untangleLink(node.textContent); for (const node of getTextNodes(link)) {
node.textContent = untangleLink(node.textContent);
}
// Create popup event handlers
if (isTangledLink(link.href)) {
addLinkPopup(link);
}
} }
}
}
// Create popup event handlers
/**
* Remove all safe links in an element
* @param {Element} root - DOM element in which to fix links.
*/
function removeAllTheLinks(root) {
for (const link of root.getElementsByTagName('a')) {
if (isTangledLink(link.href)) { if (isTangledLink(link.href)) {
addLinkPopup(link); link.href = untangleLink(link.href);
} }
} }
for (const textNode of getTextNodes(root)) {
textNode.textContent = untangleLink(textNode.textContent);
}
} }
...@@ -22,13 +22,4 @@ ...@@ -22,13 +22,4 @@
// Compose script // Compose script
for (const link of document.links) { removeAllTheLinks(document.body);
if (isTangledLink(link.href)) {
link.href = untangleLink(link.href);
}
}
for (const node of getTextNodes(document)) {
node.textContent = untangleLink(node.textContent);
}
...@@ -31,5 +31,4 @@ function addLinkPopup(link) { ...@@ -31,5 +31,4 @@ function addLinkPopup(link) {
link.addEventListener('mouseleave', scheduleHidePopup, {passive: true}); link.addEventListener('mouseleave', scheduleHidePopup, {passive: true});
} }
fixAllTheLinks(document.body);
fixAllTheLinks();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment