I am using the MutationObserver observer to check for elements as they are added with my program. One condition being checked is if the element added is an anchor element (tag="a") and then check if the link is a DOI then a contextmenu event listener is attached to it.
This is the code:
var doiLinkClicked = function (event) {
currentDOIElement = event.target
let jsonObject =
{
Key: 'doiContextmenu',
Value: currentDOIElement.href
};
window.chrome.webview.postMessage(jsonObject)
}
const config = { attributes: true, childList: true, subtree: true }
const mutationObserverCallback = (mutationList, observer) => {
var word_regexp = /^10\.\d{4,9}\/[-._;()\/:a-zA-Z0-9]+$/
var word_regexp2 = /^https:\/\/doi\.org\/10\.\d{4,9}\/[-._;()\/:a-zA-Z0-9]+$/
var word_regexp3 = /^DOI: 10\.\d{4,9}\/[-._;()\/:a-zA-Z0-9]+$/
var word_regexp4 = /^DOI: https:\/\/doi\.org\/10\.\d{4,9}\/[-._;()\/:a-zA-Z0-9]+$/
for (const mutation of mutationList) {
if (mutation.type === "childList") {
var addedNodes = mutation.addedNodes;
for (var i = 0; i < addedNodes.length; i++) {
var addedNode = addedNodes[i]
var tagName = addedNode.tagName
if (tagName === "A") {
let linkAdress = addedNode.href
if (linkAdress.includes("https://doi.org/10.")) {
console.log(`link: ${linkAdress}`)
console.log("A DOI link was found!")
addedNode.addEventListener("contextmenu", doiLinkClicked)
doiLinksElementsCollection.push(addedNode)
}
}
}
}
}
};
var observer = new MutationObserver(mutationObserverCallback)
observer.observe(this.document, config);
This works perfectly well with all the anchor elements that are linked to DOI's that I have tried except the one on this page. What could be the possible reason for this?
Update: The project is a WPF application that has the WebView2 Microsoft Edge browser interface embedded in it. This code is contained in a javascript file that is injected into documents once they are created. I have added the other codes as requested in the comment. Again, the DOI link on the webpage of interest is detected as the if condition part of the code:
if (tagName === "A") {
let linkAdress = addedNode.href
if (linkAdress.includes("https://doi.org/10.")) {
console.log(`link: ${linkAdress}`)
console.log("A DOI link was found!")
addedNode.addEventListener("contextmenu", doiLinkClicked)
doiLinksElementsCollection.push(addedNode)
}
}
holds true during debugging, and the only thing that doesn't get successful is the event listener being attached.
This error can result due to a lot of reasons: