From 36863416771ce186e883dbc1a65acbc6fda24e0e Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Sun, 13 May 2018 21:05:11 +0200 Subject: [PATCH] rewrite finished to the point where extension doesn't outright crash. Nothing works, though. --- js/conf/Debug.js | 4 +- js/lib/EdgeDetect.js | 4 +- js/lib/PlayerData.js | 2 +- js/lib/VideoData.js | 9 +++- js/modules/ArDetect.js | 4 +- js/modules/DebugCanvas.js | 8 +-- js/modules/PageInfo.js | 42 ++++++++++----- js/modules/Resizer.js | 8 ++- js/uw.js | 104 +++++++++++++++----------------------- 9 files changed, 90 insertions(+), 95 deletions(-) diff --git a/js/conf/Debug.js b/js/conf/Debug.js index 1fbd447..7dd594b 100644 --- a/js/conf/Debug.js +++ b/js/conf/Debug.js @@ -1,6 +1,6 @@ // Set prod to true when releasing -_prod = true; -// _prod = false; +// _prod = true; +_prod = false; Debug = { debug: true, diff --git a/js/lib/EdgeDetect.js b/js/lib/EdgeDetect.js index 358d700..b2ce706 100644 --- a/js/lib/EdgeDetect.js +++ b/js/lib/EdgeDetect.js @@ -27,7 +27,7 @@ class EdgeDetect{ } findBars(image, sampleCols, direction = EdgeDetectPrimaryDirection.VERTICAL, quality = EdgeDetectQuality.IMPROVED, guardLineOut){ - var fastCandidates, edgeCandidates, bars, + var fastCandidates, edgeCandidates, bars; if (direction == EdgeDetectPrimaryDirection.VERTICAL) { fastCandidates = this.findCandidates(image, sampleCols, guardLine); @@ -230,7 +230,7 @@ class EdgeDetect{ }; } - edgePostprocess = function(edges){ + edgePostprocess(edges){ var edgesTop = []; var edgesBottom = []; var alignMargin = this.conf.context.height * ExtensionConf.arDetect.allowedMisaligned; diff --git a/js/lib/PlayerData.js b/js/lib/PlayerData.js index 437de85..e1a1665 100644 --- a/js/lib/PlayerData.js +++ b/js/lib/PlayerData.js @@ -99,7 +99,7 @@ class PlayerData { return; } - var isFullScreen = PlayerData.isFullScreen(); + var isFullScreen = this.isFullScreen(); var trustCandidateAfterGrows = 2; // if candidate_width or candidate_height increases in either dimensions this many // times, we say we found our player. (This number ignores weird elements) diff --git a/js/lib/VideoData.js b/js/lib/VideoData.js index fe32194..c07d538 100644 --- a/js/lib/VideoData.js +++ b/js/lib/VideoData.js @@ -3,7 +3,7 @@ class VideoData { constructor(video){ this.video = video; // todo: add ArDetect instance - this.arDetector = new ArDetector(video); + this.arDetector = new ArDetector(video); // this starts Ar detection. needs optional parameter that prevets ardetdctor from starting this.resizer = new Resizer(this); this.player = new PlayerData(this); @@ -27,5 +27,12 @@ class VideoData { this.resizer.setLastAr(lastAr); } + setAr(ar, lastAr){ + this.resizer.setAr(ar, lastAr); + } + + setStretchMode(stretchMode){ + this.resizer.setStretchMode(stretchMode); + } } \ No newline at end of file diff --git a/js/modules/ArDetect.js b/js/modules/ArDetect.js index e8d2295..b296d1c 100644 --- a/js/modules/ArDetect.js +++ b/js/modules/ArDetect.js @@ -18,7 +18,7 @@ class ArDetector { this.guardLine = new GuardLine(this); this.edgeDetector = new EdgeDetect(this); this.debugCanvas = new DebugCanvas(this); - setup(ExtensionConf.arDetect.hSamples, ExtensionConf.arDetect.vSamples); + this.setup(ExtensionConf.arDetect.hSamples, ExtensionConf.arDetect.vSamples); } setup(cwidth, cheight){ @@ -226,7 +226,7 @@ class ArDetector { } //#endregion - processAr = function(edges){ + processAr(edges){ if(Debug.debug && Debug.debugArDetect){ console.log("[ArDetect::_ard_processAr] processing ar. sample width:", this.canvas.width, "; sample height:", this.canvas.height, "; edge top:", edges.top); diff --git a/js/modules/DebugCanvas.js b/js/modules/DebugCanvas.js index 00691d7..2def108 100644 --- a/js/modules/DebugCanvas.js +++ b/js/modules/DebugCanvas.js @@ -3,7 +3,7 @@ class DebugCanvas { this.conf = ardConf; } - init = async function(canvasSize, canvasPosition){ + init(canvasSize, canvasPosition) { console.log("initiating DebugCanvas") var body = document.getElementsByTagName('body')[0]; @@ -34,18 +34,18 @@ class DebugCanvas { console.log("debug canvas is:", this.canvas, "context:", this.context) } - setBuffer = function(buffer) { + setBuffer(buffer) { // this.imageBuffer = buffer.splice(0); this.imageBuffer = new Uint8ClampedArray(buffer); } - trace = function(arrayIndex, colorClass){ + trace(arrayIndex, colorClass) { this.imageBuffer[arrayIndex ] = colorClass.colorRgb[0]; this.imageBuffer[arrayIndex+1] = colorClass.colorRgb[1]; this.imageBuffer[arrayIndex+2] = colorClass.colorRgb[2]; } - update(){ + update() { if(this.context && this.imageBuffer instanceof Uint8ClampedArray){ var idata = new ImageData(this.imageBuffer, this.canvas.width, this.canvas.height); this.putImageData(this.context, idata, 0, 0); diff --git a/js/modules/PageInfo.js b/js/modules/PageInfo.js index 1efc088..c3c7f19 100644 --- a/js/modules/PageInfo.js +++ b/js/modules/PageInfo.js @@ -6,19 +6,23 @@ class PageInfo { this.hasVideos = false; this.siteDisabled = false; this.videos = []; + + + this.rescan(); } + rescan(count){ + var vids = document.getElementsByTagName('video'); - rescan(){ - var videos = document.getElementsByTagName('video'); - - if(!videos || videos.length == 0){ + if(!vids || vids.length == 0){ this.hasVideos = false; return; } + debugger; + // add new videos - for(video of videos){ + for(var video of vids){ var existing = this.videos.find( (x) => { if (x == video.video) return x; @@ -30,20 +34,30 @@ class PageInfo { if(existing){ video.video = existing; } else { - videos.push( + this.videos.push( new VideoData(video) ); } } } -} + initAr(){ + for(var vd in this.videos){ + vd.initAr(); + } + } -var _pi_hasVideos = function(){ -// return true; - var videos = document.getElementsByTagName("video"); - if(videos.length == 0) - return false; + setAr(ar){ + // TODO: find a way to only change aspect ratio for one video + for(var vd in this.videos){ + vd.setAr(ar) + } + } -// if(videos[0].style.display == "none") // in this case ultrawidify doesn't even work -// return false; \ No newline at end of file + setStretchMode(sm){ + for(var vd in this.videos){ + vd.setStretchMode(ar) + } + } + +} diff --git a/js/modules/Resizer.js b/js/modules/Resizer.js index 6184584..e2d1be3 100644 --- a/js/modules/Resizer.js +++ b/js/modules/Resizer.js @@ -1,12 +1,10 @@ if(Debug.debug) console.log("Loading: Resizer.js"); - - var StretchMode = { - NO_STRETCH = 0, - CONDITIONAL = 1, - FULL = 2 + NO_STRETCH: 0, + CONDITIONAL: 1, + FULL: 2 } diff --git a/js/uw.js b/js/uw.js index 4301339..714149a 100644 --- a/js/uw.js +++ b/js/uw.js @@ -12,88 +12,64 @@ if(Debug.debug){ } } -class VideoManager { - refresh(){ - - } - -} - - -// load all settings from localStorage: - -async function main(){ +async function init(){ if(Debug.debug) console.log("[uw::main] loading configuration ..."); // load settings var isSlave = true; await Settings.init(isSlave); - var scpromise = SitesConf.init(); - var kbpromise = Keybinds.init(); + + // za sporočilca poslušamo v vsakem primeru, tudi če je razširitev na spletnem mestu onemogočena + // we listen for messages in any case, even if extension is disabled on current site. + browser.runtime.onMessage.addListener(receiveMessage); + + await SitesConf.init(); + // če je trenutno mesto onemogočeno, potem zaključimo na tem mestu + // if current site is disabled, we quit here - // reset current css stuff in GlobalVars - GlobalVars.correctedVideoDimensions.top = null; - GlobalVars.correctedVideoDimensions.left = null; - GlobalVars.correctedVideoDimensions.width = null; - GlobalVars.correctedVideoDimensions.height = null; - - GlobalVars.currentCss = {top: null, left: null}; - - // počakamo, da so nastavitve naložene - // wait for settings to load - await scpromise; - await kbpromise; - - // globalVars: lastAr type = original - GlobalVars.lastAr = {type: "original"}; - - if(Debug.debug) - console.log("[uw::main] configuration should be loaded now"); - // start autoar and setup everything - - - if(Debug.debug) - console.log("[uw::main] | document is ready. Starting ar script ..."); - if(! SitesConf.isEnabled(window.location.hostname)){ if(Debug.debug) console.log("[uw:main] | site", window.location.hostname, "is blacklisted."); return; } - - // if(SitesConf.isArEnabled(window.location.hostname)){ - // if(Debug.debug) - // console.log("[uw::main] Aspect ratio detection is enabled. Starting ArDetect"); - // ArDetect.arSetup(); - // } - // console.log("[uw::main] ExtensionConf:", ExtensionConf); - if(ExtensionConf.arDetect.mode == "blacklist"){ - if(Debug.debug) - console.log("[uw::main] Aspect ratio detection is enabled (mode=",ExtensionConf.arDetect.mode,"). Starting ArDetect"); - ArDetect.arSetup(); - } - else{ - if(Debug.debug) - console.log("[uw::main] Aspect ratio detection is disabled. Mode:", ExtensionConf.arDetect.mode); + + + await Keybinds.init(); + + if(Debug.debug) + console.log("[uw::main] configuration should be loaded now"); + + + // setup the extension + setup(); +} + +var pageInfo; + +async function setup(){ + + pageInfo = new PageInfo(); + + if(Debug.debug){ + console.log("[uw.js::setup] pageInfo initialized. Here's the object:", pageInfo); } - browser.runtime.onMessage.addListener(receiveMessage); - setInterval( ghettoOnChange, 100); - setInterval( ghettoUrlWatcher, 500); + // if(ExtensionConf.arDetect.mode == "blacklist"){ + // if(Debug.debug) + // console.log("[uw::main] Aspect ratio detection is enabled (mode=",ExtensionConf.arDetect.mode,"). Starting ArDetect"); + // ArDetect.arSetup(); + // } + // else{ + // if(Debug.debug) + // console.log("[uw::main] Aspect ratio detection is disabled. Mode:", ExtensionConf.arDetect.mode); + // } + - // ko se na ticevki zamenja video, console.log pravi da ultrawidify spremeni razmerje stranic. preglej element - // in pogled na predvajalnik pravita, da se to ni zgodilo. Iz tega sledi, da nam ticevka povozi css, ki smo ga - // vsilili. To super duper ni kul, zato uvedemo nekaj protiukrepov. - // - // when you change a video on youtube, console.log says that ultrawidify changes aspect ratio. inspect element - // and a look at youtube player, on the other hand, say this didn't happen. It seems that youtube overrides our - // css, and this is super duper uncool. Therefore, extra checks and measures. - setInterval( Resizer.antiCssOverride, 200); } @@ -272,5 +248,5 @@ function receiveMessage(message, sender, sendResponse) { // $(document).ready(function() { - main(); + init(); // });