@@ -822,33 +822,42 @@ const proxyURL = "${proxyURL}";
822822 newChild : T ,
823823 refChild : Node | null
824824 ) : T {
825- if ( typeof newChild === "string" ) {
826- console . warn (
827- "[Node Interceptor] String passed to insertBefore, converting to TextNode"
828- ) ;
829- const textNode = document . createTextNode ( newChild ) ;
830- return originalInsertBefore . call (
831- this ,
832- textNode ,
833- refChild
834- ) as unknown as T ;
835- }
825+ try {
826+ if ( typeof newChild === "string" ) {
827+ console . warn (
828+ "[Node Interceptor] String passed to insertBefore, converting to TextNode"
829+ ) ;
830+ const textNode = document . createTextNode ( newChild ) ;
831+ return originalInsertBefore . call (
832+ this ,
833+ textNode ,
834+ refChild
835+ ) as unknown as T ;
836+ }
837+
838+ if ( ! ( newChild instanceof Node ) ) {
839+ console . warn (
840+ "[Node Interceptor] Invalid parameter passed to insertBefore:" ,
841+ newChild
842+ ) ;
843+ throw new TypeError (
844+ "Failed to execute 'insertBefore' on 'Node': parameter 1 is not of type 'Node'"
845+ ) ;
846+ }
836847
837- if ( ! ( newChild instanceof Node ) ) {
848+ if ( newChild instanceof HTMLElement ) {
849+ applyInterceptorsToNewElement ( newChild ) ;
850+ }
851+ //@ts -ignore
852+ return originalInsertBefore . call ( this , newChild , refChild ) ;
853+ } catch ( e ) {
838854 console . warn (
839- "[Node Interceptor] Invalid parameter passed to insertBefore:" ,
840- newChild
841- ) ;
842- throw new TypeError (
843- "Failed to execute 'insertBefore' on 'Node': parameter 1 is not of type 'Node'"
855+ "[Node Interceptor] Error in insertBefore, using original method:" ,
856+ e
844857 ) ;
858+ //@ts -ignore
859+ return originalInsertBefore . call ( this , newChild , refChild ) ;
845860 }
846-
847- if ( newChild instanceof HTMLElement ) {
848- applyInterceptorsToNewElement ( newChild ) ;
849- }
850- //@ts -ignore
851- return originalInsertBefore . call ( this , newChild , refChild ) ;
852861 } ;
853862
854863 const originalReplaceChild = Node . prototype . replaceChild ;
@@ -857,62 +866,87 @@ const proxyURL = "${proxyURL}";
857866 newChild : T ,
858867 oldChild : Node
859868 ) : T {
860- if ( typeof newChild === "string" ) {
861- console . warn (
862- "[Node Interceptor] String passed to replaceChild, converting to TextNode"
863- ) ;
864- const textNode = document . createTextNode ( newChild ) ;
865- return originalReplaceChild . call (
866- this ,
867- textNode ,
868- oldChild
869- ) as unknown as T ;
870- }
869+ try {
870+ if ( typeof newChild === "string" ) {
871+ console . warn (
872+ "[Node Interceptor] String passed to replaceChild, converting to TextNode"
873+ ) ;
874+ const textNode = document . createTextNode ( newChild ) ;
875+ return originalReplaceChild . call (
876+ this ,
877+ textNode ,
878+ oldChild
879+ ) as unknown as T ;
880+ }
871881
872- if ( ! ( newChild instanceof Node ) ) {
882+ if ( ! ( newChild instanceof Node ) ) {
883+ console . warn (
884+ "[Node Interceptor] Invalid parameter passed to replaceChild:" ,
885+ newChild
886+ ) ;
887+ throw new TypeError (
888+ "Failed to execute 'replaceChild' on 'Node': parameter 1 is not of type 'Node'"
889+ ) ;
890+ }
891+
892+ if ( newChild instanceof HTMLElement ) {
893+ applyInterceptorsToNewElement ( newChild ) ;
894+ }
895+ //@ts -ignore
896+ return originalReplaceChild . call ( this , newChild , oldChild ) ;
897+ } catch ( e ) {
873898 console . warn (
874- "[Node Interceptor] Invalid parameter passed to replaceChild:" ,
875- newChild
876- ) ;
877- throw new TypeError (
878- "Failed to execute 'replaceChild' on 'Node': parameter 1 is not of type 'Node'"
899+ "[Node Interceptor] Error in replaceChild, using original method:" ,
900+ e
879901 ) ;
902+ //@ts -ignore
903+ return originalReplaceChild . call ( this , newChild , oldChild ) ;
880904 }
881-
882- if ( newChild instanceof HTMLElement ) {
883- applyInterceptorsToNewElement ( newChild ) ;
884- }
885- //@ts -ignore
886- return originalReplaceChild . call ( this , newChild , oldChild ) ;
887905 } ;
888906
889907 if ( Element . prototype . append ) {
890908 const originalAppend = Element . prototype . append ;
891909 Element . prototype . append = function ( ...nodes : ( Node | string ) [ ] ) {
892- const processedNodes = nodes . map ( ( node ) => {
893- if ( node instanceof HTMLElement ) {
894- applyInterceptorsToNewElement ( node ) ;
910+ try {
911+ const processedNodes = nodes . map ( ( node ) => {
912+ if ( node instanceof HTMLElement ) {
913+ applyInterceptorsToNewElement ( node ) ;
914+ return node ;
915+ }
895916 return node ;
896- }
897- return node ;
898- } ) ;
917+ } ) ;
899918
900- return originalAppend . apply ( this , processedNodes ) ;
919+ return originalAppend . apply ( this , processedNodes ) ;
920+ } catch ( e ) {
921+ console . warn (
922+ "[Node Interceptor] Error in append, using original method:" ,
923+ e
924+ ) ;
925+ return originalAppend . apply ( this , nodes ) ;
926+ }
901927 } ;
902928 }
903929
904930 if ( Element . prototype . prepend ) {
905931 const originalPrepend = Element . prototype . prepend ;
906932 Element . prototype . prepend = function ( ...nodes : ( Node | string ) [ ] ) {
907- const processedNodes = nodes . map ( ( node ) => {
908- if ( node instanceof HTMLElement ) {
909- applyInterceptorsToNewElement ( node ) ;
933+ try {
934+ const processedNodes = nodes . map ( ( node ) => {
935+ if ( node instanceof HTMLElement ) {
936+ applyInterceptorsToNewElement ( node ) ;
937+ return node ;
938+ }
910939 return node ;
911- }
912- return node ;
913- } ) ;
940+ } ) ;
914941
915- return originalPrepend . apply ( this , processedNodes ) ;
942+ return originalPrepend . apply ( this , processedNodes ) ;
943+ } catch ( e ) {
944+ console . warn (
945+ "[Node Interceptor] Error in prepend, using original method:" ,
946+ e
947+ ) ;
948+ return originalPrepend . apply ( this , nodes ) ;
949+ }
916950 } ;
917951 }
918952
@@ -921,49 +955,57 @@ const proxyURL = "${proxyURL}";
921955 name : string ,
922956 value : string
923957 ) : void {
924- const urlAttributes = [ "src" , "href" , "action" , "data-src" ] ;
925- if ( urlAttributes . includes ( name ) && typeof value === "string" ) {
926- let mod = "mp_" ;
927-
928- if ( this instanceof HTMLScriptElement && name === "src" ) {
929- mod = "js_" ;
930- } else if (
931- this instanceof HTMLLinkElement &&
932- name === "href" &&
933- ( this . rel === "stylesheet" ||
934- this . as === "style" ||
935- ( value && value . endsWith ( ".css" ) ) )
936- ) {
937- mod = "cs_" ;
958+ try {
959+ const urlAttributes = [ "src" , "href" , "action" , "data-src" ] ;
960+ if ( urlAttributes . includes ( name ) && typeof value === "string" ) {
961+ let mod = "mp_" ;
962+
963+ if ( this instanceof HTMLScriptElement && name === "src" ) {
964+ mod = "js_" ;
965+ } else if (
966+ this instanceof HTMLLinkElement &&
967+ name === "href" &&
968+ ( this . rel === "stylesheet" ||
969+ this . as === "style" ||
970+ ( value && value . endsWith ( ".css" ) ) )
971+ ) {
972+ mod = "cs_" ;
973+ }
974+
975+ const rewrittenValue = rewriteUrl ( value , mod ) ;
976+ return originalSetAttribute . call ( this , name , rewrittenValue ) ;
938977 }
939978
940- const rewrittenValue = rewriteUrl ( value , mod ) ;
941- return originalSetAttribute . call ( this , name , rewrittenValue ) ;
942- }
979+ if ( name === "srcset" && typeof value === "string" ) {
980+ const parts = value . split ( "," ) . map ( ( part ) => {
981+ const [ url , ...descriptors ] = part . trim ( ) . split ( / \s + / ) ;
982+ if ( url && ! url . startsWith ( "data:" ) ) {
983+ const rewrittenUrl = rewriteUrl ( url , "mp_" ) ;
984+ return [ rewrittenUrl , ...descriptors ] . join ( " " ) ;
985+ }
986+ return part ;
987+ } ) ;
943988
944- if ( name === "srcset" && typeof value === "string" ) {
945- const parts = value . split ( "," ) . map ( ( part ) => {
946- const [ url , ...descriptors ] = part . trim ( ) . split ( / \s + / ) ;
947- if ( url && ! url . startsWith ( "data:" ) ) {
948- const rewrittenUrl = rewriteUrl ( url , "mp_" ) ;
949- return [ rewrittenUrl , ...descriptors ] . join ( " " ) ;
950- }
951- return part ;
952- } ) ;
989+ return originalSetAttribute . call ( this , name , parts . join ( ", " ) ) ;
990+ }
953991
954- return originalSetAttribute . call ( this , name , parts . join ( ", " ) ) ;
955- }
992+ if (
993+ name === "style" &&
994+ typeof value === "string" &&
995+ value . includes ( "url(" )
996+ ) {
997+ const rewrittenStyle = rewriteCssUrls ( value ) ;
998+ return originalSetAttribute . call ( this , name , rewrittenStyle ) ;
999+ }
9561000
957- if (
958- name === "style" &&
959- typeof value === "string" &&
960- value . includes ( "url(" )
961- ) {
962- const rewrittenStyle = rewriteCssUrls ( value ) ;
963- return originalSetAttribute . call ( this , name , rewrittenStyle ) ;
1001+ return originalSetAttribute . call ( this , name , value ) ;
1002+ } catch ( e ) {
1003+ console . warn (
1004+ "[Node Interceptor] Error in setAttribute, using original method:" ,
1005+ e
1006+ ) ;
1007+ return originalSetAttribute . call ( this , name , value ) ;
9641008 }
965-
966- return originalSetAttribute . call ( this , name , value ) ;
9671009 } ;
9681010 }
9691011
@@ -1272,7 +1314,7 @@ const proxyURL = "${proxyURL}";
12721314
12731315 overrideDocumentDefaultView ( ) ;
12741316
1275- overrideImport ( ) ;
1317+ // overrideImport();
12761318 }
12771319
12781320 initAllInterceptors ( ) ;
0 commit comments