@@ -480,13 +480,6 @@ const onBeforeBehindTheSceneRequest = function(fctxt) {
480
480
// - CSP injection
481
481
482
482
const onHeadersReceived = function ( details ) {
483
- // https://github.com/uBlockOrigin/uBlock-issues/issues/610
484
- // Process behind-the-scene requests in a special way.
485
- if ( details . tabId < 0 ) {
486
- if ( normalizeBehindTheSceneResponseHeaders ( details ) === false ) {
487
- return ;
488
- }
489
- }
490
483
491
484
const fctxt = µb . filteringContext . fromWebrequestDetails ( details ) ;
492
485
const isRootDoc = fctxt . itype === fctxt . MAIN_FRAME ;
@@ -522,13 +515,14 @@ const onHeadersReceived = function(details) {
522
515
}
523
516
}
524
517
518
+ const mime = mimeFromHeaders ( responseHeaders ) ;
519
+
525
520
// https://github.com/gorhill/uBlock/issues/2813
526
521
// Disable the blocking of large media elements if the document is itself
527
522
// a media element: the resource was not prevented from loading so no
528
523
// point to further block large media elements for the current document.
529
524
if ( isRootDoc ) {
530
- const contentType = headerValueFromName ( 'content-type' , responseHeaders ) ;
531
- if ( reMediaContentTypes . test ( contentType ) ) {
525
+ if ( reMediaContentTypes . test ( mime ) ) {
532
526
pageStore . allowLargeMediaElementsUntil = 0 ;
533
527
// Fall-through: this could be an SVG document, which supports
534
528
// script tags.
@@ -547,7 +541,7 @@ const onHeadersReceived = function(details) {
547
541
} ) ;
548
542
}
549
543
// html filtering
550
- if ( isRootDoc || fctxt . itype === fctxt . SUB_FRAME ) {
544
+ if ( mime === 'text/html' || mime === 'application/xhtml+xml' ) {
551
545
const selectors = htmlFilteringEngine . retrieve ( fctxt ) ;
552
546
if ( selectors ) {
553
547
jobs . push ( {
@@ -600,23 +594,19 @@ const reMediaContentTypes = /^(?:audio|image|video)\//;
600
594
601
595
/******************************************************************************/
602
596
603
- // https://github.com/uBlockOrigin/uBlock-issues/issues/610
604
-
605
- const normalizeBehindTheSceneResponseHeaders = function ( details ) {
606
- if ( details . type !== 'xmlhttprequest' ) { return false ; }
607
- const headers = details . responseHeaders ;
608
- if ( Array . isArray ( headers ) === false ) { return false ; }
609
- const contentType = headerValueFromName ( 'content-type' , headers ) ;
610
- if ( contentType === '' ) { return false ; }
611
- if ( reMediaContentTypes . test ( contentType ) === false ) { return false ; }
612
- if ( contentType . startsWith ( 'image' ) ) {
613
- details . type = 'image' ;
614
- } else {
615
- details . type = 'media' ;
616
- }
617
- return true ;
597
+ const mimeFromHeaders = headers => {
598
+ if ( Array . isArray ( headers ) === false ) { return '' ; }
599
+ return mimeFromContentType ( headerValueFromName ( 'content-type' , headers ) ) ;
600
+ } ;
601
+
602
+ const mimeFromContentType = contentType => {
603
+ const match = reContentTypeMime . exec ( contentType ) ;
604
+ if ( match === null ) { return '' ; }
605
+ return match [ 1 ] . toLowerCase ( ) ;
618
606
} ;
619
607
608
+ const reContentTypeMime = / ^ ( [ ^ ; ] + ) (?: ; | $ ) / i;
609
+
620
610
/******************************************************************************/
621
611
622
612
function textResponseFilterer ( session , directives ) {
@@ -683,7 +673,6 @@ htmlResponseFilterer.xmlSerializer = null;
683
673
684
674
const bodyFilterer = ( ( ) => {
685
675
const sessions = new Map ( ) ;
686
- const reContentTypeDocument = / ^ ( [ ^ ; ] + ) (?: ; | $ ) / i;
687
676
const reContentTypeCharset = / c h a r s e t = [ ' " ] ? ( [ ^ ' " ] + ) / i;
688
677
const otherValidMimes = new Set ( [
689
678
'application/javascript' ,
@@ -712,12 +701,6 @@ const bodyFilterer = (( ) => {
712
701
return '' ;
713
702
} ;
714
703
715
- const mimeFromContentType = contentType => {
716
- const match = reContentTypeDocument . exec ( contentType ) ;
717
- if ( match === null ) { return ; }
718
- return match [ 1 ] . toLowerCase ( ) ;
719
- } ;
720
-
721
704
const charsetFromContentType = contentType => {
722
705
const match = reContentTypeCharset . exec ( contentType ) ;
723
706
if ( match === null ) { return ; }
@@ -932,6 +915,9 @@ const bodyFilterer = (( ) => {
932
915
if ( contentType !== '' ) {
933
916
mime = mimeFromContentType ( contentType ) ;
934
917
if ( mime === undefined ) { return ; }
918
+ if ( mime . startsWith ( 'text/' ) === false ) {
919
+ if ( otherValidMimes . has ( mime ) === false ) { return ; }
920
+ }
935
921
charset = charsetFromContentType ( contentType ) ;
936
922
if ( charset !== undefined ) {
937
923
charset = textEncode . normalizeCharset ( charset ) ;
@@ -941,10 +927,6 @@ const bodyFilterer = (( ) => {
941
927
}
942
928
}
943
929
944
- if ( mime . startsWith ( 'text/' ) === false ) {
945
- if ( otherValidMimes . has ( mime ) === false ) { return ; }
946
- }
947
-
948
930
return true ;
949
931
}
950
932
} ;
0 commit comments