From 9ba1afebee033af4b9616feaf479527efcd90c2b Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Tue, 28 Jan 2020 23:34:36 +0100 Subject: [PATCH] Use optional chaining --- src/ext/lib/Settings.js | 6 ++-- src/ext/lib/video-data/PageInfo.js | 7 ++-- src/ext/lib/video-data/PlayerData.js | 29 ++++++--------- src/ext/lib/video-data/VideoData.js | 11 +++--- src/ext/lib/video-transform/Resizer.js | 50 +++++++++++++------------- src/ext/uw-bg.js | 14 +++----- 6 files changed, 49 insertions(+), 68 deletions(-) diff --git a/src/ext/lib/Settings.js b/src/ext/lib/Settings.js index 47b091b..30110da 100644 --- a/src/ext/lib/Settings.js +++ b/src/ext/lib/Settings.js @@ -555,7 +555,7 @@ class Settings { } getDefaultStretchMode(site) { - if (site && this.active.sites[site] && this.active.sites[site].stretch !== Stretch.Default) { + if (site && this.active.sites[site]?.stretch !== Stretch.Default) { return this.active.sites[site].stretch; } @@ -563,7 +563,7 @@ class Settings { } getDefaultCropPersistenceMode(site) { - if (site && this.active.sites[site] && this.active.sites[site].cropModePersistence !== Stretch.Default) { + if (site && this.active.sites[site]?.cropModePersistence !== Stretch.Default) { return this.active.sites[site].cropModePersistence; } @@ -572,7 +572,7 @@ class Settings { } getDefaultVideoAlignment(site) { - if (site && this.active.sites[site] && this.active.sites[site].videoAlignment !== VideoAlignment.Default) { + if (this.active.sites[site]?.videoAlignment !== VideoAlignment.Default) { return this.active.sites[site].videoAlignment; } diff --git a/src/ext/lib/video-data/PageInfo.js b/src/ext/lib/video-data/PageInfo.js index 024a6c7..7b1b71f 100644 --- a/src/ext/lib/video-data/PageInfo.js +++ b/src/ext/lib/video-data/PageInfo.js @@ -132,11 +132,8 @@ class PageInfo { } 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){ + if (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) { diff --git a/src/ext/lib/video-data/PlayerData.js b/src/ext/lib/video-data/PlayerData.js index 914bcd3..ac6da43 100644 --- a/src/ext/lib/video-data/PlayerData.js +++ b/src/ext/lib/video-data/PlayerData.js @@ -247,12 +247,9 @@ class PlayerData { this.logger.log('info', 'playerDetect', "\n\n[PlayerDetect::getPlayer()] element hierarchy (video->root)", logObj); } - if (this.settings.active.sites[host] - && this.settings.active.sites[host].DOM - && this.settings.active.sites[host].DOM.player - && this.settings.active.sites[host].DOM.player.manual) { - if (this.settings.active.sites[host].DOM.player.useRelativeAncestor - && this.settings.active.sites[host].DOM.player.videoAncestor) { + if (this.settings.active.sites[host]?.DOM?.player?.manual) { + if (this.settings.active.sites[host]?.DOM?.player?.useRelativeAncestor + && this.settings.active.sites[host]?.DOM?.player?.videoAncestor) { let parentsLeft = this.settings.active.sites[host].DOM.player.videoAncestor - 1; while (parentsLeft --> 0) { @@ -262,7 +259,7 @@ class PlayerData { this.updatePlayerDimensions(element); return element; } - } else if (this.settings.active.sites[host].DOM.player.querySelectors) { + } else if (this.settings.active.sites[host]?.DOM?.player?.querySelectors) { const allSelectors = document.querySelectorAll(this.settings.active.sites[host].DOM.player.querySelectors); // actually we'll also score this branch in a similar way we score the regular, auto branch while (element) { @@ -397,9 +394,7 @@ class PlayerData { // this 'if' is just here for debugging — real code starts later. It's safe to collapse and // ignore the contents of this if (unless we need to change how logging works) if (this.logger.canLog('debug')){ - if (!this.dimensions) { - - } else if (this.dimensions && this.dimensions.fullscreen){ + if (this.dimensions?.fullscreen){ if(! PlayerData.isFullScreen()){ this.logger.log('info', 'debug', "[PlayerDetect] player size changed. reason: exited fullscreen"); } @@ -408,19 +403,17 @@ class PlayerData { this.logger.log('info', 'playerDetect', "[PlayerDetect] player element isn't defined"); } - if ( this.element && this.dimensions && - ( this.dimensions.width != this.element.offsetWidth || - this.dimensions.height != this.element.offsetHeight ) + if ( this.element && + ( +this.dimensions?.width != +this.element?.offsetWidth || + +this.dimensions?.height != +this.element?.offsetHeight ) ) { - this.logger.log('info', 'debug', "[PlayerDetect] player size changed. reason: dimension change. Old dimensions?", this.dimensions.width, this.dimensions.height, "new dimensions:", this.element.offsetWidth, this.element.offsetHeight); + this.logger.log('info', 'debug', "[PlayerDetect] player size changed. reason: dimension change. Old dimensions?", this.dimensions?.width, this.dimensions?.height, "new dimensions:", this.element?.offsetWidth, this.element?.offsetHeight); } } - // if size doesn't match, update & return true - if (!this.dimensions - || this.dimensions.width != this.element.offsetWidth - || this.dimensions.height != this.element.offsetHeight ){ + if (this.dimensions?.width != this.element.offsetWidth + || this.dimensions?.height != this.element.offsetHeight ){ const isFullScreen = PlayerData.isFullScreen(); diff --git a/src/ext/lib/video-data/VideoData.js b/src/ext/lib/video-data/VideoData.js index 532bf42..a85cfee 100644 --- a/src/ext/lib/video-data/VideoData.js +++ b/src/ext/lib/video-data/VideoData.js @@ -420,18 +420,17 @@ class VideoData { if(! this.video) { this.logger.log('info', 'videoDetect', "[VideoDetect] player element isn't defined"); } - if ( this.video && this.dimensions && - ( this.dimensions.width != videoWidth || - this.dimensions.height != videoHeight ) + if ( this.video && + ( this.dimensions?.width != videoWidth || + this.dimensions?.height != videoHeight ) ) { this.logger.log('info', 'debug', "[VideoDetect] player size changed. reason: dimension change. Old dimensions?", this.dimensions.width, this.dimensions.height, "new dimensions:", this.video.offsetWidth, this.video.offsetHeight); } } // if size doesn't match, update & return true - if (!this.dimensions - || this.dimensions.width != videoWidth - || this.dimensions.height != videoHeight ){ + if (this.dimensions?.width != videoWidth + || this.dimensions?.height != videoHeight ){ this.dimensions = { width: videoWidth, height: videoHeight, diff --git a/src/ext/lib/video-transform/Resizer.js b/src/ext/lib/video-transform/Resizer.js index 5b6c1af..7385607 100644 --- a/src/ext/lib/video-transform/Resizer.js +++ b/src/ext/lib/video-transform/Resizer.js @@ -167,32 +167,30 @@ class Resizer { ar.type === AspectRatio.Reset && this.lastAr.type === AspectRatio.Initial) { // some sites do things that interfere with our site (and aspect ratio setting in general) // first, we check whether video contains anything we don't like - if (siteSettings && siteSettings.autoarPreventConditions) { - if (siteSettings.autoarPreventConditions.videoStyleString) { - const styleString = (this.video.getAttribute('style') || '').split(';'); - - if (siteSettings.autoarPreventConditions.videoStyleString.containsProperty) { - const bannedProperties = siteSettings.autoarPreventConditions.videoStyleString.containsProperty; - for (const prop in bannedProperties) { - for (const s of styleString) { - if (s.trim().startsWith(prop)) { - - // check if css property has a list of allowed values: - if (bannedProperties[prop].allowedValues) { - const styleValue = s.split(':')[1].trim(); - - // check if property value is on the list of allowed values - // if it's not, we aren't allowed to start aard - if (bannedProperties[prop].allowedValues.indexOf(styleValue) === -1) { - this.logger.log('error', 'debug', "%c[Resizer::setAr] video style contains forbidden css property/value combo: ", "color: #900, background: #100", prop, " — we aren't allowed to start autoar.") - return; - } - } else { - // no allowed values, no problem. We have forbidden property - // and this means aard can't start. - this.logger.log('info', 'debug', "%c[Resizer::setAr] video style contains forbidden css property: ", "color: #900, background: #100", prop, " — we aren't allowed to start autoar.") + if (siteSettings?.autoarPreventConditions?.videoStyleString) { + const styleString = (this.video.getAttribute('style') || '').split(';'); + + if (siteSettings.autoarPreventConditions.videoStyleString.containsProperty) { + const bannedProperties = siteSettings.autoarPreventConditions.videoStyleString.containsProperty; + for (const prop in bannedProperties) { + for (const s of styleString) { + if (s.trim().startsWith(prop)) { + + // check if css property has a list of allowed values: + if (bannedProperties[prop].allowedValues) { + const styleValue = s.split(':')[1].trim(); + + // check if property value is on the list of allowed values + // if it's not, we aren't allowed to start aard + if (bannedProperties[prop].allowedValues.indexOf(styleValue) === -1) { + this.logger.log('error', 'debug', "%c[Resizer::setAr] video style contains forbidden css property/value combo: ", "color: #900, background: #100", prop, " — we aren't allowed to start autoar.") return; } + } else { + // no allowed values, no problem. We have forbidden property + // and this means aard can't start. + this.logger.log('info', 'debug', "%c[Resizer::setAr] video style contains forbidden css property: ", "color: #900, background: #100", prop, " — we aren't allowed to start autoar.") + return; } } } @@ -368,7 +366,7 @@ class Resizer { this.setAr({type: AspectRatio.Reset}); } else { - if (this.lastAr && this.lastAr.ratio === null) { + if (this.lastAr?.ratio === null) { // if this is the case, we do nothing as we have the correct aspect ratio // throw "Last ar is null!" return; @@ -378,7 +376,7 @@ class Resizer { } reset(){ - this.setStretchMode(this.settings.active.sites[window.location.hostname] ? this.settings.active.sites[window.location.hostname].stretch : this.settings.active.sites['@global'].stretch); + this.setStretchMode(this.settings.active.sites[window.location.hostname]?.stretch ?? this.settings.active.sites['@global'].stretch); this.zoom.setZoom(1); this.resetPan(); this.setAr({type: AspectRatio.Reset}); diff --git a/src/ext/uw-bg.js b/src/ext/uw-bg.js index 7564680..7cf0d62 100644 --- a/src/ext/uw-bg.js +++ b/src/ext/uw-bg.js @@ -179,16 +179,10 @@ class UWServer { // preveri za osirotele/zastarele vrednosti ter jih po potrebi izbriši // check for orphaned/outdated values and remove them if neccessary - if (this.videoTabs[sender.tab.id]) { - if (this.videoTabs[sender.tab.id].host != tabHostname) { - delete this.videoTabs[sender.tab.id] - } else { - if(this.videoTabs[sender.tab.id].frames[sender.frameId]) { - if (this.videoTabs[sender.tab.id].frames[sender.frameId].host != frameHostname) { - delete this.videoTabs[sender.tab.id].frames[sender.frameId]; - } - } - } + if (this.videoTabs[sender.tab.id]?.host != tabHostname) { + delete this.videoTabs[sender.tab.id] + } else if(this.videoTabs[sender.tab.id]?.frames[sender.frameId]?.host != frameHostname) { + delete this.videoTabs[sender.tab.id].frames[sender.frameId]; } if (this.videoTabs[sender.tab.id]) {