From 53dd339d78dbd765e24783b9aa52cf0d29935598 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 18 Oct 2020 10:07:46 -0400 Subject: [PATCH] Improve interactivity for blocked large media elements Related issues: - https://github.com/gorhill/uBlock/issues/1390 - https://github.com/gorhill/uBlock/issues/2334 The deadline to interactively load a specific media element has been extended from 2sec to 5sec. Clicking over a blocked large media element will cause uBO to lookup and handle all potentially blocked large elements at the cursor position. This should take care of being able to unblock media elements hidden under other DOM object. The CSS style applied to blocked large media elements has been fine tuned to improve interactivity. uBO will now remember the specific media elements which were unblocked and keep them exempted from being further blocked. This would be an issue when unblocking a video and then a bit later seeking to another point in the video, in which case uBO would again block network requests for that video. --- src/js/messaging.js | 2 +- src/js/pagestore.js | 30 ++++- src/js/scriptlets/load-large-media-all.js | 36 ++---- .../load-large-media-interactive.js | 119 +++++++++++------- src/js/traffic.js | 2 +- 5 files changed, 113 insertions(+), 76 deletions(-) diff --git a/src/js/messaging.js b/src/js/messaging.js index 44c4e810f..806845c7c 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -1641,7 +1641,7 @@ const onMessage = function(request, sender, callback) { case 'temporarilyAllowLargeMediaElement': if ( pageStore !== null ) { - pageStore.allowLargeMediaElementsUntil = Date.now() + 2000; + pageStore.allowLargeMediaElementsUntil = Date.now() + 5000; } break; diff --git a/src/js/pagestore.js b/src/js/pagestore.js index 7301f992b..ad80d1ae0 100644 --- a/src/js/pagestore.js +++ b/src/js/pagestore.js @@ -244,7 +244,7 @@ const PageStore = class { typeof this.allowLargeMediaElementsUntil !== 'number' || tabContext.rootHostname !== this.tabHostname ) { - this.allowLargeMediaElementsUntil = 0; + this.allowLargeMediaElementsUntil = Date.now(); } this.tabHostname = tabContext.rootHostname; @@ -260,6 +260,7 @@ const PageStore = class { this.largeMediaCount = 0; this.largeMediaTimer = null; this.internalRedirectionCount = 0; + this.allowLargeMediaElementsRegex = undefined; this.extraData.clear(); this.frameAddCount = 0; @@ -339,7 +340,8 @@ const PageStore = class { this.rawURL = ''; this.hostnameToCountMap = null; this.netFilteringCache.empty(); - this.allowLargeMediaElementsUntil = 0; + this.allowLargeMediaElementsUntil = Date.now(); + this.allowLargeMediaElementsRegex = undefined; if ( this.largeMediaTimer !== null ) { clearTimeout(this.largeMediaTimer); this.largeMediaTimer = null; @@ -438,7 +440,12 @@ const PageStore = class { temporarilyAllowLargeMediaElements(state) { this.largeMediaCount = 0; µb.contextMenu.update(this.tabId); - this.allowLargeMediaElementsUntil = state ? Date.now() + 86400000 : 0; + if ( state ) { + this.allowLargeMediaElementsUntil = 0; + this.allowLargeMediaElementsRegex = undefined; + } else { + this.allowLargeMediaElementsUntil = Date.now(); + } µb.scriptlets.injectDeep(this.tabId, 'load-large-media-all'); } @@ -704,7 +711,23 @@ const PageStore = class { filterLargeMediaElement(fctxt, size) { fctxt.filter = undefined; + if ( this.allowLargeMediaElementsUntil === 0 ) { + return 0; + } + // Disregard large media elements previously allowed: for example, to + // seek inside a previously allowed audio/video. + if ( + this.allowLargeMediaElementsRegex instanceof RegExp && + this.allowLargeMediaElementsRegex.test(fctxt.url) + ) { + return 0; + } if ( Date.now() < this.allowLargeMediaElementsUntil ) { + const sources = this.allowLargeMediaElementsRegex instanceof RegExp + ? [ this.allowLargeMediaElementsRegex.source ] + : []; + sources.push('^' + µb.escapeRegex(fctxt.url)); + this.allowLargeMediaElementsRegex = new RegExp(sources.join('|')); return 0; } if ( @@ -713,6 +736,7 @@ const PageStore = class { fctxt.getTabHostname() ) !== true ) { + this.allowLargeMediaElementsUntil = 0; return 0; } if ( (size >>> 10) < µb.userSettings.largeMediaSize ) { diff --git a/src/js/scriptlets/load-large-media-all.js b/src/js/scriptlets/load-large-media-all.js index a6d71734a..943706d28 100644 --- a/src/js/scriptlets/load-large-media-all.js +++ b/src/js/scriptlets/load-large-media-all.js @@ -19,48 +19,36 @@ Home: https://github.com/gorhill/uBlock */ +'use strict'; + /******************************************************************************/ -(function() { - -'use strict'; +(( ) => { /******************************************************************************/ // For all media resources which have failed to load, trigger a reload. -var elems, i, elem, src; - //