From b429e1c7bad8d891f83f9f282a8406952caedbcd Mon Sep 17 00:00:00 2001 From: gorhill Date: Sat, 22 Jul 2017 16:58:08 -0400 Subject: [PATCH] fix #2813 --- src/js/traffic.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/js/traffic.js b/src/js/traffic.js index 6b840ee27..67718a15e 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -410,15 +410,24 @@ var onHeadersReceived = function(details) { return foilLargeMediaElement(pageStore, details); } - // https://github.com/gorhill/uBO-Extra/issues/19 - // Turns out scripts must also be considered as potential embedded - // contexts (as workers) and as such we may need to inject content - // security policy directives. - if ( requestType === 'main_frame' || requestType === 'sub_frame' ) { + // 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 + // point to further block large media elements for the current document. + if ( requestType === 'main_frame' ) { + if ( reMediaContentTypes.test(headerValueFromName('content-type', details.responseHeaders)) ) { + pageStore.allowLargeMediaElementsUntil = Date.now() + 86400000; + } + return injectCSP(pageStore, details); + } + + if ( requestType === 'sub_frame' ) { return injectCSP(pageStore, details); } }; +var reMediaContentTypes = /^(?:audio|image|video)\//; + /******************************************************************************/ var injectCSP = function(pageStore, details) { @@ -603,6 +612,11 @@ var headerIndexFromName = function(headerName, headers) { return -1; }; +var headerValueFromName = function(headerName, headers) { + var i = headerIndexFromName(headerName, headers); + return i !== -1 ? headers[i].value : ''; +}; + /******************************************************************************/ vAPI.net.onBeforeRequest = {