diff --git a/src/js/traffic.js b/src/js/traffic.js index e6adf3ffe..b24d69971 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -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);