From de2677fdd60d95d4c9f634e94e4a11af14b9f1db Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Fri, 29 Dec 2017 23:34:40 +0100 Subject: [PATCH] Added ui/popup window (and it kinda works) --- js/conf/Debug.js | 4 +- js/conf/Keybinds.js | 3 + js/conf/Settings.js | 47 ++++++- js/conf/SitesConf.js | 2 +- js/lib/StorageManager.js | 14 +- js/modules/ArDetect.js | 46 +++++-- js/modules/Resizer.js | 273 ++++++++++++++------------------------- js/uw-bg.js | 70 ++++++++++ js/uw.js | 52 +++++++- manifest.json | 12 +- res/popup/js/popup.js | 107 ++++++++++++++- res/popup/popup.html | 62 ++++++++- 12 files changed, 486 insertions(+), 206 deletions(-) create mode 100644 js/uw-bg.js diff --git a/js/conf/Debug.js b/js/conf/Debug.js index 34a1f4d..cbfac0c 100644 --- a/js/conf/Debug.js +++ b/js/conf/Debug.js @@ -1,7 +1,9 @@ Debug = { debug: true, + debugResizer: true, debugArDetect: true, - showArDetectCanvas: false + showArDetectCanvas: false, + flushStoredSettings: true } if(Debug.debug) diff --git a/js/conf/Keybinds.js b/js/conf/Keybinds.js index eacf5a2..0ccab91 100644 --- a/js/conf/Keybinds.js +++ b/js/conf/Keybinds.js @@ -1,3 +1,6 @@ +if(Debug.debug) + console.log("Loading: Keybinds.js"); + // Yeah hi /r/badcode. // Anyway, because nazi localstorage flat out refuses to store arrays: diff --git a/js/conf/Settings.js b/js/conf/Settings.js index 29351bc..3a0c4a4 100644 --- a/js/conf/Settings.js +++ b/js/conf/Settings.js @@ -4,15 +4,48 @@ if(Debug.debug) console.log("Loading: Settings.js"); -var _se_init = function(){ - console.log("pls load settings from localstorage here"); +var _se_init = async function(neverFlushStored){ + + if(Debug.flushStoredSettings && neverFlushStored === false) + StorageManager.delopt("uw-settings"); + + if(Debug.debug) + console.log("[Settings::_se_init()] -------- starting init! ---------"); + + + 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 )); + + if (newSettings === {}){ + StorageManager.setopt({"uw-settings": this}); + } + else{ + for (var k in newSettings) + this[k] = newSettings[k]; + } + + if(Debug.debug) + console.log("[Settings::_se_init] settings have been loaded/reloaded. Current state: ", this); + } -Settings = { +var _se_save = function(){ + StorageManager.delopt("uw-settings"); + StorageManager.setopt({"uw-settings": this}); +} + +var _se_reload = function(){ + this.init(true); +} + +var Settings = { arDetect: { - enabled: "global", + enabled: "global", // thats my csgo rank kappa allowedMisaligned: 0.01, // top and bottom letterbox thickness can differ by this much. Any more and we don't adjust ar. allowedArVariance: 0.025, // % by which old ar can differ from the new + timer_playing: 30, + timer_paused: 3000, blacklist: [], // banned on enabled: "global" whitelist: [] // enabled on enabled: "whitelist-only", disabled on "disabled" }, @@ -20,7 +53,9 @@ Settings = { samenessTreshold: 0.025, // if aspect ratios are within 2.5% within each other, don't resize }, miscFullscreenSettings: { - videoFloat: "center", + videoFloat: "center" }, - init: _se_init + init: _se_init, + save: _se_save, + reload: _se_reload, } diff --git a/js/conf/SitesConf.js b/js/conf/SitesConf.js index 5e179fa..f5fdaf2 100644 --- a/js/conf/SitesConf.js +++ b/js/conf/SitesConf.js @@ -42,7 +42,7 @@ var _sc_prepareNonfsPlayer = function(){ } var _sc_getMode = function(site){ - if(! this.sites[site] ) + if(! this || !this.sites || ! this.sites[site] ) return "global"; return this.sites[site].enabled; diff --git a/js/lib/StorageManager.js b/js/lib/StorageManager.js index ab2ba07..6a2bdbf 100644 --- a/js/lib/StorageManager.js +++ b/js/lib/StorageManager.js @@ -9,7 +9,16 @@ var _sm_getopt = function(prop, callback){ return browser.storage.local.get(prop, callback); else return browser.storage.local.get(prop).then(callback); -} +} + + +var _sm_getopt_async = async function(prop){ + if(BrowserDetect.usebrowser == "chrome") + return await browser.storage.local.get(prop); + else + return await browser.storage.local.get(prop); +} + var _sm_delopt = function(item){ return browser.storage.local.remove(item); } @@ -17,5 +26,6 @@ var _sm_delopt = function(item){ var StorageManager = { setopt: _sm_setopt, getopt: _sm_getopt, - delopt: _sm_delopt + delopt: _sm_delopt, + getopt_async: _sm_getopt_async } diff --git a/js/modules/ArDetect.js b/js/modules/ArDetect.js index 7d6f1d4..efd1db8 100644 --- a/js/modules/ArDetect.js +++ b/js/modules/ArDetect.js @@ -1,14 +1,18 @@ if(Debug.debug) console.log("Loading: ArDetect"); - - +var _ard_console_stop = "background: #000; color: #f41"; +var _ard_console_start = "background: #000; color: #00c399"; // global-ish variables var _ard_oldAr; var _ard_currentAr; + +var _ard_setup_timer; +var _ard_timer + // kjer vzemamo vzorce za blackbox/stuff. 9 vzorcev. Če spremenimo velikost vzorca, moramo spremeniti tudi vrednosti v tej tabeli // vrednosti v tabeli so na osminskih intervalih od [0, - 4]. // we sample these lines in blackbox/stuff. 9 samples. If we change the canvas sample size, we have to correct these values as well @@ -21,10 +25,13 @@ var _ard_sampleLines = [ 0, 360, 720, 1080, 1440, 1800, 2160, 2520, 2876] // **** FUNCTIONS **** // var _arSetup = function(){ + if(Debug.debug) + console.log("%c[ArDetect::_ard_setup] Starting automatic aspect ratio detection", _ard_console_start); + var vid = document.getElementsByTagName("video")[0]; if(vid === undefined){ - setTimeout(_arSetup, 1000); + _ard_setup_timer = setTimeout(_arSetup, 1000); return; } @@ -32,7 +39,7 @@ var _arSetup = function(){ // we have a video, but also a problem. This problem will prolly be fixed very soon, so setup is called with // less delay than before if(vid.videoWidth == 0){ - setTimeout(_arSetup, 100); + _ard_setup_timer = setTimeout(_arSetup, 100); return; } @@ -75,6 +82,7 @@ var _arSetup = function(){ _ard_oldAr = vid.videoWidth / vid.videoHeight; _ard_currentAr = _ard_oldAr; + this._forcehalt = false; _ard_vdraw(vid, context, canvasWidth, canvasHeight, false); }; @@ -134,13 +142,18 @@ var _ard_processAr = function(video, width, height, edge_h, edge_w){ } var _ard_vdraw = function (vid, context, w, h, conf){ + if(this._forcehalt) + return; + var blackbar_tresh = 10; // how non-black can the bar be var how_far_treshold = 8; // how much can the edge pixel vary (*4) - var msec_pause = 33; // how long is the pause between two executions — 33ms ~ 30fps + + if(Debug.debug) + Settings.arDetect.timer_playing = 1000; // how long is the pause between two executions — 33ms ~ 30fps if(vid === undefined || vid.paused || vid.ended || Status.arStrat != "auto"){ // we slow down if paused, no detection - setTimeout(_ard_vdraw, 3000, vid, context, w, h); + _ard_timer = setTimeout(_ard_vdraw, 3000, vid, context, w, h); return false; } @@ -175,7 +188,7 @@ var _ard_vdraw = function (vid, context, w, h, conf){ // corrected mode. _ard_processAr(vid, w, h); - setTimeout(_ard_vdraw, msec_pause, vid, context, w, h); //no letterbox, no problem + _ard_timer = setTimeout(_ard_vdraw, Settings.arDetect.timer_playing, vid, context, w, h); //no letterbox, no problem return; } @@ -312,7 +325,7 @@ var _ard_vdraw = function (vid, context, w, h, conf){ if(blackPoints > (blackPointsMax >> 1)){ // if more than half of those points are black, we consider the entire frame black (or too dark to get anything useful // out of it, anyway) - setTimeout(_ard_vdraw, msec_pause, vid, context, w, h); //no letterbox, no problem + _ard_timer = setTimeout(_ard_vdraw, Settings.arDetect.timer_playing, vid, context, w, h); //no letterbox, no problem return; } @@ -321,7 +334,7 @@ var _ard_vdraw = function (vid, context, w, h, conf){ // why exactly are we here again? _ard_processAr(vid, w, h); - setTimeout(_ard_vdraw, msec_pause, vid, context, w, h); //no letterbox, no problem + _ard_timer = setTimeout(_ard_vdraw, Settings.arDetect.timer_playing, vid, context, w, h); //no letterbox, no problem return; } @@ -399,13 +412,24 @@ var _ard_vdraw = function (vid, context, w, h, conf){ if(isLetter) _ard_processAr(vid, w, h, topPixel); - setTimeout(_ard_vdraw, msec_pause, vid, context, w, h); + _ard_timer = setTimeout(_ard_vdraw, Settings.arDetect.timer_playing, vid, context, w, h); } +var _ard_stop = function(){ + if(Debug.debug){ + console.log("%c[ArDetect::_ard_stop] Stopping automatic aspect ratio detection", _ard_console_stop); + } + this._forcehalt = true; + clearTimeout(_ard_timer); + clearTimeout(_ard_setup_timer); +} var ArDetect = { + _forcehalt: false, + arSetup: _arSetup, vdraw: _ard_vdraw, detectedAr: 1, - arChangedCallback: function() {} + arChangedCallback: function() {}, + stop: _ard_stop, } diff --git a/js/modules/Resizer.js b/js/modules/Resizer.js index ec4eb4c..4c367f2 100644 --- a/js/modules/Resizer.js +++ b/js/modules/Resizer.js @@ -337,6 +337,8 @@ var _res_legacyAr = function(action){ var ar = screen.width / screen.height; var fileAr = vid.videoWidth / vid.videoHeight; + + if(action == "fitw"){ _res_setAr_kbd( ar > fileAr ? ar : fileAr); return; @@ -346,24 +348,32 @@ var _res_legacyAr = function(action){ return; } if(action == "reset"){ - _res_setAr_kbd(fileAr); +// _res_setAr_kbd(fileAr); + this.reset(true); return; } } -var _res_reset = function(){ +var _res_reset = function(force){ dimensions = {top: "", left: "", width: "100%", height: "100%"}; $("video").css({"position": "relative", "width": dimensions.width,"height": dimensions.height,"top": dimensions.top, "left": dimensions.left}); if(Debug.debug) - console.log("[Resizer::_res_applyCss] css applied. Dimensions/pos: w:",dimensions.width,"; h:",dimensions.height,"; top:",dimensions.top,"; left:",dimensions.left); + console.log("[Resizer::_res_reset] css applied. Dimensions/pos: w:",dimensions.width,"; h:",dimensions.height,"; top:",dimensions.top,"; left:",dimensions.left); + + if(force) + this._currentAr = -1; } var _res_setAr_kbd = function(ar){ - if(FullScreenDetect.isFullScreen()) + if(FullScreenDetect.isFullScreen()){ + if(Debug.debug) + console.log("[Resizer::_res_setAr_kbd] We're in full screen. Setting ar to ", ar); + _res_setAr(ar, {width: screen.width, height: screen.height} ); + } // else // _res_setAr_nonfs(ar); // TODO: check if site supports non-fs ar @@ -376,6 +386,9 @@ var _res_setAr = function(ar, playerDimensions){ // Actual aspect ratio of the file/