@@ -24,16 +24,26 @@ let lastDetectionResult;
24
24
// So instead, the hook will use postMessage() to pass message to us here.
25
25
// And when this happens, we'll send a message to the "background page".
26
26
window . addEventListener ( 'message' , function ( evt ) {
27
- if (
28
- evt . source === window &&
29
- evt . data &&
30
- evt . data . source === 'react-devtools-detector'
31
- ) {
32
- lastDetectionResult = {
33
- hasDetectedReact : true ,
34
- reactBuildType : evt . data . reactBuildType ,
35
- } ;
36
- chrome . runtime . sendMessage ( lastDetectionResult ) ;
27
+ if ( evt . source === window && evt . data ) {
28
+ if ( evt . data . source === 'react-devtools-detector' ) {
29
+ lastDetectionResult = {
30
+ hasDetectedReact : true ,
31
+ reactBuildType : evt . data . reactBuildType ,
32
+ } ;
33
+ chrome . runtime . sendMessage ( lastDetectionResult ) ;
34
+ } else if ( evt . data . source === 'react-devtools-inject-backend' ) {
35
+ // The backend is injected by the content script to avoid CSP and Trusted Types violations,
36
+ // since content scripts can modify the DOM and are not subject to the page's policies.
37
+ // The prototype stuff is in case document.createElement has been modified.
38
+ const script = document . constructor . prototype . createElement . call (
39
+ document ,
40
+ 'script' ,
41
+ ) ;
42
+ script . src = chrome . runtime . getURL ( 'build/backend.js' ) ;
43
+ script . charset = 'utf-8' ;
44
+ document . documentElement . appendChild ( script ) ;
45
+ script . parentNode . removeChild ( script ) ;
46
+ }
37
47
}
38
48
} ) ;
39
49
0 commit comments