Skip to content

Commit

Permalink
Exclude cached resources from large-media-element blocking
Browse files Browse the repository at this point in the history
Related feedback:
- gorhill#1390 (comment)
  • Loading branch information
gorhill committed Oct 19, 2020
1 parent e47f3c4 commit 3ef41e1
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/js/traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,17 +451,17 @@ const onHeadersReceived = function(details) {
}
if ( pageStore.getNetFilteringSwitch(fctxt) === 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);
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;

// 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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3ef41e1

Please sign in to comment.