From b13eff44e322ff3d705b653bc7df8eb03a895f0e Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Fri, 2 Feb 2018 00:21:29 +0100 Subject: [PATCH] Reworked popup warnings. Chrome doesn't work again. --- js/conf/Settings.js | 10 ++++-- js/uw-bg.js | 71 ++++++++++++++++++++++++++++++++++++++++++- js/uw.js | 47 +--------------------------- res/popup/js/popup.js | 32 +++++++++++-------- res/popup/popup.html | 29 +++++++++++++----- 5 files changed, 119 insertions(+), 70 deletions(-) diff --git a/js/conf/Settings.js b/js/conf/Settings.js index 62b166e..16b426e 100644 --- a/js/conf/Settings.js +++ b/js/conf/Settings.js @@ -18,11 +18,17 @@ var _se_init = async function(neverFlushStored){ console.log("[Settings::_se_init()] settings saved in localstorage are:", newSettings, " - if that's empty, it's gonna be replaced by this:", JSON.stringify(this), ")"); if ((Object.keys(newSettings).length === 0 && newSettings.constructor === Object)){ + console.log("[Settings::_se_init()] replacing settings"); StorageManager.setopt({"uw-settings": JSON.stringify(this)}); } else{ - for (var k in newSettings) - this[k] = newSettings[k]; + var actualSettings = JSON.parse(newSettings["uw-settings"]); + + if(Debug.debug) + console.log("[Settings::_se_init()] parsed settings:", actualSettings); + + for (var k in actualSettings) + this[k] = actualSettings[k]; } if(Debug.debug) diff --git a/js/uw-bg.js b/js/uw-bg.js index e86390f..a4eb633 100644 --- a/js/uw-bg.js +++ b/js/uw-bg.js @@ -1,7 +1,22 @@ var BgVars = { arIsActive: true, - hasVideos: false + hasVideos: false, + currentSite: "" +} + +function extractHostname(url){ + // extract hostname + if (url.indexOf("://") > -1) { //find & remove protocol (http, ftp, etc.) and get hostname + hostname = url.split('/')[2]; + } + else { + hostname = url.split('/')[0]; + } + hostname = hostname.split(':')[0]; //find & remove port number + hostname = hostname.split('?')[0]; //find & remove "?" + + return hostname; } async function main(){ @@ -21,6 +36,11 @@ async function main(){ if(Debug.debug) console.log("[uw-bg::main] listeners registered"); + + // add currentSite + var tabs = await Comms.getActiveTab(); + BgVars.currentSite = extractHostname(tabs[0].url); + } async function _uwbg_onTabSwitched(activeInfo){ @@ -28,8 +48,14 @@ async function _uwbg_onTabSwitched(activeInfo){ if(Debug.debug) console.log("[uw-bg::onTabSwitched] TAB CHANGED, GETTING INFO FROM MAIN TAB"); + var tabId = activeInfo.tabId; // just for readability + var tab = await browser.tabs.get(tabId); + + BgVars.currentSite = extractHostname(tab.url); + + // this can fail. This might also not return a promise? Check that. var videoFrameList = await Comms.sendToEach({"cmd":"has-videos"}, tabId); if(Debug.debug) @@ -125,6 +151,49 @@ function _uwbg_rcvmsg(message, sender, sendResponse){ _uwbg_registerVideo(sender.tab.id); } + + else if(message.cmd == "uw-enabled-for-site"){ + var wlindex = Settings.whitelist.indexOf(BgVars.currentSite); + var blindex = Settings.blacklist.indexOf(BgVars.currentSite); + + var mode = "default"; + if(wlindex > -1) + mode = "whitelist"; + if(blindex > -1) + mode = "blacklist"; + + if(Debug.debug){ + console.log("[uw::receiveMessage] is this site: ", BgVars.currentSite, "\n\n", "whitelisted or blacklisted? whitelist:", (wlindex > -1), "; blacklist:", (blindex > -1), "; mode (return value):", mode, "\nwhitelist:",Settings.whitelist,"\nblacklist:",Settings.blacklist); + + } + + if(BrowserDetect.usebrowser == "firefox") + return Promise.resolve({response: mode}); + + try{ + sendResponse({response: mode}); + } + catch(chromeIsShitError){}; + + return true; + } + else if(message.cmd == "enable-for-site"){ + var wlindex = Settings.whitelist.indexOf(BgVars.currentSite); + var blindex = Settings.blacklist.indexOf(BgVars.currentSite); + + if(wlindex > -1) + Settings.whitelist.splice(BgVars.currentSite, 1); + if(blindex > -1) + Settings.blacklist.splice(BgVars.currentSite, 1); + + if(message.option == "whitelist") + Settings.whitelist.push(BgVars.currentSite); + if(message.option == "blacklist") + Settings.blacklist.push(BgVars.currentSite); + + Settings.save(); + } + } diff --git a/js/uw.js b/js/uw.js index a1c8379..83230d7 100644 --- a/js/uw.js +++ b/js/uw.js @@ -94,8 +94,7 @@ var _video_recheck_counter = 0; var _video_recheck_period = 60; // on this many retries function ghettoOnChange(){ -// console.log(".."); -// console.log("events:", $._data($(document)[0], "events")); + if(_video_recheck_counter++ > _video_recheck_period){ _video_recheck_counter = 0; @@ -215,50 +214,6 @@ function receiveMessage(message, sender, sendResponse) { else if(message.cmd == "reload-settings"){ Settings.reload(); } - else if(message.cmd == "uw-enabled-for-site"){ - var site = window.location.hostname; - var wlindex = Settings.whitelist.indexOf(site); - var blindex = Settings.blacklist.indexOf(site); - - var mode = "default"; - if(wlindex > -1) - mode = "whitelist"; - if(blindex > -1) - mode = "blacklist"; - - if(Debug.debug){ - console.log("[uw::receiveMessage] is this site whitelisted or blacklisted? whitelist:", (wlindex > -1), "; blacklist:", (blindex > -1), "; mode (return value):", mode, "\nwhitelist:",Settings.whitelist,"\nblacklist:",Settings.blacklist); - - } - - if(BrowserDetect.usebrowser == "firefox") - return Promise.resolve({response: mode}); - - try{ - sendResponse({response: mode}); - } - catch(chromeIsShitError){}; - - return true; - } - else if(message.cmd == "enable-for-site"){ - var site = window.location.hostname; - var wlindex = Settings.whitelist.indexOf(site); - var blindex = Settings.blacklist.indexOf(site); - - if(wlindex > -1) - Settings.whitelist.splice(site, 1); - if(blindex > -1) - Settings.blacklist.splice(site, 1); - - if(message.option == "whitelist") - Settings.whitelist.push(site); - if(message.option == "blacklist") - Settings.blacklist.push(site); - - Settings.save(Settings); - } - if(message.cmd == "testing"){ if(Browserdetect.usebrowser = "firefox") return Promise.resolve({response: "test response hier"}); diff --git a/res/popup/js/popup.js b/res/popup/js/popup.js index 9cf6226..f3a23fc 100644 --- a/res/popup/js/popup.js +++ b/res/popup/js/popup.js @@ -4,7 +4,7 @@ if(Debug.debug) document.getElementById("uw-version").textContent = browser.runtime.getManifest().version; var Menu = {}; -Menu.noVideo = document.getElementById("no-videos-display"); +// Menu.noVideo = document.getElementById("no-videos-display"); Menu.general = document.getElementById("extension-mode"); Menu.thisSite = document.getElementById("settings-for-current-site"); Menu.arSettings = document.getElementById("aspect-ratio-settings"); @@ -66,6 +66,10 @@ async function sendMessage(message){ return response; } +function hideWarning(warn){ + document.getElementById(warn).classList.add("hidden"); +} + function check4videos(){ Comms.sendToBackgroundScript({cmd: "has-videos"}) @@ -75,7 +79,8 @@ function check4videos(){ } if(response.response.hasVideos){ hasVideos = true; - openMenu(selectedMenu); +// openMenu(selectedMenu); + hideWarning("no-videos-warning"); } }) .catch(error => { @@ -104,7 +109,7 @@ function check4conf(){ } function check4siteStatus(){ - Comms.sendToMain({cmd: "uw-enabled-for-site"}) + Comms.sendToBackgroundScript({cmd: "uw-enabled-for-site"}) .then(response => { if(Debug.debug) console.log("[popup::check4siteStatus] received response:", response); @@ -208,13 +213,13 @@ function openMenu(menu){ } if(menu == "arSettings" || menu == "cssHacks" ){ - if(!hasVideos) - Menu.noVideo.classList.remove("hidden"); - else{ +// if(!hasVideos) +// Menu.noVideo.classList.remove("hidden"); +// else{ Menu[menu].classList.remove("hidden"); if(Debug.debug){ console.log("[popup.js::openMenu] unhid", menu, "| element: ", Menu[menu]); - } +// } } } else{ @@ -224,10 +229,10 @@ function openMenu(menu){ } } - if(menu != "noVideo"){ - selectedMenu = menu; - MenuTab[menu].classList.add("selected"); - } +// if(menu != "noVideo"){ +// selectedMenu = menu; +// MenuTab[menu].classList.add("selected"); +// } } function _arctl_onclick(command){ @@ -280,7 +285,7 @@ function toggleSite(option){ if(Debug.debug) console.log("[popup::toggleSite] toggling extension 'should I work' status to", option, "on current site"); - Comms.sendToMain({cmd:"enable-for-site", option:option}); + Comms.sendToBackgroundScript({cmd:"enable-for-site", option:option}); } document.addEventListener("click", (e) => { @@ -422,7 +427,8 @@ document.addEventListener("click", (e) => { return true; }); - +hideWarning("script-not-running-warning"); +openMenu(selectedMenu); check4videos(); check4conf(); check4siteStatus(); diff --git a/res/popup/popup.html b/res/popup/popup.html index 39852ee..d683965 100644 --- a/res/popup/popup.html +++ b/res/popup/popup.html @@ -108,6 +108,23 @@ .w24 { width: 100px; } + .warning { + /*background-*/color: #d6ba4a; +/* color: #000; */ + padding-left: 35px; + float: right; + } + .warning::before { + content: "⚠ "; + display: inline-block; + font-weight: bold; + font-size: 2.5em; + margin-left: -35px; + padding-right: 10px; +/* top: 0px; */ + float: left; + } + .button:hover { color: #fff; background-color: #433221; @@ -153,13 +170,7 @@
-
-

No videos have been found on this page.

If there is a video on this page, then you're seeing this message for one of these reasons:

-
  • Page hasn't finished loading
    Note: webextensions only start working when page is fully loaded.
  • -
  • The video is embedded / in an iframe.
    This is a known issue/limitation. Use keyboard shortcuts to correct aspect ratio.
  • -
  • Hamster Rajko is on strike.
    (Close this popup and reopen in a few seconds)
-

Keyboard shortcuts:
* A - attempt automatic detection
* W - fit to width
* E - fit to height
* R - reset/stop automatic detection

Force aspect ratio:
* S: 16:9
* D: 21:9
* X: 2:1

-
+
If you see this line, then your browser didn't start the popup script yet. This is a problem caused by the browser. Please wait a few seconds.