feat: improve ui and logic

This commit is contained in:
CSF 2024-08-21 09:52:16 +02:00
parent d2723183a4
commit 82d922b04a
6 changed files with 135 additions and 136 deletions

BIN
InviTube.zip Normal file

Binary file not shown.

View file

@ -1,25 +1,24 @@
let tabWhitelist = []; let tabWhitelist = [];
// Einstellungen ziehen, ContentScript registrieren // Einstellungen ziehen, ContentScript registrieren
browser.storage.local.get( browser.storage.local.get({
{ activated: true,
activated: true, instance: "yewtu.be",
instance: "yewtu.be", })
} .then((storage) => {
).then((storage) => { if (storage.activated) {
if (storage.activated) { activateBlocker();
activateBlocker(); } else {
} else { deactivateBlocker();
deactivateBlocker(); }
}
registerCS(storage.instance); registerCS(storage.instance);
}); });
// ContentScript für aktuelle Instanz registrieren // ContentScript für aktuelle Instanz registrieren
async function registerCS(url) { function registerCS(url) {
return await browser.contentScripts.register({ browser.contentScripts.register({
js: [{ file: "content.js" }], js: [{ file: "content.js" }],
matches: ["*://" + url + "/*"] matches: ["*://" + url + "/*"]
}); });
@ -27,7 +26,7 @@ async function registerCS(url) {
// pageAction anzeigen // pageAction anzeigen
browser.runtime.onMessage.addListener((data, sender) => { browser.runtime.onMessage.addListener((data, sender) => {
if (data.type == "showYoutubePageAction") { if (data === "showYoutubePageAction") {
browser.pageAction.setIcon({ browser.pageAction.setIcon({
tabId: sender.tab.id, tabId: sender.tab.id,
path: "icons/youtube.png" path: "icons/youtube.png"
@ -36,7 +35,8 @@ browser.runtime.onMessage.addListener((data, sender) => {
tabId: sender.tab.id, tabId: sender.tab.id,
title: "Open on YouTube" title: "Open on YouTube"
}); });
} else if (data.type == "showInvidiousPageAction") {
} else if (data === "showInvidiousPageAction") {
browser.pageAction.setIcon({ browser.pageAction.setIcon({
tabId: sender.tab.id, tabId: sender.tab.id,
path: "icons/invidious.png" path: "icons/invidious.png"
@ -126,25 +126,27 @@ browser.webRequest.onBeforeRequest.addListener(async (details) => {
// URL Icon Click --> Open on YouTube/Invidious // URL Icon Click --> Open on YouTube/Invidious
browser.pageAction.onClicked.addListener((tab) => { browser.pageAction.onClicked.addListener((tab) => {
browser.storage.local.get({ activated: true, instance: "yewtu.be" }).then(async (storage) => { browser.storage.local.get({
if (tab.url.startsWith("https://www.youtube.com")) { activated: true,
browser.tabs.create({ instance: "yewtu.be"
url: "https://" + storage.instance + "/" + tab.url.split("/")[3] })
}); .then(async (storage) => {
} else { if (tab.url.includes("youtube")) {
let newTab = await browser.tabs.create({ browser.tabs.create({
url: "https://www.youtube.com/" + tab.url.split("/")[3] url: "https://" + storage.instance + "/" + tab.url.split("/")[3]
}); });
if (!tabWhitelist.includes(newTab.id)) { } else {
tabWhitelist.push(newTab.id); const newTab = await browser.tabs.create({
url: "https://www.youtube.com/" + tab.url.split("/")[3]
});
if (!tabWhitelist.includes(newTab.id))
tabWhitelist.push(newTab.id);
} }
} });
});
}); });
// Close tab -> Remove from whitelist // Close tab -> Remove from whitelist
browser.tabs.onRemoved.addListener((tabId) => { browser.tabs.onRemoved.addListener((tabId) => {
if (tabWhitelist.includes(tabId)) { if (tabWhitelist.includes(tabId))
tabWhitelist.splice(tabWhitelist.indexOf(tabId), 1); tabWhitelist.splice(tabWhitelist.indexOf(tabId), 1);
}
}); });

View file

@ -1,5 +1,5 @@
if (location.href.startsWith("https://www.youtube.com")) { if (location.href.startsWith("https://www.youtube.com")) {
browser.runtime.sendMessage({ type: "showInvidiousPageAction" }); browser.runtime.sendMessage("showInvidiousPageAction");
} else { } else {
browser.runtime.sendMessage({ type: "showYoutubePageAction" }); browser.runtime.sendMessage("showYoutubePageAction");
} }

View file

@ -2,7 +2,7 @@
"manifest_version": 2, "manifest_version": 2,
"name": "InviTube", "name": "InviTube",
"author": "Indogermane", "author": "Indogermane",
"version": "1.1", "version": "1.2",
"description": "Redirect YouTube to Invidious.", "description": "Redirect YouTube to Invidious.",
"background": { "background": {
"scripts": [ "scripts": [

View file

@ -1,95 +1,86 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head>
<style>
.switch {
position: relative;
display: inline-block;
width: 40px;
height: 23px;
margin: 1.5em 1em;
}
.slider { <head>
position: absolute; <meta charset="utf-8">
border-radius: 34px;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before { <style>
position: absolute; :root {
border-radius: 34px; color-scheme: dark;
content: ""; }
height: 17px;
width: 17px;
left: 4px;
bottom: 3px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider { body {
background-color: #009EE0; background-color: #23222b;
} padding-top: 2em;
padding-left: 5em;
padding-right: 5em;
padding-bottom: 0;
}
input:focus + .slider { input {
box-shadow: 0 0 1px #009EE0; outline: none;
} border: none;
padding: 1em;
}
input:checked + .slider:before { input[type="button"] {
-webkit-transform: translateX(26px); text-align: center;
-ms-transform: translateX(26px); padding: 1em;
transform: translateX(16px); }
}
p { input[type="button"]:hover {
font-size: 1.4em; cursor: pointer;
color: white; }
text-align: center;
}
.textfield { input:focus {
text-align: center; outline: 1px solid #006699;
background-color: #131313; }
color: white;
font-size: 1em;
border: none;
border-bottom: 1px solid #ccc;
outline: none;
margin: 1.5em 1em;
}
#saveBtn { label {
margin: 1.5em 0; margin-left: 1em;
border-radius: 0; margin-bottom: 4px;
font-size: 1em; }
padding: .5em 1em;
border: none;
background-color: #131313;
color: white;
}
</style> .full-width {
<meta charset="utf-8"> width: 100%;
</head> }
<body bgcolor="202023" style="padding: 2em">
<form> .red-border {
<p> border: 1px solid red;
Instance URL: }
<input id="instance" class="textfield" placeholder="piped.kavin.rocks"/>
<br> table input {
<button id="saveBtn" type="submit">Save</button> width: 100%;
</p> }
</form>
<script src="options.js"></script> .mt-2 {
</body> margin-top: 8px;
</html> }
.mt-3 {
margin-top: 12px;
}
.mt-4 {
margin-top: 16px;
}
.mt-12 {
margin-top: 48px;
}
.rounded {
border-radius: 20px;
}
</style>
</head>
<body>
<label for="instance">Instance URL</label>
<input id="instance" placeholder="yewtu.be" class="rounded" />
<input id="saveBtn" type="button" class="rounded mt-2" value="Save" />
<script src="options.js"></script>
</body>
</html>

View file

@ -1,23 +1,29 @@
// Save options
document.querySelector("form").addEventListener("submit", (e) => {
e.preventDefault();
let instance = document.querySelector("#instance").value.trim();
instance = instance.replace(/^https?:?\/?\/?/, '').replace('\/$');
if (!instance) {
return alert("Please fill out the fields!");
}
browser.storage.local.set({
instance: instance,
}).then(() => alert("Saved!"));
});
// Restore options // Restore options
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
browser.storage.local.get({ browser.storage.local.get({
instance: "yewtu.be", instance: "yewtu.be",
}).then((item) => { }).then((storage) => {
document.querySelector("#instance").value = item.instance; document.querySelector("#instance").value = storage.instance;
}); });
}); });
// Save options
const saveBtn = document.getElementById("saveBtn");
saveBtn.addEventListener("click", (e) => {
e.preventDefault();
const instance = document.querySelector("#instance").value.trim().replace(/^https?:\/\/*|\/$/g, '');
if (!instance) return alert("Please fill out the fields!");
browser.storage.local.set({
instance,
}).then(() => {
saveBtn.disabled = true;
saveBtn.value = "Saved!";
setTimeout(() => {
saveBtn.disabled = false;
saveBtn.value = "Save";
}, 3000)
});
});