From 174c987690a343d596348b44a45f38188b3d427a Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Thu, 1 Feb 2018 00:26:16 +0100 Subject: [PATCH] Keyboard shortcuts get loaded in popup, fixed 'reset' button in popup. --- js/conf/Debug.js | 2 +- js/conf/Keybinds.js | 6 ++- js/conf/Settings.js | 25 +++++++--- js/conf/SitesConf.js | 8 ++-- js/lib/Comms.js | 103 ++++++++++++++++++++++++----------------- js/modules/Resizer.js | 3 +- js/uw-bg.js | 37 ++------------- js/uw.js | 104 +++++++++++++++++++++--------------------- res/popup/js/popup.js | 102 +++++++++++++++++++++++++++++++---------- res/popup/popup.html | 19 ++++++-- 10 files changed, 238 insertions(+), 171 deletions(-) diff --git a/js/conf/Debug.js b/js/conf/Debug.js index a95f98f..aa4de47 100644 --- a/js/conf/Debug.js +++ b/js/conf/Debug.js @@ -9,7 +9,7 @@ Debug = { debugArDetect: false, debugStorage: true, showArDetectCanvas: false, - flushStoredSettings: true + flushStoredSettings: false } if(_prod){ diff --git a/js/conf/Keybinds.js b/js/conf/Keybinds.js index 7b769c3..d5ff9c2 100644 --- a/js/conf/Keybinds.js +++ b/js/conf/Keybinds.js @@ -79,7 +79,6 @@ var _kbd_process = function (event) { // Tukaj ugotovimo, katero tipko ArDetect.stop(); if(conf.action == "char"){ -// Status.arStat = "fixed"; Resizer.setAr(conf.targetAr); } else{ @@ -136,6 +135,8 @@ var _kbd_fetch = async function(){ return _kbd_keybinds; } + + var _kbd_getKeybinds = function(){ return _kbd_keybinds; } @@ -144,5 +145,6 @@ var Keybinds = { init: _kbd_setup, fetch: _kbd_fetch, mods: _kbd_ModKeys, - getKeybinds: _kbd_getKeybinds + getKeybinds: _kbd_getKeybinds, + keybinds: _kbd_keybinds } diff --git a/js/conf/Settings.js b/js/conf/Settings.js index 51dcf12..62b166e 100644 --- a/js/conf/Settings.js +++ b/js/conf/Settings.js @@ -6,8 +6,8 @@ if(Debug.debug) var _se_init = async function(neverFlushStored){ - if(Debug.flushStoredSettings && neverFlushStored === false) - StorageManager.delopt("uw-settings"); +// if(Debug.flushStoredSettings && neverFlushStored === false) +// StorageManager.delopt("uw-settings"); if(Debug.debug) console.log("[Settings::_se_init()] -------- starting init! ---------"); @@ -15,10 +15,10 @@ var _se_init = async function(neverFlushStored){ var newSettings = await StorageManager.getopt_async("uw-settings"); if (Debug.debug) - console.log("[Settings::_se_init()] settings saved in localstorage are ", (newSettings === {} ? ("nonexistent (", newSettings, ")") : newSettings )); + 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 (newSettings === {}){ - StorageManager.setopt({"uw-settings": this}); + if ((Object.keys(newSettings).length === 0 && newSettings.constructor === Object)){ + StorageManager.setopt({"uw-settings": JSON.stringify(this)}); } else{ for (var k in newSettings) @@ -30,9 +30,18 @@ var _se_init = async function(neverFlushStored){ } -var _se_save = function(){ +var _se_save = function(settings){ StorageManager.delopt("uw-settings"); - StorageManager.setopt({"uw-settings": this}); + + if(settings !== undefined){ + StorageManager.setopt({"uw-settings": JSON.stringify(settings)}); + } + else{ + StorageManager.setopt({"uw-settings": JSON.stringify(this)}); + } + + if (Debug.debug) + console.log("[Settings::_se_save()] saving settings:", JSON.stringify(settings)); } var _se_reload = function(){ @@ -64,6 +73,8 @@ var Settings = { colors:{ // criticalFail: "background: #fa2; color: #000" }, + whitelist: [], + blacklist: ["vimeo.com"], init: _se_init, save: _se_save, reload: _se_reload, diff --git a/js/conf/SitesConf.js b/js/conf/SitesConf.js index f5fdaf2..096b4c3 100644 --- a/js/conf/SitesConf.js +++ b/js/conf/SitesConf.js @@ -73,10 +73,12 @@ var _sc_init = function() { * : { * enabled: string, // whitelist, blacklist, global * type: string, - * autoAr: { // konfiguracija za samodejno zaznavanje razmerja stranic | conf for aspect ratio autodetection + * autoAr: { // konfiguracija za samodejno zaznavanje razmerja stranic | conf for aspect ratio autodetection * active: bool // aktivno zaznavanje — zaznavamo letterbox na sliki | active detection: scan the image - * passive: bool // pasivno zaznavanje — za ar vprašamo imdb in ostale | passive detection: query imdb for aspect ratio - * nonfs: bool // zaznavanje razmerja stranic izven celozaslonskega načina | detect ar if not in fullscreen? + * passive: bool // pasivno zaznavanje — za ar vprašamo imdb in ostale | passive detection: query imdb + * // for aspect ratio [DEPRECATED] + * nonfs: bool // zaznavanje razmerja stranic izven celozaslonskega načina | detect ar if not in + * // fullscreen? [DEPRECATED] * playerIdentificationString: string * playerIdentificationType: string // "className" | "id" * nonfsExtra: function // non-fs hacks are generally site-specific, which means we need to write site-specific code diff --git a/js/lib/Comms.js b/js/lib/Comms.js index 771a237..a9ee265 100644 --- a/js/lib/Comms.js +++ b/js/lib/Comms.js @@ -108,52 +108,68 @@ var _com_sendToEachFrame = async function(message, tabId) { if(Debug.debug) console.log("[Comms::_com_sendToEveryFrame] sending message to every frames of currenntly active tab"); -// try{ - if(tabId === undefined){ - var tabs = await browser.tabs.query({currentWindow: true, active: true}); - tabId = tabs[0].id; - } - var frames = await browser.webNavigation.getAllFrames({tabId: tabId}); - - if(Debug.debug) - console.log("[Comms::_com_sendToEveryFrame] we have this many frames:", frames.length, "||| tabId:", tabId ,"frames:",frames); - - - // pošlji sporočilce vsakemu okvirju, potisni obljubo v tabelo - // send message to every frame, push promise to array - var promises = []; - for(var frame of frames){ + if(tabId === undefined){ + var tabs = await browser.tabs.query({currentWindow: true, active: true}); + tabId = tabs[0].id; + } + var frames = await browser.webNavigation.getAllFrames({tabId: tabId}); + + if(Debug.debug) + console.log("[Comms::_com_sendToEveryFrame] we have this many frames:", frames.length, "||| tabId:", tabId ,"frames:",frames); + + + // pošlji sporočilce vsakemu okvirju, potisni obljubo v tabelo + // send message to every frame, push promise to array + var promises = []; + for(var frame of frames){ + if(Debug.debug) + console.log("[Comms:_com_sendToEachFrame] we sending message to tab with id", tabId, ", frame with id", frame.frameId); + try{ + promises.push(browser.tabs.sendMessage(tabId, message, {frameId: frame.frameId})); + } + catch(e){ if(Debug.debug) console.log("[Comms:_com_sendToEachFrame] we sending message to tab with id", tabId, ", frame with id", frame.frameId); - try{ - promises.push(browser.tabs.sendMessage(tabId, message, {frameId: frame.frameId})); - } - catch(e){ - if(Debug.debug) - console.log("[Comms:_com_sendToEachFrame] we sending message to tab with id", tabId, ", frame with id", frame.frameId); - } - } + } + } + + // počakajmo, da so obljube izpolnjene. + // wait for all promises to be kept + + var responses = []; + + for(var promise of promises){ + var response = await promise; + if(response !== undefined) + responses.push(response); + } + + if(Debug.debug) + console.log("[Comms::_com_sendToEveryFrame] we received responses from all frames", responses); + + return responses; +} + +var _com_sendToMainFrame = async function(message, tabId){ + if(Debug.debug) + console.log("[Comms::_com_sendToMainFrame] sending message to every frames of currenntly active tab"); + + if(tabId === undefined){ + var tabs = await browser.tabs.query({currentWindow: true, active: true}); + tabId = tabs[0].id; + } + + // pošlji sporočilce glavnemu okvirju. Glavni okvir ima id=0 + // send message to the main frame. Main frame has id=0 + try{ + var response = await browser.tabs.sendMessage(tabId, message, {frameId: 0}); + console.log("[Comms::_com_sendToMainFrame] response is this:",response); - // počakajmo, da so obljube izpolnjene. - // wait for all promises to be kept - - var responses = []; - - for(var promise of promises){ - var response = await promise; - if(response !== undefined) - responses.push(response); - } - - if(Debug.debug) - console.log("[Comms::_com_sendToEveryFrame] we received responses from all frames", responses); - - return responses; -// } -// catch(e){ -// console.log("[Comms::_com_sendToEveryFrame] something went wrong when getting frames. this is error:", e); -// return Promise.reject(); -// } + } + catch(e){ + console.log("[Comms:_com_sendToEachFrame] failed sending message to tab with id", tabId, ", frame with id", 0, "\nerror:",e); + } + return response; } var Comms = { @@ -164,4 +180,5 @@ var Comms = { sendMessageRuntime: _com_sendMessageRuntime, sendToEach: _com_sendToEachFrame, sendToAll: _com_sendToAllFrames, + sendToMain: _com_sendToMainFrame, } diff --git a/js/modules/Resizer.js b/js/modules/Resizer.js index 8c0f684..b417195 100644 --- a/js/modules/Resizer.js +++ b/js/modules/Resizer.js @@ -88,6 +88,7 @@ var _res_legacyAr = function(action){ var fileAr = vid.videoWidth / vid.videoHeight; GlobalVars.lastAr = {type: "legacy", action: action}; + if(action == "fitw"){ _res_setAr( ar > fileAr ? ar : fileAr); return; @@ -101,7 +102,7 @@ var _res_legacyAr = function(action){ _res_setAr(fileAr); return; } - if(action == "autoar"){ + if(action == "autoar" || action == "auto"){ GlobalVars.lastAr = {type: "auto"}; ArDetect.init(); } diff --git a/js/uw-bg.js b/js/uw-bg.js index d355ded..e86390f 100644 --- a/js/uw-bg.js +++ b/js/uw-bg.js @@ -10,6 +10,8 @@ async function main(){ await Settings.init(); await Keybinds.init(); + Keybinds.keybinds = await Keybinds.fetch(); + // Poslušalci za dogodke | event listeners here // {===]///[-------------------------------------]\\\[===} @@ -95,9 +97,9 @@ function _uwbg_rcvmsg(message, sender, sendResponse){ config.arConf.enabled_global = Settings.arDetect.enabled == "global"; if(Debug.debug) - console.log("[uw-bg::_uwbg_rcvmsg] Keybinds.fetch returned this:", keybinds); + console.log("[uw-bg::_uwbg_rcvmsg] Keybinds.getKeybinds() returned this:", Keybinds.getKeybinds()); - config.keyboardShortcuts = BgVars.keyboardShortcuts; + config.keyboardShortcuts = Keybinds.getKeybinds(); // predvidevajmo, da je enako. Če je drugače, bomo popravili ko dobimo odgovor @@ -123,37 +125,6 @@ function _uwbg_rcvmsg(message, sender, sendResponse){ _uwbg_registerVideo(sender.tab.id); } -// else if(message.cmd == "force-ar"){ -// sendMessage(message); // args: {cmd: string, newAr: number/"auto"} -// } -// else if(message.cmd == "stop-autoar"){ -// sendMessage(message); -// } -// else if(message.cmd == "force-video-float"){ -// if(message.global){ -// Settings.miscFullscreenSettings.videoFloat = message.newFloat; -// sendMessage(message); -// } -// else{ -// sendMessage(message); -// } -// } -// -// else if(message.cmd == "disable-autoar"){ -// Settings.arDetect.enabled = "no"; -// Settings.save(); -// sendMessage("reload-settings"); -// } -// else if(message.cmd == "disable-autoar-whitelist-only"){ -// Settings.arDetect.enabled = "whitelist"; -// Settings.save(); -// sendMessage("reload-settings"); -// } -// else if(message.cmd == "enable-autoar"){ -// Settings.arDetect.enabled = "global"; -// Settings.save(); -// sendMessage("reload-settings"); -// } } diff --git a/js/uw.js b/js/uw.js index 2db0496..a1c8379 100644 --- a/js/uw.js +++ b/js/uw.js @@ -132,54 +132,13 @@ function ghettoOnChange(){ } -var _main_fscheck_tries = 3; -// function fullScreenCheck(count) { -// if(count >= _main_fscheck_tries){ -// if(Debug.debug){ -// console.log("[uw::fullScreenCheck] ok really, I guess."); -// } -// return; -// } -// -// var fsnow = FullScreenDetect.isFullScreen(); -// -// // we restore, always — esp. now that we also do things in non-fullscreen -// -// // if(fsnow){ -// // full screen is on -// // Resizer.restore(); -// // } -// // else{ -// // Resizer.reset(); -// // } -// -// // kaj pa, če je FullScreenDetect vrnil narobno vrednost? -// // what if FullScreenDetect was not right? Let's verify; if it was wrong we re-trigger it in about 100 ms. -// -// if(fsnow != _main_last_fullscreen){ -// -// // posodobimo vrednost / update value -// _main_last_fullscreen = fsnow; -// -// // če je to res, count pa je večji kot 0, potem smo imeli prav. -// // if that's the case and count is greater than 0, then we were right at some point. -// if(Debug.debug && count > 0){ -// console.log("[uw::fullScreenCheck] fucking knew it") -// } -// return; -// } -// else{ -// // dobili smo event za spremembo celozaslonskega stanja. Stanje se ni spremenilo. Hmmm. -// // we got an event for fullscreen state change. State is unchanged. Hmmm. -// if(Debug.debug){ -// console.log("[uw::fullScreenCheck] oh _really_? 🤔🤔🤔 -- fullscreen state", FullScreenDetect.isFullScreen()); -// } -// count++; -// setTimeout(fullScreenCheck, 200, count); -// } -// console.log("-------------------------------"); -// } + + + + + + // comms function receiveMessage(message, sender, sendResponse) { @@ -234,14 +193,12 @@ function receiveMessage(message, sender, sendResponse) { if(Debug.debug) console.log("[uw::receiveMessage] we're being commanded to change aspect ratio to", message.newAr); - if(message.newAr == "auto"){ - ArDetect.stop(); // just in case - ArDetect.arSetup(); + if(message.arType == "legacy"){ + ArDetect.stop(); + Resizer.legacyAr(message.newAr); } else{ ArDetect.stop(); - - // we aren't in full screen, but we will want aspect ratio to be fixed when we go to Resizer.setAr(message.newAr); } } @@ -258,6 +215,49 @@ 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") diff --git a/res/popup/js/popup.js b/res/popup/js/popup.js index 98ec181..9cf6226 100644 --- a/res/popup/js/popup.js +++ b/res/popup/js/popup.js @@ -6,12 +6,14 @@ document.getElementById("uw-version").textContent = browser.runtime.getManifest( var Menu = {}; 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"); Menu.cssHacks = document.getElementById("css-hacks-settings"); Menu.about = document.getElementById("panel-about"); var MenuTab = {}; MenuTab.general = document.getElementById("_menu_general"); +MenuTab.thisSite = document.getElementById("_menu_this_site"); MenuTab.arSettings = document.getElementById("_menu_aspectratio"); MenuTab.cssHacks = document.getElementById("_menu_hacks"); MenuTab.about = document.getElementById("_menu_about"); @@ -86,7 +88,7 @@ function check4videos(){ function check4conf(){ - sendToBackgroundScript({cmd: "get-config"}) + Comms.sendToBackgroundScript({cmd: "get-config"}) .then(response => { if(Debug.debug) console.log("[popup.js::check4conf] received response:",response, response.response); @@ -101,6 +103,22 @@ function check4conf(){ }); } +function check4siteStatus(){ + Comms.sendToMain({cmd: "uw-enabled-for-site"}) + .then(response => { + if(Debug.debug) + console.log("[popup::check4siteStatus] received response:", response); + + document.extensionEnabledOnCurrentSite.mode.value = response.response; + }) + .catch(error => { + if(Debug.debug) + console.log("%c[popup.js::check4siteStatus] sending message failed with error", "color: #f00", error, "%c retrying in 1s ...", "color: #f00"); + +// setTimeout(check4siteStatus, 1000); + }); +} + function stringToKeyCombo(key_in){ var keys_in = key_in.split("_"); var keys_out = ""; @@ -232,30 +250,37 @@ function showArctlButtons(){ if(! _config) return; - if(_config.arConf){ - if(! _config.arConf.enabled_global){ - ArPanel.autoar.disable.classList.add("hidden"); - ArPanel.autoar.enable.classList.remove("hidden"); - - ArPanel.autoar.enable_tmp.textContent = "Temporarily enable"; - ArPanel.autoar.disable_tmp.textContent = "Temporarily disable"; - } - else{ - ArPanel.autoar.disable.classList.remove("hidden"); - ArPanel.autoar.enable.classList.add("hidden"); - - ArPanel.autoar.enable_tmp.textContent = "Re-enable"; - ArPanel.autoar.disable_tmp.textContent = "Temporarily disable"; - } - if(! _config.arConf.enabled_current){ - ArPanel.autoar.disable_tmp.classList.add("hidden"); - ArPanel.autoar.enable_tmp.classList.remove("hidden"); - } - else{ - ArPanel.autoar.disable_tmp.classList.remove("hidden"); - ArPanel.autoar.enable_tmp.classList.add("hidden"); - } - } +// if(_config.arConf){ +// if(! _config.arConf.enabled_global){ +// ArPanel.autoar.disable.classList.add("hidden"); +// ArPanel.autoar.enable.classList.remove("hidden"); +// +// ArPanel.autoar.enable_tmp.textContent = "Temporarily enable"; +// ArPanel.autoar.disable_tmp.textContent = "Temporarily disable"; +// } +// else{ +// ArPanel.autoar.disable.classList.remove("hidden"); +// ArPanel.autoar.enable.classList.add("hidden"); +// +// ArPanel.autoar.enable_tmp.textContent = "Re-enable"; +// ArPanel.autoar.disable_tmp.textContent = "Temporarily disable"; +// } +// if(! _config.arConf.enabled_current){ +// ArPanel.autoar.disable_tmp.classList.add("hidden"); +// ArPanel.autoar.enable_tmp.classList.remove("hidden"); +// } +// else{ +// ArPanel.autoar.disable_tmp.classList.remove("hidden"); +// ArPanel.autoar.enable_tmp.classList.add("hidden"); +// } +// } +} + +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}); } document.addEventListener("click", (e) => { @@ -274,6 +299,9 @@ document.addEventListener("click", (e) => { if(e.target.classList.contains("_menu_general")){ openMenu("general"); } + if(e.target.classList.contains("_menu_this_site")){ + openMenu("thisSite"); + } else if(e.target.classList.contains("_menu_aspectratio")){ openMenu("arSettings"); } @@ -292,31 +320,49 @@ document.addEventListener("click", (e) => { if(e.target.classList.contains("_ar_auto")){ command.cmd = "force-ar"; command.newAr = "auto"; + command.arType = "legacy"; return command; } if(e.target.classList.contains("_ar_reset")){ command.cmd = "force-ar"; command.newAr = "reset"; + command.arType = "legacy"; + return command; + } + if(e.target.classList.contains("_ar_fitw")){ + command.cmd = "force-ar"; + command.newAr = "fitw"; + command.arType = "legacy"; + return command; + } + if(e.target.classList.contains("_ar_fitw")){ + command.cmd = "force-ar"; + command.newAr = "fith"; + command.arType = "legacy"; return command; } if(e.target.classList.contains("_ar_219")){ command.cmd = "force-ar"; command.newAr = 2.39; + command.arType = "static"; return command; } if(e.target.classList.contains("_ar_189")){ command.cmd = "force-ar"; command.newAr = 2.0; + command.arType = "static"; return command; } if(e.target.classList.contains("_ar_169")){ command.cmd = "force-ar"; command.newAr = 1.78; + command.arType = "static"; return command; } if(e.target.classList.contains("_ar_1610")){ command.cmd = "force-ar"; command.newAr = 1.6; + command.arType = "static"; return command; } } @@ -362,6 +408,11 @@ document.addEventListener("click", (e) => { return command; } } + + if(e.target.classList.contains("extensionEnabledOnCurrentSite")){ + toggleSite(document.extensionEnabledOnCurrentSite.mode.value); + } + } var command = getcmd(e); @@ -374,3 +425,4 @@ document.addEventListener("click", (e) => { check4videos(); check4conf(); +check4siteStatus(); diff --git a/res/popup/popup.html b/res/popup/popup.html index 9b13696..39852ee 100644 --- a/res/popup/popup.html +++ b/res/popup/popup.html @@ -139,8 +139,8 @@ Ultrawidify: Quick settings
-