From 75fed20b17e4bec780be4e3661e971fc4f04050e Mon Sep 17 00:00:00 2001 From: aminecmi Date: Sat, 16 Jul 2022 15:43:32 +0200 Subject: [PATCH] Handling events differently. Now, there will be no more "false positives". The click listener will only be on youtube links. --- background.js | 26 +++++++++---------- manifest.json | 2 +- to-invidious.js | 69 ++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 74 insertions(+), 23 deletions(-) diff --git a/background.js b/background.js index d5bc742..7e5d276 100644 --- a/background.js +++ b/background.js @@ -27,23 +27,23 @@ function cleanToken(token) { // Links click event handling browser.runtime.onMessage.addListener(function(message) { - let url = message.youtubeUrl; + let url = message.url; const name = message.targetName; browser.storage.sync.get("url").then(function(item) { const subst = item.url + `/watch?v=`; let token = getVideoToken(url) - const cleanedToken = cleanToken(token) - url = url.replace(/.*\/watch\?v=/gm, subst); + const cleanedToken = cleanToken(token) + url = url.replace(/.*\/watch\?v=/gm, subst); - if (name != "_self") { - browser.tabs.create( - {active: true, url: url} - ); - } else { - browser.tabs.update( - {url: url, loadReplace: true} - ) - } - console.log('Youtube to invidious is redirecting you to invidious') + if (name != "_self") { + browser.tabs.create( + {active: true, url: url} + ); + } else { + browser.tabs.update( + {url: url, loadReplace: false} + ) + } + console.log('Youtube to invidious is redirecting you to invidious') }) }); \ No newline at end of file diff --git a/manifest.json b/manifest.json index 691d48f..0bf8391 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "Youtube to invidious", - "version": "0.2", + "version": "0.3", "description": "Change every youtube link to an invidious one", diff --git a/to-invidious.js b/to-invidious.js index a6f01ce..e9edb9c 100644 --- a/to-invidious.js +++ b/to-invidious.js @@ -1,11 +1,62 @@ -window.addEventListener("click", (e) => { - var target = e.target; - while ((target.tagName != "A" || !target.href) && target.parentNode) { - target = target.parentNode; - } - if (target.tagName != "A" || target.href.indexOf('youtube') == -1) - return; +function findATagsAndAddListener(el) { + let tags = []; - e.preventDefault(); - browser.runtime.sendMessage({"youtubeUrl": target.href, "targetName": (target.attributes.target != null ? target.attributes.target.nodeValue : '_self')}); + // Google has elements that prevent click on links. May need different handling + // if (window.location.href.indexOf("google.") > 0) { + // const elements = document.getElementsByClassName("NqpkQc"); + // while(elements.length > 0){ + // elements[0].parentNode.removeChild(elements[0]); + // } + // } + + if (window.location.href.indexOf('youtube.com') > 0) { + tags = el.querySelectorAll('a[href*="/watch"]'); + } else { + tags = el.querySelectorAll('a[href*="youtube.com/"]'); + } + + for (i = 0; i < tags.length; i++) { + addListenerToATag(tags[i]); + } +} + +function addListenerToATag(tag) { + tag.addEventListener('click', function(e) { + e.preventDefault(); + e.stopImmediatePropagation(); + + // Find the a tag + var target = e.target; + while ((target.tagName != "A" || !target.href) && target.parentNode) { + target = target.parentNode; + } + + let targetName = (target.attributes.target != null ? target.attributes.target.nodeValue : '_self') + if (e.ctrlKey || e.metaKey) { + targetName = "_blank"; + } + + browser.runtime.sendMessage({"url": target.href, "targetName": targetName}); + }) +} + +// Add the event listeners to the current elements of the body +findATagsAndAddListener(document.body); + +// Monitor the DOM for additions and create event listeners on the go +const observer = new MutationObserver((mutations) => { + mutations.forEach((mutation) => { + if (mutation.addedNodes && mutation.addedNodes.length > 0) { + for (let i = 0; i < mutation.addedNodes.length; i++) { + const newNode = mutation.addedNodes[i]; + if (newNode.nodeType === Node.ELEMENT_NODE || newNode.nodeType === Node.DOCUMENT_NODE || newNode.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { + findATagsAndAddListener(newNode); + } + } + } + }); +}); +observer.observe(document.body, { + childList: true, + subtree: true }); \ No newline at end of file