Reorganized stuff for content script & get it working (preliminary; performance issues on YT)

This commit is contained in:
Tamius Han 2018-12-31 01:03:07 +01:00
parent 20030f1679
commit 9c3ca3744b
24 changed files with 215 additions and 177 deletions

View File

@ -6,9 +6,4 @@ var ExtensionMode = Object.freeze({
Full: 2
});
var StretchMode = Object.freeze({
NO_STRETCH: 0,
BASIC: 1,
HYBRID: 2,
CONDITIONAL: 3
});
export default ExtensionMode;

View File

@ -5,27 +5,27 @@ var _bd_isFirefox = true;
var _bd_isChrome = false;
var _bd_isEdge = false; // we'll see if FF
// try{
// // todo: find something that works in firefox but not in edge (or vice-versa)
// // note that this function returns a promise! and is broken for some reason
// var browserinfo = browser.runtime.getBrowserInfo();
try{
// todo: find something that works in firefox but not in edge (or vice-versa)
// note that this function returns a promise! and is broken for some reason
var browserinfo = browser.runtime.getBrowserInfo();
// // we don't need to actually check because only firefox supports that.
// // if we're not on firefox, the above call will probably throw an exception anyway.
// // if browsers other than firefox start supporting that, well ... we'll also need to actually await for promise
// // that getBrowserInfo() returns to resolve.
// we don't need to actually check because only firefox supports that.
// if we're not on firefox, the above call will probably throw an exception anyway.
// if browsers other than firefox start supporting that, well ... we'll also need to actually await for promise
// that getBrowserInfo() returns to resolve.
// // if (Browser.name.toLowerCase().indexOf(firefox) !== -1 || Browser.vendor.toLowerCase().indexOf(mozilla) !== -1) {
// _bd_isFirefox = true;
// _bd_isEdge = false;
// // }
// if (Browser.name.toLowerCase().indexOf(firefox) !== -1 || Browser.vendor.toLowerCase().indexOf(mozilla) !== -1) {
_bd_isFirefox = true;
_bd_isEdge = false;
// }
// }
// catch (e) {
// if(Debug.debug) {
// console.info("[BrowserDetect] browser.runtime.getBrowserInfo() probably failed. This means we're probably not using firefox.", e)
// }
// };
}
catch (e) {
if(Debug.debug) {
console.info("[BrowserDetect] browser.runtime.getBrowserInfo() probably failed. This means we're probably not using firefox.", e)
}
};
try {
if(browser === undefined){ // This is a good sign we're in chrome or chromium-based browsers

View File

@ -1,4 +1,5 @@
import Debug from '../conf/Debug';
import PlayerData from './video-data/PlayerData'
class ActionHandler {

View File

@ -18,4 +18,6 @@ class KeyboardShortcutParser {
return shortcutCombo;
}
}
}
export default KeyboardShortcutParser;

View File

@ -1,7 +1,7 @@
import Debug from '../conf/Debug';
import currentBrowser from '../conf/BrowserDetect';
import ExtensionConf from '../conf/ExtensionConf';
import ExtensionMode from '../../common/enums/extension-mode';
class Settings {
@ -380,4 +380,4 @@ class Settings {
}
}
export default Settings;
export default Settings;

View File

@ -1,3 +1,11 @@
import Debug from '../../conf/Debug';
import EdgeDetect from './edge-detect/EdgeDetect';
import EdgeStatus from './edge-detect/enums/EdgeStatusEnum';
import EdgeDetectPrimaryDirection from './edge-detect/enums/EdgeDetectPrimaryDirectionEnum';
import EdgeDetectQuality from './edge-detect/enums/EdgeDetectQualityEnum';
import GuardLine from './GuardLine';
import DebugCanvas from './DebugCanvas';
class ArDetector {
constructor(videoData){
@ -34,7 +42,7 @@ class ArDetector {
if(Debug.debug || Debug.init) {
console.log(`[ArDetect::destroy] <arid:${this.arid}>`)
}
this.debugCanvas.destroy();
// this.debugCanvas.destroy();
this.stop();
}
@ -42,7 +50,7 @@ class ArDetector {
this.guardLine = new GuardLine(this);
this.edgeDetector = new EdgeDetect(this);
this.debugCanvas = new DebugCanvas(this);
// this.debugCanvas = new DebugCanvas(this);
if(Debug.debug || Debug.init) {
console.log("[ArDetect::setup] Starting autodetection setup. arid:", this.arid);
@ -169,7 +177,7 @@ class ArDetector {
}
if(Debug.debugCanvas.enabled){
this.debugCanvas.init({width: cwidth, height: cheight});
// this.debugCanvas.init({width: cwidth, height: cheight});
// DebugCanvas.draw("test marker","test","rect", {x:5, y:5}, {width: 5, height: 5});
}
@ -276,7 +284,7 @@ class ArDetector {
postFrameCheck(){
if(Debug.debugCanvas.enabled){
this.debugCanvas.update();
// this.debugCanvas.update();
}
}
@ -520,7 +528,7 @@ class ArDetector {
var image = this.context.getImageData(0, 0, this.canvas.width, this.canvas.height).data;
if(Debug.debugCanvas.enabled){
this.debugCanvas.setBuffer(image);
// this.debugCanvas.setBuffer(image);
}
//#region black level detection
@ -709,7 +717,7 @@ class ArDetector {
}
// console.log("SAMPLES:", blackbarSamples, "candidates:", edgeCandidates, "post:", edgePost,"\n\nblack level:", this.blackLevel, "tresh:", this.blackLevel + this.settings.active.arDetect.blackbarTreshold);
if(edgePost.status == "ar_known"){
if(edgePost.status == EdgeStatus.AR_KNOWN){
// zaznali smo rob — vendar pa moramo pred obdelavo še preveriti, ali ni "rob" slučajno besedilo. Če smo kot rob pofočkali
// besedilo, potem to ni veljaven rob. Razmerja stranic se zato ne bomo pipali.
@ -789,97 +797,95 @@ class ArDetector {
}
}
if(Debug.debug)
console.log("Loading: ArDetect");
var _ard_console_stop = "background: #000; color: #f41";
var _ard_console_start = "background: #000; color: #00c399";
// var textLineTest = function(image, row){
// // preverimo, če vrstica vsebuje besedilo na črnem ozadju. Če ob pregledu vrstice naletimo na veliko sprememb
// // iz črnega v ne-črno, potem obstaja možnost, da gledamo besedilo. Prisotnost take vrstice je lahko znak, da
// // zaznano razmerje stranic ni veljavno
// //
// // vrne 'true' če zazna text, 'false' drugače.
// //
// //
// // check if line contains any text. If line scan reveals a lot of changes from black to non-black there's a
// // chance we're looking at text on a black background. If we detect text near what we think is an edge of the
// // video, there's a good chance we're about to incorrectly adjust the aspect ratio.
// //
// // returns 'true' if text is detected, 'false' otherwise
var textLineTest = function(image, row){
// preverimo, če vrstica vsebuje besedilo na črnem ozadju. Če ob pregledu vrstice naletimo na veliko sprememb
// iz črnega v ne-črno, potem obstaja možnost, da gledamo besedilo. Prisotnost take vrstice je lahko znak, da
// zaznano razmerje stranic ni veljavno
//
// vrne 'true' če zazna text, 'false' drugače.
//
//
// check if line contains any text. If line scan reveals a lot of changes from black to non-black there's a
// chance we're looking at text on a black background. If we detect text near what we think is an edge of the
// video, there's a good chance we're about to incorrectly adjust the aspect ratio.
//
// returns 'true' if text is detected, 'false' otherwise
// var blackbarTreshold = this.blackLevel + this.settings.active.arDetect.blackbarTreshold;
// var nontextTreshold = this.canvas.width * this.settings.active.arDetect.textLineTest.nonTextPulse;
var blackbarTreshold = this.blackLevel + this.settings.active.arDetect.blackbarTreshold;
var nontextTreshold = this.canvas.width * this.settings.active.arDetect.textLineTest.nonTextPulse;
// var rowStart = (row * this.canvas.width) << 2;
// var rowEnd = rowStart + (this.canvas.width << 2);
var rowStart = (row * this.canvas.width) << 2;
var rowEnd = rowStart + (this.canvas.width << 2);
// var pulse = false;
// var currentPulseLength = 0, pulseCount = 0;
// var pulses = [];
// var longestBlack = 0;
var pulse = false;
var currentPulseLength = 0, pulseCount = 0;
var pulses = [];
var longestBlack = 0;
// preglejmo vrstico
// analyse the row
for(var i = rowStart; i < rowEnd; i+= 4){
if(pulse){
if(image[i] < blackbarTreshold || image[i+1] < blackbarTreshold || image[i+2] < blackbarTreshold){
// pulses.push(currentPulseLength);
pulseCount++;
pulse = false;
currentPulseLength = 0;
}
else{
currentPulseLength++;
// // preglejmo vrstico
// // analyse the row
// for(var i = rowStart; i < rowEnd; i+= 4){
// if(pulse){
// if(image[i] < blackbarTreshold || image[i+1] < blackbarTreshold || image[i+2] < blackbarTreshold){
// // pulses.push(currentPulseLength);
// pulseCount++;
// pulse = false;
// currentPulseLength = 0;
// }
// else{
// currentPulseLength++;
// če najdemo dovolj dolgo zaporedje ne-črnih točk, potem vrnemo 'false' — dobili smo legitimen rob
// if we find long enough uninterrupted line of non-black point, we fail the test. We found a legit edge.
if(currentPulseLength > nontextTreshold){
return false;
}
}
}
else{
if(image[i] > blackbarTreshold || image[i+1] > blackbarTreshold || image[i+2] > blackbarTreshold){
if(currentPulseLength > longestBlack){
longestBlack = currentPulseLength;
}
pulse = true;
currentPulseLength = 0;
}
else{
currentPulseLength++;
}
}
}
if(pulse){
pulseCount++;
// pulses.push(currentPulseLength);
}
// // če najdemo dovolj dolgo zaporedje ne-črnih točk, potem vrnemo 'false' — dobili smo legitimen rob
// // if we find long enough uninterrupted line of non-black point, we fail the test. We found a legit edge.
// if(currentPulseLength > nontextTreshold){
// return false;
// }
// }
// }
// else{
// if(image[i] > blackbarTreshold || image[i+1] > blackbarTreshold || image[i+2] > blackbarTreshold){
// if(currentPulseLength > longestBlack){
// longestBlack = currentPulseLength;
// }
// pulse = true;
// currentPulseLength = 0;
// }
// else{
// currentPulseLength++;
// }
// }
// }
// if(pulse){
// pulseCount++;
// // pulses.push(currentPulseLength);
// }
// pregledamo rezultate:
// analyse the results
// console.log("pulse test:\n\npulses:", pulseCount, "longest black:", longestBlack);
// // pregledamo rezultate:
// // analyse the results
// // console.log("pulse test:\n\npulses:", pulseCount, "longest black:", longestBlack);
// če smo zaznali dovolj pulzov, potem vrnemo res
// if we detected enough pulses, we return true
if(pulseCount > this.settings.active.arDetect.textLineTest.pulsesToConfirm){
return true;
}
// // če smo zaznali dovolj pulzov, potem vrnemo res
// // if we detected enough pulses, we return true
// if(pulseCount > this.settings.active.arDetect.textLineTest.pulsesToConfirm){
// return true;
// }
// če je najdaljša neprekinjena črta črnih pikslov širša od polovice širine je merilo za zaznavanje
// besedila rahlo milejše
// if the longest uninterrupted line of black pixels is wider than half the width, we use a more
// forgiving standard for determining if we found text
if( longestBlack > (this.canvas.width >> 1) &&
pulseCount > this.settings.active.arDetect.textLineTest.pulsesToConfirmIfHalfBlack ){
return true;
}
// // če je najdaljša neprekinjena črta črnih pikslov širša od polovice širine je merilo za zaznavanje
// // besedila rahlo milejše
// // if the longest uninterrupted line of black pixels is wider than half the width, we use a more
// // forgiving standard for determining if we found text
// if( longestBlack > (this.canvas.width >> 1) &&
// pulseCount > this.settings.active.arDetect.textLineTest.pulsesToConfirmIfHalfBlack ){
// return true;
// }
// če pridemo do sem, potem besedilo ni bilo zaznano
// if we're here, no text was detected
return false;
}
// // če pridemo do sem, potem besedilo ni bilo zaznano
// // if we're here, no text was detected
// return false;
// }
export default ArDetector;

View File

@ -116,7 +116,7 @@ class DebugCanvas {
}
DebugCanvasClasses = {
VIOLATION: {color: '#ff0000', colorRgb: [255, 00, 0], text: 'violation (general)'},
VIOLATION: {color: '#ff0000', colorRgb: [255, 0, 0], text: 'violation (general)'},
WARN: {color: '#d0d039', colorRgb: [208, 208, 57], text: 'lesser violation (general)'},
GUARDLINE_BLACKBAR: {color: '#3333FF', colorRgb: [51, 51, 255], text: 'guardline/blackbar (expected value)'},
GUARDLINE_IMAGE: {color: '#000088', colorRgb: [0, 0, 136], text: 'guardline/image (expected value)'},

View File

@ -1,3 +1,5 @@
import Debug from '../../conf/Debug';
class GuardLine {
// ardConf — reference to ArDetector that has current GuardLine instance
@ -302,3 +304,5 @@ class GuardLine {
return false;
}
}
export default GuardLine;

View File

@ -1,12 +1,7 @@
var EdgeDetectPrimaryDirection = {
VERTICAL: 0,
HORIZONTAL: 1
}
var EdgeDetectQuality = {
FAST: 0,
IMPROVED: 1
}
import Debug from '../../../conf/Debug';
import EdgeStatus from './enums/EdgeStatusEnum';
import EdgeDetectQuality from './enums/EdgeDetectQualityEnum';
import EdgeDetectPrimaryDirection from './enums/EdgeDetectPrimaryDirectionEnum';
class EdgeDetect{
@ -40,7 +35,7 @@ class EdgeDetect{
// }
} else {
bars = this.pillarTest(image) ? {status: 'ar_known'} : {status: 'ar_unknown'};
bars = this.pillarTest(image) ? {status: EdgeStatus.AR_KNOWN} : {status: EdgeStatus.AR_UNKNOWN};
}
return bars;
@ -290,7 +285,7 @@ class EdgeDetect{
edgesTop[0].distance : edgesBottom[0].distance;
return {
status: "ar_known",
status: EdgeStatus.AR_KNOWN,
blackbarWidth: blackbarWidth,
guardLineTop: edgesTop[0].distance,
guardLineBottom: edgesBottom[0].absolute,
@ -321,7 +316,7 @@ class EdgeDetect{
edgesTop[i].distance : edgesBottom[0].distance;
return {
status: "ar_known",
status: EdgeStatus.AR_KNOWN,
blackbarWidth: blackbarWidth,
guardLineTop: edgesTop[i].distance,
guardLineBottom: edgesBottom[0].absolute,
@ -349,7 +344,7 @@ class EdgeDetect{
edgesBottom[i].distance : edgesTop[0].distance;
return {
status: "ar_known",
status: EdgeStatus.AR_KNOWN,
blackbarWidth: blackbarWidth,
guardLineTop: edgesTop[0].distance,
guardLineBottom: edgesBottom[0].absolute,
@ -374,7 +369,7 @@ class EdgeDetect{
for(var edge of edgesBottom){
if(edge.count >= edgeDetectionTreshold)
return {
status: "ar_known",
status: EdgeStatus.AR_KNOWN,
blackbarWidth: edge.distance,
guardLineTop: null,
guardLineBottom: edge.bottom,
@ -388,7 +383,7 @@ class EdgeDetect{
for(var edge of edgesTop){
if(edge.count >= edgeDetectionTreshold)
return {
status: "ar_known",
status: EdgeStatus.AR_KNOWN,
blackbarWidth: edge.distance,
guardLineTop: edge.top,
guardLineBottom: null,
@ -401,7 +396,7 @@ class EdgeDetect{
}
// če pridemo do sem, nam ni uspelo nič. Razmerje stranic ni znano
// if we reach this bit, we have failed in determining aspect ratio. It remains unknown.
return {status: "ar_unknown"}
return {status: EdgeStatus.AR_UNKNOWN}
}
pillarTest(image){
@ -646,4 +641,6 @@ class EdgeDetect{
}
}
}
}
export default EdgeDetect;

View File

@ -0,0 +1,6 @@
var EdgeDetectPrimaryDirection = Object.freeze({
VERTICAL: 0,
HORIZONTAL: 1
});
export default EdgeDetectPrimaryDirection;

View File

@ -0,0 +1,6 @@
var EdgeDetectQuality = Object.freeze({
FAST: 0,
IMPROVED: 1
});
export default EdgeDetectQuality;

View File

@ -0,0 +1,6 @@
var EdgeStatus = Object.freeze({
AR_UNKNOWN: 0,
AR_KNOWN: 1,
});
export default EdgeStatus;

View File

@ -1,3 +1,7 @@
import Debug from '../../conf/Debug';
import VideoData from './VideoData';
import RescanReason from './enums/RescanReason';
if(Debug.debug)
console.log("Loading: PageInfo.js");
@ -444,8 +448,4 @@ class PageInfo {
}
}
var RescanReason = {
PERIODIC: 0,
URL_CHANGE: 1,
MANUAL: 2
}
export default PageInfo;

View File

@ -1,3 +1,6 @@
import Debug from '../../conf/Debug';
import ExtensionMode from '../../../common/enums/extension-mode'
if(Debug.debug)
console.log("Loading: PlayerData.js");
@ -381,3 +384,4 @@ class PlayerData {
}
}
export default PlayerData;

View File

@ -1,3 +1,8 @@
import Debug from '../../conf/Debug';
import PlayerData from './PlayerData';
import Resizer from '../video-transform/Resizer';
import ArDetector from '../ar-detect/ArDetector';
class VideoData {
constructor(video, settings, pageInfo){
@ -206,4 +211,6 @@ class VideoData {
return this.video && this.video.currentTime > 0 && !this.video.paused && !this.video.ended;
}
}
}
export default VideoData;

View File

@ -0,0 +1,7 @@
var RescanReason = Object.freeze({
PERIODIC: 0,
URL_CHANGE: 1,
MANUAL: 2
});
export default RescanReason;

View File

@ -1,7 +1,15 @@
import Debug from '../../conf/Debug';
import Scaler from './Scaler';
import Stretcher from './Stretcher';
import Zoom from './Zoom';
import PlayerData from '../video-data/PlayerData';
import ExtensionMode from '../../../common/enums/extension-mode';
import StretchMode from '../../../common/enums/stretch-mode';
if(Debug.debug)
console.log("Loading: Resizer.js");
class Resizer {
class Resizer {
constructor(videoData) {
this.conf = videoData;
@ -491,3 +499,5 @@ if(Debug.debug)
}
}
}
export default Resizer;

View File

@ -1,3 +1,5 @@
import Debug from '../../conf/Debug';
// računa velikost videa za približevanje/oddaljevanje
// does video size calculations for zooming/cropping
@ -145,4 +147,6 @@ class Scaler {
return videoDimensions;
}
}
}
export default Scaler;

View File

@ -1,9 +1,10 @@
import Debug from '../../conf/Debug';
// računa vrednosti za transform-scale (x, y)
// transform: scale(x,y) se uporablja za raztegovanje videa, ne pa za približevanje
// calculates values for transform scale(x, y)
// transform: scale(x,y) is used for stretching, not zooming.
class Stretcher {
// internal variables
@ -177,4 +178,6 @@ class Stretcher {
return stretchFactors;
}
}
}
export default Stretcher;

View File

@ -1,10 +1,9 @@
// računa približevanje ter računa/popravlja odmike videa
import Debug from '../../conf/Debug';
// računa približevanje ter računa/popravlja odmike videa
// calculates zooming and video offsets/panning
class Zoom {
// internal variables
// functions
constructor(videoData) {
this.scale = 1;
@ -59,4 +58,6 @@ class Zoom {
stretchFactors.xFactor *= this.scale;
stretchFactors.yFactor *= this.scale;
}
}
}
export default Zoom;

View File

@ -1,3 +1,11 @@
import Debug from './conf/Debug';
import BrowserDetect from './conf/BrowserDetect';
import ExtensionMode from '../common/enums/extension-mode'
import Settings from './lib/Settings';
import ActionHandler from './lib/ActionHandler';
import CommsClient from './lib/comms/CommsClient';
import PageInfo from './lib/video-data/PageInfo';
if(Debug.debug){
console.log("\n\n\n\n\n\n ——— Sᴛλʀᴛɪɴɢ Uʟᴛʀᴀɪɪʏ ———\n << ʟᴏᴀᴅɪɴɢ ᴍᴀɪɴ ꜰɪʟᴇ >>\n\n\n\n");
try {

View File

@ -19,37 +19,8 @@
"content_scripts": [{
"matches": ["*://*/*"],
"js": [
"js/conf/Debug.js",
"js/lib/enums.js",
"js/lib/BrowserDetect.js",
"js/conf/ExtensionConf.js",
"js/lib/ObjectCopy.js",
"js/lib/Settings.js",
"js/lib/Comms.js",
"js/lib/EdgeDetect.js",
"js/lib/GuardLine.js",
"js/modules/PageInfo.js",
"js/modules/DebugCanvas.js",
"js/modules/ArDetect.js",
"js/modules/Zoom.js",
"js/modules/Scaler.js",
"js/modules/Stretcher.js",
"js/modules/Resizer.js",
"js/lib/PlayerData.js",
"js/lib/VideoData.js",
"js/modules/ActionHandler.js",
"js/uw.js"
"js": [
"ext/uw.js"
],
"all_frames": true
}],