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:
parent
980d60eb44
commit
75fed20b17
@ -27,7 +27,7 @@ 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=`;
|
||||||
@ -41,7 +41,7 @@ browser.runtime.onMessage.addListener(function(message) {
|
|||||||
);
|
);
|
||||||
} 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')
|
||||||
|
@ -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",
|
||||||
|
|
||||||
|
@ -1,11 +1,62 @@
|
|||||||
window.addEventListener("click", (e) => {
|
function findATagsAndAddListener(el) {
|
||||||
|
let tags = [];
|
||||||
|
|
||||||
|
// 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;
|
var target = e.target;
|
||||||
while ((target.tagName != "A" || !target.href) && target.parentNode) {
|
while ((target.tagName != "A" || !target.href) && target.parentNode) {
|
||||||
target = target.parentNode;
|
target = target.parentNode;
|
||||||
}
|
}
|
||||||
if (target.tagName != "A" || target.href.indexOf('youtube') == -1)
|
|
||||||
return;
|
|
||||||
|
|
||||||
e.preventDefault();
|
let targetName = (target.attributes.target != null ? target.attributes.target.nodeValue : '_self')
|
||||||
browser.runtime.sendMessage({"youtubeUrl": target.href, "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
|
||||||
});
|
});
|
Loading…
Reference in New Issue
Block a user