Page action and quicker.

This commit is contained in:
aminecmi 2022-07-16 21:20:30 +02:00
parent 75fed20b17
commit c2b9015309
5 changed files with 62 additions and 35 deletions

View File

@ -1,3 +1,5 @@
let urlFromStorage
// Storage init // Storage init
browser.runtime.onInstalled.addListener(function() { browser.runtime.onInstalled.addListener(function() {
browser.storage.sync.set({ browser.storage.sync.set({
@ -5,6 +7,12 @@ browser.runtime.onInstalled.addListener(function() {
}); });
}); });
browser.storage.sync.onChanged.addListener(function(changes) {
if (changes.url && changes.url.newValue) {
urlFromStorage = changes.url.newValue
}
})
// Url format // Url format
function getVideoToken(url) { function getVideoToken(url) {
@ -25,25 +33,35 @@ function cleanToken(token) {
} }
// Links click event handling
browser.runtime.onMessage.addListener(function(message) { function toInvidious(message) {
let url = message.url; let url = message.url;
const name = message.targetName; const name = message.targetName;
browser.storage.sync.get("url").then(function(item) { const subst = urlFromStorage + `/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: false} {url: url, loadReplace: false}
) )
} }
console.log('Youtube to invidious is redirecting you to invidious') console.log('Youtube to invidious is redirecting you to invidious')
}) }
// Links click event handling
browser.runtime.onMessage.addListener(function(message) {
toInvidious(message)
});
// Page action
browser.pageAction.onClicked.addListener(function(page) {
toInvidious({url: page.url, targetName: "_self"});
}); });

BIN
button/19.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
button/38.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@ -2,13 +2,22 @@
"manifest_version": 2, "manifest_version": 2,
"name": "Youtube to invidious", "name": "Youtube to invidious",
"version": "0.3", "version": "0.4",
"description": "Change every youtube link to an invidious one", "description": "Change every youtube link to an invidious one",
"icons": { "icons": {
"48": "icons/48.png" "48": "icons/48.png"
}, },
"page_action": {
"show_matches": ["https://*.youtube.com/watch*"],
"browser_style": true,
"default_icon": {
"19": "button/19.png",
"38": "button/38.png"
}
},
"web_accessible_resources": ["icons/48.png"], "web_accessible_resources": ["icons/48.png"],
"content_scripts": [ "content_scripts": [
{ {

View File

@ -2,14 +2,14 @@ function findATagsAndAddListener(el) {
let tags = []; let tags = [];
// Google has elements that prevent click on links. May need different handling // Google has elements that prevent click on links. May need different handling
// if (window.location.href.indexOf("google.") > 0) { // if (window.location.href.indexOf("google.") >= 0) {
// const elements = document.getElementsByClassName("NqpkQc"); // const elements = document.getElementsByClassName("NqpkQc");
// while(elements.length > 0){ // while(elements.length > 0){
// elements[0].parentNode.removeChild(elements[0]); // elements[0].parentNode.removeChild(elements[0]);
// } // }
// } // }
if (window.location.href.indexOf('youtube.com') > 0) { if (window.location.href.indexOf('youtube.com') >= 0) {
tags = el.querySelectorAll('a[href*="/watch"]'); tags = el.querySelectorAll('a[href*="/watch"]');
} else { } else {
tags = el.querySelectorAll('a[href*="youtube.com/"]'); tags = el.querySelectorAll('a[href*="youtube.com/"]');
@ -22,22 +22,22 @@ function findATagsAndAddListener(el) {
function addListenerToATag(tag) { function addListenerToATag(tag) {
tag.addEventListener('click', function(e) { tag.addEventListener('click', function(e) {
e.preventDefault(); e.preventDefault();
e.stopImmediatePropagation(); e.stopImmediatePropagation();
// Find the a tag // 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;
} }
let targetName = (target.attributes.target != null ? target.attributes.target.nodeValue : '_self') let targetName = (target.attributes.target != null ? target.attributes.target.nodeValue : '_self')
if (e.ctrlKey || e.metaKey) { if (e.ctrlKey || e.metaKey) {
targetName = "_blank"; targetName = "_blank";
} }
browser.runtime.sendMessage({"url": target.href, "targetName": targetName}); browser.runtime.sendMessage({"url": target.href, "targetName": targetName});
}) })
} }
// Add the event listeners to the current elements of the body // Add the event listeners to the current elements of the body