Fixed a series of small bugs with autodetection.

This commit is contained in:
Tamius Han 2018-02-12 23:28:31 +01:00
parent f284c8b48a
commit 4daa054169
7 changed files with 96 additions and 146 deletions

View File

@ -81,6 +81,12 @@ Manually triggering aspect ratio change will suspend automatic aspect ratio dete
## Changelog ## Changelog
### v2.1.2
* Fixed some bugs with autodetection sometimes not working properly on Youtube.
Problem: there's this bit of code that keeps aspect ratio from changing when the difference between 'previous' and 'current' aspect ratio is too small. Unfortunately, the 'previous' value was _not_ updated on every aspect ratio switch for some reason. Also `ArDetect.init()` — for some reason — didn't always clean the 'previous' value even though it should.
### v2.1.1 (FF/Amo) ### v2.1.1 (FF/Amo)
* Fixed issue #20 — aspect ratio autodetection should now start on subsequent videos as well. * Fixed issue #20 — aspect ratio autodetection should now start on subsequent videos as well.

View File

@ -1,84 +0,0 @@
#!/bin/bash
# Takes all the files from 'src' and compiles it intro a single content script.
# Also compiles background scripts and settings pages.
#
# I'm using this over something like requirejs so my extension doesn't need even more
# dependencies.
#
# How does this work?
# 0. You're supposed to run this in the root directory of the extension.
# 1. see if file has any #include [filename] comments. If so, merge [filename] file
# into the current file (after checking if that file has any #include comments
# Compiled files are saved to /tmp.
#
# 2. After compiling file and its dependencies, check for presence of #location [directory]
# comments. [directory] is relative to the root directory of the extension
# 2.1 also check for #out, which defines filename.
#
# 3. Use '-nodebug' to remove all calls of console.log (to be implemented)
function flattenFileName {
echo $1 | sed 's/\//_/g'
}
fileList="";
function compileFile {
local file=$1
local flatfile=$(flattenFileName $file);
if [ -f /tmp/$flatfile ] ; then
return 0
fi;
fileList="$fileList $flatfile"
# let's take all the #included files and put them at the top
for f in $(grep "#include" $file | awk '{print $NF}') ; do
local lf=$f
fflat=$(flattenFileName $lf);
compileFile $lf
printf "\n//BEGIN included from $lf\n" >> /tmp/$flatfile
grep -Ev "#include|#location|#out" /tmp/$fflat >> /tmp/$flatfile
printf "//END included from $lf\n\n" >> /tmp/$flatfile
done
grep -Ev "#include|#location|#out" $file >> /tmp/$flatfile
}
cd src;
for file in *.js ; do
echo "main loop, we're looking at this file: $file"
compileFile $file
echo "Files compiled. Moving to location (if specified)"
outFile=$file
if grep -q "#out" $file ; then
outFile=$( grep "#out" $file | awk '{print $NF}')
fi
if grep -q "#location" $file ; then
location=$( grep "#location" $file | awk '{print $NF}')
echo "File will be saved to $location as $outFile"
cd ..
echo "// Autogenerated using buildext. This file should not be modified — modify source files instead." > $location/$outFile
grep -Ev "#location|#include|#out" /tmp/$(flattenFileName $file) >> $location/$outFile
cd src
fi
done
# perform cleanup
for file in $fileList ; do
rm /tmp/$file
done

View File

