Exclude cached resources from large-media-element blocking

Related feedback:
- https://github.com/gorhill/uBlock/issues/1390#issuecomment-187310719
This commit is contained in:
Raymond Hill 2020-10-19 08:01:03 -04:00
parent e47f3c48aa
commit 3ef41e1d78
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -451,17 +451,17 @@ const onHeadersReceived = function(details) {
}
if ( pageStore.getNetFilteringSwitch(fctxt) === false ) { return; }
if ( requestType === 'image' || requestType === 'media' ) {
return foilLargeMediaElement(details, fctxt, pageStore);
}
if ( isDoc === false ) { return; }
// Keep in mind response headers will be modified in-place if needed, so
// `details.responseHeaders` will always point to the modified response
// headers.
const responseHeaders = details.responseHeaders;
if ( requestType === 'image' || requestType === 'media' ) {
return foilLargeMediaElement(fctxt, pageStore, responseHeaders);
}
if ( isDoc === false ) { return; }
// https://github.com/gorhill/uBlock/issues/2813
// Disable the blocking of large media elements if the document is itself
// a media element: the resource was not prevented from loading so no
@ -939,14 +939,21 @@ const injectCSP = function(fctxt, pageStore, responseHeaders) {
/******************************************************************************/
// https://github.com/gorhill/uBlock/issues/1163
// "Block elements by size"
// "Block elements by size".
// https://github.com/gorhill/uBlock/issues/1390#issuecomment-187310719
// Do not foil when the media element is fetched from the browser
// cache. This works only when the webext API supports the `fromCache`
// property (Firefox).
const foilLargeMediaElement = function(details, fctxt, pageStore) {
if ( details.fromCache === true ) { return; }
const foilLargeMediaElement = function(fctxt, pageStore, responseHeaders) {
let size = 0;
if ( µBlock.userSettings.largeMediaSize !== 0 ) {
const i = headerIndexFromName('content-length', responseHeaders);
const headers = details.responseHeaders;
const i = headerIndexFromName('content-length', headers);
if ( i === -1 ) { return; }
size = parseInt(responseHeaders[i].value, 10) || 0;
size = parseInt(headers[i].value, 10) || 0;
}
const result = pageStore.filterLargeMediaElement(fctxt, size);