Handling events differently.

Now, there will be no more "false positives".
The click listener will only be on youtube links.
This commit is contained in:
aminecmi 2022-07-16 15:43:32 +02:00
parent 980d60eb44
commit 75fed20b17
3 changed files with 74 additions and 23 deletions

View File

@ -27,23 +27,23 @@ function cleanToken(token) {
// Links click event handling // Links click event handling
browser.runtime.onMessage.addListener(function(message) { browser.runtime.onMessage.addListener(function(message) {
let url = message.youtubeUrl; let url = message.url;
const name = message.targetName; const name = message.targetName;
browser.storage.sync.get("url").then(function(item) { browser.storage.sync.get("url").then(function(item) {
const subst = item.url + `/watch?v=`; const subst = item.url + `/watch?v=`;
let token = getVideoToken(url) let token = getVideoToken(url)
const cleanedToken = cleanToken(token) const cleanedToken = cleanToken(token)
url = url.replace(/.*\/watch\?v=/gm, subst); url = url.replace(/.*\/watch\?v=/gm, subst);
if (name != "_self") { if (name != "_self") {
browser.tabs.create( browser.tabs.create(
{active: true, url: url} {active: true, url: url}
); );
} else { } else {
browser.tabs.update( browser.tabs.update(
{url: url, loadReplace: true} {url: url, loadReplace: false}
) )
} }
console.log('Youtube to invidious is redirecting you to invidious') console.log('Youtube to invidious is redirecting you to invidious')
}) })
}); });

View File

@ -2,7 +2,7 @@
"manifest_version": 2, "manifest_version": 2,
"name": "Youtube to invidious", "name": "Youtube to invidious",
"version": "0.2", "version": "0.3",
"description": "Change every youtube link to an invidious one", "description": "Change every youtube link to an invidious one",

View File

@ -1,11 +1,62 @@
window.addEventListener("click", (e) => { function findATagsAndAddListener(el) {
var target = e.target; let tags = [];
while ((target.tagName != "A" || !target.href) && target.parentNode) {
target = target.parentNode;
}
if (target.tagName != "A" || target.href.indexOf('youtube') == -1)
return;
e.preventDefault(); // Google has elements that prevent click on links. May need different handling
browser.runtime.sendMessage({"youtubeUrl": target.href, "targetName": (target.attributes.target != null ? target.attributes.target.nodeValue : '_self')}); // 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
}); });