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,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')
|
||||
})
|
||||
});
|
@ -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",
|
||||
|
||||
|
@ -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
|
||||
});
|
Loading…
Reference in New Issue
Block a user