ultrawidify/src/ext/lib/video-data/PageInfo.js

519 lines
13 KiB
JavaScript
Raw Normal View History

import Debug from '../../conf/Debug';
import VideoData from './VideoData';
import RescanReason from './enums/RescanReason';
2019-03-10 23:27:50 +01:00
import AspectRatio from '../../../common/enums/aspect-ratio.enum';
2017-12-30 02:58:24 +01:00
if(Debug.debug)
console.log("Loading: PageInfo.js");
2019-02-19 21:10:49 +01:00
class PageInfo {
2018-11-02 02:52:01 +01:00
constructor(comms, settings, extensionMode){
this.hasVideos = false;
this.siteDisabled = false;
this.videos = [];
this.settings = settings;
this.actionHandlerInitQueue = [];
this.lastUrl = window.location.href;
2018-11-02 02:52:01 +01:00
this.extensionMode = extensionMode;
2019-07-03 21:03:12 +02:00
this.readOnly = readOnly;
2019-07-03 21:03:12 +02:00
if (comms){
this.comms = comms;
}
this.rescan(RescanReason.PERIODIC);
this.scheduleUrlCheck();
2018-09-21 00:26:08 +02:00
this.currentZoomScale = 1;
}
destroy() {
if(Debug.debug || Debug.init){
console.log("[PageInfo::destroy] destroying all videos!")
}
if(this.rescanTimer){
clearTimeout(this.rescanTimer);
}
for (var video of this.videos) {
this.comms.unregisterVideo(video.id)
video.destroy();
}
try {
playerStyleString = this.settings.active.sites[window.location.host].css;
if (playerStyleString) {
this.comms.sendMessage({
cmd: 'remove-css',
cssString: playerStyleString
});
}
} catch (e) {
// do nothing. It's ok if there's no special settings for the player element
}
}
2018-06-15 00:33:10 +02:00
reset() {
for(var video of this.videos) {
2018-06-15 00:33:10 +02:00
video.destroy();
}
this.rescan(RescanReason.MANUAL);
}
initMouseActionHandler(videoData) {
if (this.actionHandler) {
this.actionHandler.registerHandleMouse(videoData);
} else {
this.actionHandlerInitQueue.push(videoData);
}
}
setActionHandler(actionHandler) {
this.actionHandler = actionHandler;
for (var item of this.actionHandlerInitQueue) {
this.actionHandler.registerHandleMouse(item);
}
this.actionHandlerInitQueue = [];
}
getVideos(host) {
if (this.settings.active.sites[host]
&& this.settings.active.sites[host].DOM
&& this.settings.active.sites[host].DOM.video
&& this.settings.active.sites[host].DOM.video.manual
&& this.settings.active.sites[host].DOM.video.querySelector){
const videos = document.querySelectorAll(this.settings.active.sites[host].DOM.video.querySelector);
if (videos.length) {
return videos;
}
}
return document.getElementsByTagName('video');
}
rescan(rescanReason){
const oldVideoCount = this.videos.length;
try{
var vids = this.getVideos(window.location.host);
if(!vids || vids.length == 0){
this.hasVideos = false;
if(rescanReason == RescanReason.PERIODIC){
2019-06-14 21:53:48 +02:00
if (Debug.debug && Debug.videoRescan && Debug.periodic) {
console.log("[PageInfo::rescan] Scheduling normal rescan:")
}
this.scheduleRescan(RescanReason.PERIODIC);
}
return;
}
// add new videos
this.hasVideos = false;
var videoExists = false;
var video, v;
for (video of vids) {
// če najdemo samo en video z višino in širino, to pomeni, da imamo na strani veljavne videe
// če trenutni video nima definiranih teh vrednostih, preskočimo vse nadaljnja preverjanja
// <===[:::::::]===>
// if we find even a single video with width and height, that means the page has valid videos
// if video lacks either of the two properties, we skip all further checks cos pointless
if(video.offsetWidth && video.offsetHeight){
this.hasVideos = true;
} else {
continue;
}
videoExists = false;
for (v of this.videos) {
if (v.destroyed) {
continue; //TODO: if destroyed video is same as current video, copy aspect ratio settings to current video
}
if (v.video == video) {
videoExists = true;
break;
}
}
if (videoExists) {
continue;
} else {
if (Debug.debug && Debug.periodic && Debug.videoRescan) {
console.log("[PageInfo::rescan] found new video candidate:", video, "NOTE:: Video initialization starts here:\n--------------------------------\n")
}
2018-09-21 00:26:08 +02:00
v = new VideoData(video, this.settings, this);
// console.log("[PageInfo::rescan] v is:", v)
v.initArDetection();
this.videos.push(v);
if(Debug.debug && Debug.periodic && Debug.videoRescan){
console.log("[PageInfo::rescan] END VIDEO INITIALIZATION\n\n\n-------------------------------------\nvideos[] is now this:", this.videos,"\n\n\n\n\n\n\n\n")
}
}
}
this.removeDestroyed();
// če smo ostali brez videev, potem odregistriraj stran.
// če nismo ostali brez videev, potem registriraj stran.
//
// 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) {
this.comms.registerVideo({host: window.location.host, location: window.location});
} else {
this.comms.unregisterVideo({host: window.location.host, location: window.location});
}
}
}
} catch(e) {
// če pride do zajeba, potem lahko domnevamo da na strani ni nobenega videa. Uničimo vse objekte videoData
// da preprečimo večkratno inicializacijo. Če smo se z našim ugibom zmotili, potem se bodo vsi videi ponovno
// našli ob naslednjem preiskovanju
//
// if we encounter a fuckup, we can assume that no videos were found on the page. We destroy all videoData
// objects to prevent multiple initalization (which happened, but I don't know why). No biggie if we destroyed
// videoData objects in error — they'll be back in the next rescan
if (Debug.debug) {
console.log("rescan error: — destroying all videoData objects",e);
}
for (const v of this.videos) {
v.destroy();
}
return;
}
if(rescanReason == RescanReason.PERIODIC){
this.scheduleRescan(RescanReason.PERIODIC);
}
}
removeDestroyed(){
this.videos = this.videos.filter( vid => vid.destroyed === false);
}
scheduleRescan(rescanReason){
if(rescanReason != RescanReason.PERIODIC){
this.rescan(rescanReason);
return;
}
try{
if(this.rescanTimer){
clearTimeout(this.rescanTimer);
}
var ths = this;
2019-06-14 21:53:48 +02:00
this.rescanTimer = setTimeout(function(rescanReason){
ths.rescanTimer = null;
2019-06-14 21:53:48 +02:00
ths.rescan(rescanReason);
ths = null;
2019-06-14 21:53:48 +02:00
}, this.settings.active.pageInfo.timeouts.rescan, RescanReason.PERIODIC)
} catch(e) {
if(Debug.debug){
console.log("[PageInfo::scheduleRescan] scheduling rescan failed. Here's why:",e)
}
}
}
scheduleUrlCheck() {
try{
if(this.urlCheckTimer){
clearTimeout(this.urlCheckTimer);
}
var ths = this;
this.rescanTimer = setTimeout(function(){
ths.rescanTimer = null;
ths.ghettoUrlCheck();
ths = null;
}, this.settings.active.pageInfo.timeouts.urlCheck)
2019-06-14 21:53:48 +02:00
} catch(e){
if(Debug.debug){
2019-06-14 21:53:48 +02:00
console.error("[PageInfo::scheduleUrlCheck] scheduling URL check failed. Here's why:",e)
}
}
}
ghettoUrlCheck() {
if (this.lastUrl != window.location.href){
2019-06-14 21:53:48 +02:00
if(Debug.debug && Debug.periodic){
console.log("[PageInfo::ghettoUrlCheck] URL has changed. Triggering a rescan!");
}
this.rescan(RescanReason.URL_CHANGE);
this.lastUrl = window.location.href;
}
2018-07-12 23:27:10 +02:00
this.scheduleUrlCheck();
}
initArDetection(playingOnly){
if (playingOnly) {
for(var vd of this.videos){
if(vd.isPlaying()) {
vd.initArDetection();
}
}
return;
} else {
for(var vd of this.videos){
vd.initArDetection();
}
}
}
// to je treba klicat ob menjavi zavihkov
// these need to be called on tab switch
pauseProcessing(playingOnly){
if (playingOnly) {
for(var vd of this.videos){
if (vd.isPlaying()) {
vd.pause();
}
}
} else {
for(var vd of this.videos){
vd.pause();
}
}
}
resumeProcessing(resumeAutoar = false, playingOnly = false){
if (playingOnly) {
for(var vd of this.videos){
if (vd.isPlaying()) {
vd.resume();
if(resumeAutoar){
vd.resumeAutoAr();
}
}
}
} else {
for(var vd of this.videos){
vd.resume();
if(resumeAutoar){
vd.resumeAutoAr();
}
}
}
}
startArDetection(playingOnly){
if (Debug.debug) {
console.log('[PageInfo::startArDetection()] starting automatic ar detection!')
}
if (playingOnly) {
for(var vd of this.videos){
if (video.isPlaying()) {
vd.startArDetection();
}
}
} else {
for(var vd of this.videos){
vd.startArDetection();
}
}
}
stopArDetection(playingOnly){
if (playingOnly) {
for(var vd of this.videos){
if (vd.isPlaying()) {
vd.stopArDetection();
}
}
} else {
for(var vd of this.videos){
vd.stopArDetection();
}
}
}
setAr(ar, playingOnly){
if (Debug.debug) {
console.log('[PageInfo::setAr] aspect ratio:', ar, "playing only?", playingOnly)
}
2019-03-10 23:27:50 +01:00
if (ar.type !== AspectRatio.Automatic) {
this.stopArDetection(playingOnly);
} else {
if (Debug.debug) {
console.log('[PageInfo::setAr] aspect ratio is auto');
}
try {
for (var vd of this.videos) {
if (!playingOnly || vd.isPlaying()) {
vd.resetLastAr();
}
}
} catch (e) {
console.log("???", e);
}
this.initArDetection(playingOnly);
this.startArDetection(playingOnly);
return;
}
2018-09-14 00:10:57 +02:00
// TODO: find a way to only change aspect ratio for one video
2019-03-10 23:27:50 +01:00
if (ar === AspectRatio.Reset) {
2018-09-14 00:10:57 +02:00
for (var vd of this.videos) {
if (!playingOnly || vd.isPlaying()) {
vd.resetAr();
}
2018-09-14 00:10:57 +02:00
}
} else {
for (var vd of this.videos) {
if (!playingOnly || vd.isPlaying()) {
vd.setAr(ar)
}
2018-09-14 00:10:57 +02:00
}
}
}
2017-12-30 02:58:24 +01:00
setvideoAlignment(videoAlignment, playingOnly) {
if (playingOnly) {
for(var vd of this.videos) {
if (vd.isPlaying()) {
vd.setvideoAlignment(videoAlignment)
}
}
} else {
for(var vd of this.videos) {
vd.setvideoAlignment(videoAlignment)
}
}
}
setPanMode(mode, playingOnly) {
if (playingOnly) {
for(var vd of this.videos) {
if (vd.isPlaying()) {
vd.setPanMode(mode);
}
}
} else {
for(var vd of this.videos) {
vd.setPanMode(mode);
}
2018-09-13 23:47:20 +02:00
}
}
restoreAr(playingOnly) {
if (playingOnly) {
for(var vd of this.videos){
if (vd.isPlaying()) {
vd.restoreAr();
}
}
} else {
for(var vd of this.videos){
vd.restoreAr();
}
}
}
setStretchMode(sm, playingOnly){
2018-05-27 21:41:08 +02:00
// TODO: find a way to only change aspect ratio for one video
if (playingOnly) {
for(var vd of this.videos){
if (vd.isPlaying()) {
vd.setStretchMode(sm)
}
}
} else {
for(var vd of this.videos){
vd.setStretchMode(sm)
}
}
}
setZoom(zoomLevel, no_announce, playingOnly) {
if (playingOnly) {
for(var vd of this.videos) {
if (vd.isPlaying()) {
vd.setZoom(zoomLevel, no_announce);
}
}
} else {
for(var vd of this.videos) {
vd.setZoom(zoomLevel, no_announce);
}
}
}
zoomStep(step, playingOnly) {
for(var vd of this.videos){
if (!playingOnly || vd.isPlaying()) {
vd.zoomStep(step);
}
}
}
2018-09-21 00:26:08 +02:00
markPlayer(name, color) {
for (var vd of this.videos) {
vd.markPlayer(name,color);
}
}
unmarkPlayer() {
for (var vd of this.videos) {
vd.unmarkPlayer();
}
}
2018-09-21 00:26:08 +02:00
announceZoom(scale) {
if (this.announceZoomTimeout) {
clearTimeout(this.announceZoom);
}
this.currentZoomScale = scale;
const ths = this;
this.announceZoomTimeout = setTimeout(() => ths.comms.announceZoom(scale), this.settings.active.zoom.announceDebounce);
}
2019-02-16 01:19:29 +01:00
setManualTick(manualTick) {
for(var vd of this.videos) {
vd.setManualTick();
}
}
tick() {
for(var vd of this.videos) {
vd.tick();
}
}
sendPerformanceUpdate(performanceUpdate) {
if(this.comms) {
this.comms.sendPerformanceUpdate(performanceUpdate);
}
}
2018-09-21 00:26:08 +02:00
requestCurrentZoom() {
this.comms.announceZoom(this.currentZoomScale);
}
2019-06-02 23:54:32 +02:00
setKeyboardShortcutsEnabled(state) {
this.actionHandler.setKeybordLocal(state);
}
}
export default PageInfo;