diff --git a/src/ext/lib/ActionHandler.js b/src/ext/lib/ActionHandler.js index 60eba42..803e0ce 100644 --- a/src/ext/lib/ActionHandler.js +++ b/src/ext/lib/ActionHandler.js @@ -253,9 +253,7 @@ class ActionHandler { } handleKeydown(event) { - if(Debug.debug && Debug.keyboard ){ - console.log("%c[ActionHandler::handleKeydown] we pressed a key: ", "color: #ff0", event.key , " | keydown: ", event.keydown, "event:", event); - } + this.logger.log('info', 'keyboard', "%c[ActionHandler::handleKeydown] we pressed a key: ", "color: #ff0", event.key , " | keydown: ", event.keydown, "event:", event) if (this.preventAction()) { this.logger.log('info', 'keyboard', "[ActionHandler::handleKeydown] we are in a text box or something. Doing nothing."); diff --git a/src/ext/lib/Logger.js b/src/ext/lib/Logger.js index 8768a44..4001603 100644 --- a/src/ext/lib/Logger.js +++ b/src/ext/lib/Logger.js @@ -1,20 +1,83 @@ +import currentBrowser from '../conf/BrowserDetect'; + class Logger { constructor(conf) { - this.conf = conf; + this.initLogger(); + if (conf) { + this.conf = conf; + } this.history = []; this.startTime = performance.now(); this.temp_disable = false; } + initLogger() { + const ths = this; + const br = currentBrowser.firefox ? browser : chrome; + br.storage.onChanged.addListener( (changes, area) => { + if (Debug.debug && Debug.debugStorage) { + console.log("[Logger::] Settings have been changed outside of here. Updating active settings. Changes:", changes, "storage area:", area); + if (changes['uwLogger'] && changes['uwLogger'].newValue) { + console.log("[Logger::] new settings object:", JSON.parse(changes.uwLogger.newValue)); + } + } + if(changes['uwLogger'] && changes['uwLogger'].newValue) { + ths.conf = JSON.parse(changes.uwLogger.newValue); + } + }); + } + + async init() { + this.conf = await this.getSaved(); + } + clear() { this.log = []; this.startTime = performance.now(); } setConf(conf) { - this.conf = conf; + this.conf = conf; // effective immediately + // also persist settings: + if (currentBrowser.firefox || currentBrowser.edge) { + extensionConf.version = this.version; + return browser.storage.local.set( {'uwLogger': JSON.stringify(this.conf)}); + } else if (currentBrowser.chrome) { + return chrome.storage.local.set( {'uwLogger': JSON.stringify(this.logger)}); + } } + async getSaved() { + let ret; + + if (currentBrowser.firefox) { + ret = await browser.storage.local.get('uwLogger'); + } else if (currentBrowser.chrome) { + ret = await new Promise( (resolve, reject) => { + chrome.storage.local.get('uwLogger', (res) => resolve(res)); + }); + } else if (currentBrowser.edge) { + ret = await new Promise( (resolve, reject) => { + browser.storage.local.get('uwLogger', (res) => resolve(res)); + }); + } + + if (Debug.debug && Debug.debugStorage) { + try { + console.log("[Logger::getSaved] Got settings:", JSON.parse(ret.uwLogger)); + } catch (e) { + console.log("[Logger::getSaved] No settings.") + } + } + + try { + return JSON.parse(ret.uwLogger); + } catch(e) { + return {logToFile: false, logToConsole: false, consoleOptions: {}, fileOptions: {}}; + } + } + + // allow syncing of start times between bg and page scripts. // may result in negative times in the log file, but that doesn't // really matter diff --git a/src/ext/lib/ar-detect/ArDetector.js b/src/ext/lib/ar-detect/ArDetector.js index 7054baa..d5f1f40 100644 --- a/src/ext/lib/ar-detect/ArDetector.js +++ b/src/ext/lib/ar-detect/ArDetector.js @@ -389,14 +389,8 @@ class ArDetector { if( execTime > this.settings.active.arDetect.autoDisable.maxExecutionTime ){ // this.detectionTimeoutEventCount++; - // if(Debug.debug){ - // console.log("[ArDetect::getTimeout] Exec time exceeded maximum allowed execution time. This has now happened " + this.detectionTimeoutEventCount + " times in a row."); - // } // if( this.detectionTimeoutEventCount >= this.settings.active.arDetect.autoDisable.consecutiveTimeoutCount ){ - // if (Debug.debug){ - // console.log("[ArDetect::getTimeout] Maximum execution time was exceeded too many times. Automatic aspect ratio detection has been disabled."); - // } // Comms.sendToBackgroundScript({cmd: 'disable-autoar', reason: 'Automatic aspect ratio detection was taking too much time and has been automatically disabled in order to avoid lag.'}); // _ard_stop(); @@ -581,10 +575,6 @@ class ArDetector { // we don't do any corrections on frames confirmed black this.logger.log('info', 'arDetect_verbose', `%c[ArDetect::frameCheck] Black frame analysis suggests this frame is black or too dark. Doing nothing.`, "color: #fa3", bfanalysis); return; - } else { - // if (Debug.debug && Debug.arDetect) { - // console.log("%c[ArDetect::frameCheck] Black frame analysis suggests this frame is not completely black. Doing further analysis,", "color: #3fa", bfanalysis); - // } } diff --git a/src/ext/lib/ar-detect/GuardLine.js b/src/ext/lib/ar-detect/GuardLine.js index 5d4cd89..ca03448 100644 --- a/src/ext/lib/ar-detect/GuardLine.js +++ b/src/ext/lib/ar-detect/GuardLine.js @@ -92,7 +92,6 @@ class GuardLine { // should succeed by default. Also need to check bottom, for cases where only one edge is known if(! fallbackMode && (! this.blackbar.top || ! this.blackbar.bottom)) { - // console.log("NO EDGE WAS DETECTED. THIS TEST IS POINTLESS. btw guardline") return { success: true }; } diff --git a/src/ext/lib/ar-detect/edge-detect/EdgeDetect.js b/src/ext/lib/ar-detect/edge-detect/EdgeDetect.js index 74a7d47..8931667 100644 --- a/src/ext/lib/ar-detect/edge-detect/EdgeDetect.js +++ b/src/ext/lib/ar-detect/edge-detect/EdgeDetect.js @@ -12,6 +12,7 @@ class EdgeDetect{ constructor(ardConf){ this.conf = ardConf; + this.logger = ardConf.logger; this.settings = ardConf.settings; this.sampleWidthBase = this.settings.active.arDetect.edgeDetection.sampleWidth << 2; // corrected so we can work on imageData @@ -43,9 +44,7 @@ class EdgeDetect{ bars = this.edgePostprocess(edgeCandidates, this.conf.canvas.height); // } } catch (e) { - if (Debug.debug) { - console.log("%c[EdgeDetect::findBars] find bars failed.", "background: #f00, color: #000", e); - } + this.logger.log('error', 'arDetect', '%c[EdgeDetect::findBars] find bars failed.', 'background: #f00, color: #000', e); return {status: EdgeStatus.AR_UNKNOWN} } } else { @@ -58,89 +57,80 @@ class EdgeDetect{ findCandidates(image, sampleCols, guardLineOut){ try { - let upper_top, upper_bottom, lower_top, lower_bottom; - - // const cols_a = sampleCols.slice(0); - const cols_a = new Array(sampleCols.length); - const res_top = []; - - for (let i = 0; i < cols_a.length; i++) { - cols_a[i] = { - id: i, - value: sampleCols[i], - }; - } + let upper_top, upper_bottom, lower_top, lower_bottom; + + // const cols_a = sampleCols.slice(0); + const cols_a = new Array(sampleCols.length); + const res_top = []; + + for (let i = 0; i < cols_a.length; i++) { + cols_a[i] = { + id: i, + value: sampleCols[i], + }; + } - const cols_b = cols_a.slice(0); - const res_bottom = []; - - // console.log("[EdgeDetect::findCandidates] cols a, b (initial):", cols_a, cols_b); - - - this.colsThreshold = sampleCols.length * this.settings.active.arDetect.edgeDetection.minColsForSearch; - if (this.colsThreshold == 0) - this.colsThreshold = 1; - - this.blackbarThreshold = this.conf.blackLevel + this.settings.active.arDetect.blackbar.threshold; - this.imageThreshold = this.blackbarThreshold + this.settings.active.arDetect.blackbar.imageThreshold; - - // if guardline didn't fail and imageDetect did, we don't have to check the upper few pixels - // but only if upper and lower edge are defined. If they're not, we need to check full height - if(guardLineOut){ - if(guardLineOut.imageFail && !guardLineOut.blackbarFail && this.conf.guardLine.blackbar.top) { - upper_top = this.conf.guardLine.blackbar.top; - upper_bottom = this.conf.canvas.height >> 1; - lower_top = upper_bottom; - lower_bottom = this.conf.guardLine.blackbar.bottom; - } else if (! guardLineOut.imageFail && !guardLineOut.blackbarFail && this.conf.guardLine.blackbar.top) { - // ta primer se lahko zgodi tudi zaradi kakšnega logotipa. Ker nočemo, da nam en jeben - // logotip vsili reset razmerja stranic, se naredimo hrvata in vzamemo nekaj varnostnega - // pasu preko točke, ki jo označuje guardLine.blackbar. Recimo 1/8 višine platna na vsaki strani. - // a logo could falsely trigger this case, so we need to add some extra margins past - // the point marked by guardLine.blackbar. Let's say 1/8 of canvas height on either side. - upper_top = 0; - upper_bottom = this.conf.guardLine.blackbar.top + (this.conf.canvas.height >> 3); - lower_top = this.conf.guardLine.blackbar.bottom - (this.conf.canvas.height >> 3); - lower_bottom = this.conf.canvas.height - 1; - } else { + const cols_b = cols_a.slice(0); + const res_bottom = []; + + this.colsThreshold = sampleCols.length * this.settings.active.arDetect.edgeDetection.minColsForSearch; + if (this.colsThreshold == 0) + this.colsThreshold = 1; + + this.blackbarThreshold = this.conf.blackLevel + this.settings.active.arDetect.blackbar.threshold; + this.imageThreshold = this.blackbarThreshold + this.settings.active.arDetect.blackbar.imageThreshold; + + // if guardline didn't fail and imageDetect did, we don't have to check the upper few pixels + // but only if upper and lower edge are defined. If they're not, we need to check full height + if(guardLineOut){ + if(guardLineOut.imageFail && !guardLineOut.blackbarFail && this.conf.guardLine.blackbar.top) { + upper_top = this.conf.guardLine.blackbar.top; + upper_bottom = this.conf.canvas.height >> 1; + lower_top = upper_bottom; + lower_bottom = this.conf.guardLine.blackbar.bottom; + } else if (! guardLineOut.imageFail && !guardLineOut.blackbarFail && this.conf.guardLine.blackbar.top) { + // ta primer se lahko zgodi tudi zaradi kakšnega logotipa. Ker nočemo, da nam en jeben + // logotip vsili reset razmerja stranic, se naredimo hrvata in vzamemo nekaj varnostnega + // pasu preko točke, ki jo označuje guardLine.blackbar. Recimo 1/8 višine platna na vsaki strani. + // a logo could falsely trigger this case, so we need to add some extra margins past + // the point marked by guardLine.blackbar. Let's say 1/8 of canvas height on either side. + upper_top = 0; + upper_bottom = this.conf.guardLine.blackbar.top + (this.conf.canvas.height >> 3); + lower_top = this.conf.guardLine.blackbar.bottom - (this.conf.canvas.height >> 3); + lower_bottom = this.conf.canvas.height - 1; + } else { + upper_top = 0; + upper_bottom = (this.conf.canvas.height >> 1) /*- parseInt(this.conf.canvas.height * this.settings.active.arDetect.edgeDetection.middleIgnoredArea);*/ + lower_top = (this.conf.canvas.height >> 1) /*+ parseInt(this.conf.canvas.height * this.settings.active.arDetect.edgeDetection.middleIgnoredArea);*/ + lower_bottom = this.conf.canvas.height - 1; + } + } else{ upper_top = 0; upper_bottom = (this.conf.canvas.height >> 1) /*- parseInt(this.conf.canvas.height * this.settings.active.arDetect.edgeDetection.middleIgnoredArea);*/ lower_top = (this.conf.canvas.height >> 1) /*+ parseInt(this.conf.canvas.height * this.settings.active.arDetect.edgeDetection.middleIgnoredArea);*/ lower_bottom = this.conf.canvas.height - 1; } - } else{ - upper_top = 0; - upper_bottom = (this.conf.canvas.height >> 1) /*- parseInt(this.conf.canvas.height * this.settings.active.arDetect.edgeDetection.middleIgnoredArea);*/ - lower_top = (this.conf.canvas.height >> 1) /*+ parseInt(this.conf.canvas.height * this.settings.active.arDetect.edgeDetection.middleIgnoredArea);*/ - lower_bottom = this.conf.canvas.height - 1; - } - if(Debug.debug && Debug.debugArDetect){ - console.log("[EdgeDetect::findCandidates] searching for candidates on ranges", upper_top, "<->", upper_bottom, ";", lower_top, "<->", lower_bottom); - } + this.logger.log('info', 'arDetect', '[EdgeDetect::findCandidates] searching for candidates on ranges', upper_top, '<->', upper_bottom, ';', lower_top, '<->', lower_bottom); + + var upper_top_corrected = upper_top * this.conf.canvasImageDataRowLength; + var upper_bottom_corrected = upper_bottom * this.conf.canvasImageDataRowLength; + var lower_top_corrected = lower_top * this.conf.canvasImageDataRowLength; + var lower_bottom_corrected = lower_bottom * this.conf.canvasImageDataRowLength; + + // if(Debug.debugCanvas.enabled){ + // this._columnTest_dbgc(image, upper_top_corrected, upper_bottom_corrected, cols_a, res_top, false); + // this._columnTest_dbgc(image, lower_top_corrected, lower_bottom_corrected, cols_b, res_bottom, true); + // } else { + this._columnTest3_cross(image, upper_top_corrected, upper_bottom_corrected, cols_a, res_top, false); + this._columnTest3_cross(image, lower_top_corrected, lower_bottom_corrected, cols_b, res_bottom, true); + // } + + this.logger.log('info', 'arDetect', '[EdgeDetect::findCandidates] candidates found -->', {res_top: res_top, res_bottom: res_bottom}); - var upper_top_corrected = upper_top * this.conf.canvasImageDataRowLength; - var upper_bottom_corrected = upper_bottom * this.conf.canvasImageDataRowLength; - var lower_top_corrected = lower_top * this.conf.canvasImageDataRowLength; - var lower_bottom_corrected = lower_bottom * this.conf.canvasImageDataRowLength; - - // if(Debug.debugCanvas.enabled){ - // this._columnTest_dbgc(image, upper_top_corrected, upper_bottom_corrected, cols_a, res_top, false); - // this._columnTest_dbgc(image, lower_top_corrected, lower_bottom_corrected, cols_b, res_bottom, true); - // } else { - this._columnTest3_cross(image, upper_top_corrected, upper_bottom_corrected, cols_a, res_top, false); - this._columnTest3_cross(image, lower_top_corrected, lower_bottom_corrected, cols_b, res_bottom, true); - // } - - if (Debug.debug && Debug.debugArDetect){ - console.log("[EdgeDetect::findCandidates] candidates found -->", {res_top: res_top, res_bottom: res_bottom}); - } - - - return {res_top: res_top, res_bottom: res_bottom}; - + return {res_top: res_top, res_bottom: res_bottom}; } catch (e) { - console.log("[EdgeDetect::findCandidates] there was an error", e); + this.logger.log('error', 'debug', '[EdgeDetect::findCandidates] there was an error while finding candidates:', e); } } @@ -333,7 +323,7 @@ class EdgeDetect{ } } } catch (e) { - console.log("\n\nuwu fucky wucky:", e, "\n\n") + this.logger.log('error', 'debug', '[EdgeDetect::edgeDetect] There was an error:', e); } return { diff --git a/src/ext/lib/video-data/PageInfo.js b/src/ext/lib/video-data/PageInfo.js index d1343d2..e530fa9 100644 --- a/src/ext/lib/video-data/PageInfo.js +++ b/src/ext/lib/video-data/PageInfo.js @@ -22,8 +22,6 @@ class PageInfo { this.logger = logger; - console.log("this.logger:", logger, this.logger) - if (comms){ this.comms = comms; } @@ -77,7 +75,7 @@ class PageInfo { this.comms.unregisterVideo(video.id) video.destroy(); } catch (e) { - console.log("unabel to destroy video!", e) + this.logger.log('error', ['debug', 'init'], '[PageInfo::destroy] unable to destroy video! Error:', e); } } @@ -197,8 +195,7 @@ class PageInfo { } else { this.logger.log('info', 'videoRescan', "[PageInfo::rescan] found new video candidate:", video, "NOTE:: Video initialization starts here:\n--------------------------------\n") - v = new VideoData(video, this.settings, this, this.logger); - // console.log("[PageInfo::rescan] v is:", v) + v = new VideoData(video, this.settings, this); v.initArDetection(); this.videos.push(v); @@ -213,9 +210,6 @@ class PageInfo { // // if we're left withotu videos on the current page, we unregister the page. // if we have videos, we call register. - // if(Debug.debug) { - // console.log("[PageInfo::rescan] Comms:", this.comms, "\nvideos.length:", this.videos.length, "\nold video count:", oldVideoCount) - // } if (this.comms) { if (this.videos.length != oldVideoCount) { // only if number of videos changed, tho if (this.videos.length > 0) { diff --git a/src/ext/lib/video-data/VideoData.js b/src/ext/lib/video-data/VideoData.js index 15f404b..71528e2 100644 --- a/src/ext/lib/video-data/VideoData.js +++ b/src/ext/lib/video-data/VideoData.js @@ -5,14 +5,14 @@ import ArDetector from '../ar-detect/ArDetector'; class VideoData { - constructor(video, settings, pageInfo, logger){ + constructor(video, settings, pageInfo){ this.arSetupComplete = false; this.video = video; this.destroyed = false; this.settings = settings; this.pageInfo = pageInfo; this.extensionMode = pageInfo.extensionMode; - this.logger = logger; + this.logger = pageInfo.logger; this.vdid = (Math.random()*100).toFixed(); this.userCssClassName = `uw-fuck-you-and-do-what-i-tell-you_${this.vdid}`; @@ -40,15 +40,10 @@ class VideoData { this.logger.log('info', 'debug', "%c[VideoData::ctor] Initial resizer reset!", {background: '#afd', color: '#132'}); this.resizer.reset(); - - if (Debug.init) { - console.log("[VideoData::ctor] Created videoData with vdid", this.vdid,"\nextension mode:", this.extensionMode); - } + this.logger.log('info', ['debug', 'init'], '[VideoData::ctor] Created videoData with vdid', this.vdid, '\nextension mode:', this.extensionMode) this.pageInfo.initMouseActionHandler(this); - this.video.classList.add(this.userCssClassName); // this also needs to be applied BEFORE we initialize resizer! - } onVideoDimensionsChanged(mutationList, observer) { @@ -283,10 +278,6 @@ class VideoData { } isPlaying() { - // console.log("is playing? video:", this.video, "ctime:", this.video.currentTime, - // "paused/ended:", this.video.paused, this.video.ended, - // "is playing?", this.video && this.video.currentTime > 0 && !this.video.paused && !this.video.ended); - return this.video && this.video.currentTime > 0 && !this.video.paused && !this.video.ended; } } diff --git a/src/ext/lib/video-transform/Resizer.js b/src/ext/lib/video-transform/Resizer.js index 89fe7ac..7e3b5fb 100644 --- a/src/ext/lib/video-transform/Resizer.js +++ b/src/ext/lib/video-transform/Resizer.js @@ -40,7 +40,6 @@ class Resizer { this.resizerId = (Math.random(99)*100).toFixed(0); if (this.settings.active.pan) { - // console.log("can pan:", this.settings.active.miscSettings.mousePan.enabled, "(default:", this.settings.active.miscSettings.mousePan.enabled, ")") this.canPan = this.settings.active.miscSettings.mousePan.enabled; } else { this.canPan = false; @@ -192,7 +191,6 @@ class Resizer { } if (! this.video) { - // console.log("No video detected.") this.conf.destroy(); } @@ -215,14 +213,12 @@ class Resizer { var stretchFactors = this.scaler.calculateCrop(ar); if(! stretchFactors || stretchFactors.error){ - this.logger.log('error', 'debug', "[Resizer::setAr] failed to set AR due to problem with calculating crop. Error:", (stretchFactors ? stretchFactors.error : stretchFactors)); + this.logger.log('error', 'debug', `[Resizer::setAr] failed to set AR due to problem with calculating crop. Error:`, (stretchFactors ? stretchFactors.error : stretchFactors)); if (stretchFactors.error === 'no_video'){ this.conf.destroy(); } if (stretchFactors.error === 'illegal_video_dimensions') { - if(Debug.debug){ - console.log("[Resizer::setAr] Illegal video dimensions found. We will pause everything."); - } + this.loggger.log('error', 'debug', `[Resizer::setAr] Illegal video dimensions found. We will pause everything.`) } return; } @@ -269,7 +265,6 @@ class Resizer { } panHandler(event, forcePan) { - // console.log("this.conf.canPan:", this.conf.canPan) if (this.canPan || forcePan) { if(!this.conf.player || !this.conf.player.element) { return; diff --git a/src/ext/lib/video-transform/Scaler.js b/src/ext/lib/video-transform/Scaler.js index 4e899cf..38dacc6 100644 --- a/src/ext/lib/video-transform/Scaler.js +++ b/src/ext/lib/video-transform/Scaler.js @@ -120,16 +120,9 @@ class Scaler { actualHeight: 0, // height of the video (excluding letterbox) when