@ -1,12 +1,12 @@
// Set prod to true when releasing // Set prod to true when releasing
// _prod = true; _prod = true;
_prod = false; // _prod = false;
Debug = { Debug = {
debug: true, debug: true,
keyboard: true, keyboard: true,
debugResizer: true, debugResizer: true,
debugArDetect: false, debugArDetect: true,
debugStorage: true, debugStorage: true,
showArDetectCanvas: false, showArDetectCanvas: false,
flushStoredSettings: false flushStoredSettings: false

View File

@ -3,6 +3,13 @@
* @author Keith Henry <keith.henry@evolutionjobs.co.uk> * @author Keith Henry <keith.henry@evolutionjobs.co.uk>
* @license MIT */ * @license MIT */
(function () { (function () {
// before we start: don't do shit in browsers that aren't Google Chrome.
// We might need to modify this for use in IE at a later date tho
if(chrome === undefined)
return;
'use strict'; 'use strict';
/** Wrap a function with a callback with a Promise. /** Wrap a function with a callback with a Promise.

View File

@ -4,9 +4,6 @@ if(Debug.debug)
var _ard_console_stop = "background: #000; color: #f41"; var _ard_console_stop = "background: #000; color: #f41";
var _ard_console_start = "background: #000; color: #00c399"; var _ard_console_start = "background: #000; color: #00c399";
// global-ish variables
var _ard_oldAr;
var _ard_currentAr; var _ard_currentAr;
@ -100,10 +97,6 @@ var _arSetup = function(cwidth, cheight){
canvas.width = canvasWidth; canvas.width = canvasWidth;
canvas.height = canvasHeight; canvas.height = canvasHeight;
// init oldAr to physical <video> aspect ratio
_ard_oldAr = vid.videoWidth / vid.videoHeight;
_ard_currentAr = _ard_oldAr;
try{ try{
// determine where to sample // determine where to sample
@ -139,6 +132,8 @@ var _arSetup = function(cwidth, cheight){
} }
this._forcehalt = false; this._forcehalt = false;
// if we're restarting ArDetect, we need to do this in order to force-recalculate aspect ratio
GlobalVars.lastAr = {type: "auto", ar: null};
_ard_vdraw(vid, context, canvasWidth, canvasHeight, false); _ard_vdraw(vid, context, canvasWidth, canvasHeight, false);
}; };
@ -181,47 +176,52 @@ var _ard_processAr = function(video, width, height, edge_h, edge_w, fallbackMode
// poglejmo, če se je razmerje stranic spremenilo // poglejmo, če se je razmerje stranic spremenilo
// check if aspect ratio is changed: // check if aspect ratio is changed:
var arDiff = trueAr - _ard_oldAr;
if (arDiff < 0)
arDiff = -arDiff;
var arDiff_percent = arDiff / trueAr; if(GlobalVars.lastAr.type == "auto" && GlobalVars.lastAr.ar != null){
// spremembo lahko zavrnemo samo, če uporabljamo avtomatski način delovanja in če smo razmerje stranic
// že nastavili.
//
// we can only deny aspect ratio changes if we use automatic mode and if aspect ratio was set from here.
// ali je sprememba v mejah dovoljenega? Če da -> fertik var arDiff = trueAr - GlobalVars.lastAr.ar;
// is ar variance within acceptable levels? If yes -> we done if (arDiff < 0)
if(Debug.debug && Debug.debugArDetect) arDiff = -arDiff;
console.log("%c[ArDetect::_ard_processAr] new aspect ratio varies from the old one by this much:\n","color: #aaf","old Ar", _ard_oldAr, "current ar", trueAr, "arDiff (absolute):",arDiff,"ar diff (relative to new ar)", arDiff_percent);
if (arDiff < trueAr * Settings.arDetect.allowedArVariance){ var arDiff_percent = arDiff / trueAr;
// ali je sprememba v mejah dovoljenega? Če da -> fertik
// is ar variance within acceptable levels? If yes -> we done
if(Debug.debug && Debug.debugArDetect) if(Debug.debug && Debug.debugArDetect)
console.log("%c[ArDetect::_ard_processAr] aspect ratio change denied — diff %:", "background: #740; color: #fa2", arDiff_percent) console.log("%c[ArDetect::_ard_processAr] new aspect ratio varies from the old one by this much:\n","color: #aaf","old Ar", GlobalVars.lastAr.ar, "current ar", trueAr, "arDiff (absolute):",arDiff,"ar diff (relative to new ar)", arDiff_percent);
return;
if (arDiff < trueAr * Settings.arDetect.allowedArVariance){
if(Debug.debug && Debug.debugArDetect)
console.log("%c[ArDetect::_ard_processAr] aspect ratio change denied — diff %:", "background: #740; color: #fa2", arDiff_percent)
return;
}
else if(Debug.debug && Debug.debugArDetect){
console.log("%c[ArDetect::_ard_processAr] aspect ratio change accepted — diff %:", "background: #153; color: #4f9", arDiff_percent)
}
} }
else if(Debug.debug && Debug.debugArDetect){
console.log("%c[ArDetect::_ard_processAr] aspect ratio change accepted — diff %:", "background: #153; color: #4f9", arDiff_percent)
}
// če je sprememba več od dovoljenega, spremeni razmerje stranic. Stvari se razlikujejo glede na to, ali smo v fullscreen ali ne
// if change is greater than allowed, change the aspect ratio. Whether we do that depends on whether we're in fullscreen.
// if( PlayerDetect.isFullScreen() ){
if(Debug.debug)
console.log("[ArDetect::_ard_processAr] attempting to fix aspect ratio. New aspect ratio: ", trueAr);
_ard_oldAr = trueAr; if(Debug.debug)
Resizer.setAr(trueAr); console.log("[ArDetect::_ard_processAr] attempting to fix aspect ratio. New aspect ratio: ", trueAr);
// }
// else{
// // če nismo v fullscreen, potem preverimo, ali naša stran dovoljuje ne-fs?
// // first, we'll check if our site allows for non-fs autoar detection
// if( SitesConf.nonfsArDetectEnabled() ){
// _ard_oldAr = trueAr;
// Resizer.setAr_nonfs(trueAr);
// }
// }
// POMEMBNO: GlobalVars.lastAr je potrebno nastaviti šele po tem, ko kličemo _res_setAr(). _res_setAr() predvideva,
// da želimo nastaviti statično (type: 'static') razmerje stranic — tudi, če funkcijo kličemo tu oz. v ArDetect.
//
// IMPORTANT NOTE: GlobalVars.lastAr needs to be set after _res_setAr() is called, as _res_setAr() assumes we're
// setting a static aspect ratio (even if the function is called from here or ArDetect).
Resizer.setAr(trueAr);
GlobalVars.lastAr = {type: "auto", ar: trueAr};
} }
var _ard_vdraw = function (vid, context, w, h, conf){ var _ard_vdraw = function (vid, context, w, h, conf){
try{
if(this._forcehalt) if(this._forcehalt)
return; return;
@ -232,7 +232,7 @@ var _ard_vdraw = function (vid, context, w, h, conf){
// if(Debug.debug) // if(Debug.debug)
// Settings.arDetect.timer_playing = 1000; // how long is the pause between two executions — 33ms ~ 30fps // 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"){ if(vid == null || vid.paused || vid.ended || Status.arStrat != "auto"){
// we slow down if paused, no detection // we slow down if paused, no detection
_ard_timer = setTimeout(_ard_vdraw, Settings.arDetect.timer_paused, vid, context, w, h); _ard_timer = setTimeout(_ard_vdraw, Settings.arDetect.timer_paused, vid, context, w, h);
return false; return false;
@ -314,6 +314,7 @@ var _ard_vdraw = function (vid, context, w, h, conf){
// _ard_processAr(vid, w, h); // _ard_processAr(vid, w, h);
Resizer.reset(); Resizer.reset();
GlobalVars.lastAr = {type: "auto", ar: null};
_ard_timer = setTimeout(_ard_vdraw, Settings.arDetect.timer_playing, 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; return;
@ -550,6 +551,14 @@ var _ard_vdraw = function (vid, context, w, h, conf){
_ard_timer = setTimeout(_ard_vdraw, Settings.arDetect.timer_playing, vid, context, w, h); _ard_timer = setTimeout(_ard_vdraw, Settings.arDetect.timer_playing, vid, context, w, h);
}
catch(e){
if(Debug.debug)
console.log("%c[ArDetect::_ard_vdraw] vdraw has crashed for some reason ???. Error here:", "color: #000; background: #f80", e);
_ard_timer = setTimeout(_ard_vdraw, Settings.arDetect.timer_playing, vid, context, w, h);
}
} }
var _ard_stop = function(){ var _ard_stop = function(){

View File

@ -85,27 +85,31 @@ var _res_legacyAr = function(action){
ar = GlobalVars.playerDimensions.width / GlobalVars.playerDimensions.height; ar = GlobalVars.playerDimensions.width / GlobalVars.playerDimensions.height;
} }
// POMEMBNO: GlobalVars.lastAr je potrebno nastaviti šele po tem, ko kličemo _res_setAr(). _res_setAr() predvideva,
// da želimo nastaviti statično (type: 'static') razmerje stranic — tudi, če funkcijo kličemo tu oz. v ArDetect.
//
// IMPORTANT NOTE: GlobalVars.lastAr needs to be set after _res_setAr() is called, as _res_setAr() assumes we're
// setting a static aspect ratio (even if the function is called from here or ArDetect).
var fileAr = vid.videoWidth / vid.videoHeight; var fileAr = vid.videoWidth / vid.videoHeight;
GlobalVars.lastAr = {type: "legacy", action: action};
if (action == "fitw"){
if(action == "fitw"){
_res_setAr( ar > fileAr ? ar : fileAr); _res_setAr( ar > fileAr ? ar : fileAr);
return;
} }
if(action == "fith"){ else if(action == "fith"){
_res_setAr( ar < fileAr ? ar : fileAr); _res_setAr( ar < fileAr ? ar : fileAr);
return;
} }
if(action == "reset"){ else if(action == "reset"){
GlobalVars.lastAr = {type: "original"};
_res_setAr(fileAr); _res_setAr(fileAr);
GlobalVars.lastAr = {type: "original"};
return; return;
} }
if(action == "autoar" || action == "auto"){ else if(action == "autoar" || action == "auto"){
GlobalVars.lastAr = {type: "auto"}; GlobalVars.lastAr = {type: "auto", ar: null};
ArDetect.init(); ArDetect.init();
} }
GlobalVars.lastAr = {type: "legacy", action: action};
} }
var _res_setAr = function(ar, playerDimensions){ var _res_setAr = function(ar, playerDimensions){
@ -121,7 +125,7 @@ var _res_setAr = function(ar, playerDimensions){
if(vid == null || vid==undefined || vid.videoWidth == 0 || vid.videoHeight == 0){ if(vid == null || vid==undefined || vid.videoWidth == 0 || vid.videoHeight == 0){
if(Debug.debug) if(Debug.debug)
console.log("[Resizer::_res_setAr] You thought there is a video, didn't you? Tricked you. Never would be.", vid); console.log("[Resizer::_res_setAr] I lied. Tricked you! You thought there is a video, didn't you? Never would be.", vid); // of course that's thorin reference -> https://youtu.be/OY5gGkeQn1c?t=1m20s
return; return;
} }
} }
@ -358,7 +362,7 @@ var _res_antiCssOverride = function(){
if(Debug.debug){ if(Debug.debug){
console.log("[Resizer::_res_antiCssOverride] SOMEBODY TOUCHED MA SPAGHETT (our CSS got overriden, restoring our css)"); console.log("[Resizer::_res_antiCssOverride] SOMEBODY TOUCHED MA SPAGHETT (our CSS got overriden, restoring our css)");
} }
this.restore(); _res_restore();
return; return;
} }
stuffChecked++; stuffChecked++;
@ -368,16 +372,16 @@ var _res_antiCssOverride = function(){
if(Debug.debug){ if(Debug.debug){
console.log("[Resizer::_res_antiCssOverride] SOMEBODY TOUCHED MA SPAGHETT (our CSS got overriden, restoring our css)"); console.log("[Resizer::_res_antiCssOverride] SOMEBODY TOUCHED MA SPAGHETT (our CSS got overriden, restoring our css)");
} }
this.restore(); _res_restore();
return; return;
} }
stuffChecked++; stuffChecked++;
} }
if(stuffChecked == stuffToCheck){ if(stuffChecked == stuffToCheck){
if(Debug.debug){ // if(Debug.debug){
console.log("[Resizer::_res_antiCssOverride] My spaghett rests untouched. (nobody overrode our CSS, doing nothing)"); // console.log("[Resizer::_res_antiCssOverride] My spaghett rests untouched. (nobody overrode our CSS, doing nothing)");
} // }
return; return;
} }
} }
@ -401,6 +405,14 @@ var _res_restore = function(){
else if(GlobalVars.lastAr.type == "original"){ else if(GlobalVars.lastAr.type == "original"){
_res_legacyAr("reset"); _res_legacyAr("reset");
} }
else if(GlobalVars.lastAr.type == "auto"){
// do same as static, except keep lastAr to 'auto'. If we're here, this means video probably changed
// and there's something broken that prevents AR from working properly
// var storeLastAr = {type: GlobalVars.lastAr.type, ar: GlobalVars.lastAr.ar};
// _res_setAr(GlobalVars.lastAr.ar);
// GlobalVars.lastAr = storeLastAr;
ArDetect.init();
}
} }
var _res_reset = function(){ var _res_reset = function(){

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Ultrawidify", "name": "Ultrawidify",
"version": "2.1.1", "version": "2.1.2",
"icons": { "icons": {
"32":"res/icons/uw-32.png", "32":"res/icons/uw-32.png",