From ac139f6bcaf49017f289018e748cf24fb8b3ec7e Mon Sep 17 00:00:00 2001 From: clerie Date: Fri, 29 Jun 2018 12:16:09 +0200 Subject: [PATCH] =?UTF-8?q?Light=20Modus=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.json | 10 ++++++++-- settings/index.html | 23 +++++++++++++++++++++++ settings/settings.js | 23 +++++++++++++++++++++++ witzig.js | 42 ++++++++++++++++++++++++++++-------------- 4 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 settings/index.html create mode 100644 settings/settings.js diff --git a/manifest.json b/manifest.json index eda758b..73608b0 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "Witzig! - Nicht Witzig!", - "version": "1.1", + "version": "1.2", "description": "Replaces the thumb up and down in YouTube with a Witzig! and a Nicht Witzig! stamp.", "homepage_url": "https://github.com/clerie/witzig-nicht-witzig/", @@ -24,6 +24,12 @@ "web_accessible_resources": [ "media/*" - ] + ], + + "options_ui": { + "page": "settings/index.html" + }, + + "permissions": ["storage"] } diff --git a/settings/index.html b/settings/index.html new file mode 100644 index 0000000..1380d7f --- /dev/null +++ b/settings/index.html @@ -0,0 +1,23 @@ + + + + + + +
+

Ressourcen

+ +
+
+ +
+ + + + + + diff --git a/settings/settings.js b/settings/settings.js new file mode 100644 index 0000000..9ec1c17 --- /dev/null +++ b/settings/settings.js @@ -0,0 +1,23 @@ +function saveOptions(e) { + e.preventDefault(); + browser.storage.local.set({ + light: document.querySelector("#light").checked + }); +} + +function restoreOptions() { + + function setCurrentChoice(result) { + document.querySelector("#light").checked = result.light || false; + } + + function onError(error) { + console.log(`Error: ${error}`); + } + + var getting = browser.storage.local.get("light"); + getting.then(setCurrentChoice, onError); +} + +document.addEventListener("DOMContentLoaded", restoreOptions); +document.querySelector("form").addEventListener("submit", saveOptions); diff --git a/witzig.js b/witzig.js index 0b6a100..19b89a6 100644 --- a/witzig.js +++ b/witzig.js @@ -27,6 +27,10 @@ function getURL(path) { return chrome.runtime.getURL(path) || browser.runtime.getURL(path); } +function onError(error) { + log(`Error: ${error}`); +} + /* Grafikfunktionen */ @@ -50,7 +54,7 @@ Hauptfunktion */ // Ersetzt die Daumen durch Stempel -function inject() { +function inject(settings) { var container_object = document.querySelectorAll(container_path); resize_container(container_object[0], "100px"); resize_container(container_object[1], "100px"); @@ -59,7 +63,7 @@ function inject() { log("Packe Stempel aus..."); // wenn Like aktiv, zeige aktive Grafik an - if(container_object[0].classList.contains('style-default-active')) { + if(container_object[0].classList.contains('style-default-active') || settings.light) { inject_image(image_container_object[0], getURL("media/stempel-witzig.png")); } else { @@ -68,7 +72,7 @@ function inject() { log("Witzig!"); // wenn Dislike aktiv, zeige aktive Grafik an - if(container_object[1].classList.contains('style-default-active')) { + if(container_object[1].classList.contains('style-default-active') || settings.light) { inject_image(image_container_object[1], getURL("media/stempel-nicht-witzig.png")); } else { @@ -79,7 +83,7 @@ function inject() { // Warte noch kurz vor dem einfügen function inject_with_timeout() { - setTimeout(inject, 10); + setTimeout(function () {inject({light: false})}, 10); } /* @@ -96,7 +100,7 @@ function inject_loop () { current_url = window.location.href; if(current_url != last_loaded_url) { log("Neues"); - setTimeout(inject, 500); + setTimeout(function () {inject({light: false})}, 500); last_loaded_url = current_url; } @@ -106,23 +110,33 @@ function inject_loop () { // Die YouTube Website läd die Inhalte durch ein externes JavaScript, wenn wir von vornherein unsere eigenen Inhalte einsetzen, werden diese später von YouTube wieder überschrieben. Wir schauen also, ob ein bestimmtes Objekt, dass durch JavaScript eingefügt wird, exstiert, dann erst bringen wir unsere eigenen Inhalte ein. var loaded = false; -function loading() { +function loading(settings) { loaded = document.querySelector(load_end_check_path); if (loaded) { log("Stempelfläche gefunden."); - // initialisiere onclick events - link_object = document.querySelectorAll(link_path); - link_object[0].onclick = inject_with_timeout; - link_object[1].onclick = inject_with_timeout; - // starte Schleife, die nach aktualisierungen ausschau hält - inject_loop(); + if(!settings.light) { + // initialisiere onclick events + link_object = document.querySelectorAll(link_path); + link_object[0].onclick = inject_with_timeout; + link_object[1].onclick = inject_with_timeout; + + // starte Schleife, die nach aktualisierungen ausschau hält + inject_loop(); + } + else { + inject(settings); + } + + } else { - setTimeout(loading, 500); + setTimeout(function () {loading(settings)}, 500); } } // Starte das Add-on log("Suche Stempelfläche..."); -loading(); +// lade Einstellungen +var getting = browser.storage.local.get("light"); +getting.then(loading, onError);