Skip to content

Commit baac1dc

Browse files
author
Brian Vaughn
committed
Inline package tweaks:
* Ignore messages from the DevTools browser extension. * Cleanup/clarify README
1 parent dc8580e commit baac1dc

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

packages/react-devtools-inline/README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ const { contentWindow } = iframe;
7474
initializeBackend(contentWindow);
7575

7676
// React application can be injected into <iframe> at any time now...
77+
// Note that this would need to be done via <script> tag injection,
78+
// as setting the src of the <iframe> would load a new page (withou the injected backend).
7779

7880
// Initialize DevTools UI to listen to the hook we just installed.
7981
// This returns a React component we can render anywhere in the parent window.
@@ -91,29 +93,33 @@ Sandboxed `iframe`s are also supported but require more complex initialization.
9193

9294
**`iframe.html`**
9395
```js
94-
import { activate, initialize } from 'react-devtools-inline/backend';
96+
import { activate, initialize } from "react-devtools-inline/backend";
9597

9698
// The DevTooks hook needs to be installed before React is even required!
9799
// The safest way to do this is probably to install it in a separate script tag.
98100
initialize(window);
99101

100102
// Wait for the frontend to let us know that it's ready.
101-
window.addEventListener('message', ({ data }) => {
103+
function onMessage({ data }) {
102104
switch (data.type) {
103-
case 'activate':
105+
case "activate-backend":
106+
window.removeEventListener("message", onMessage);
107+
104108
activate(window);
105109
break;
106110
default:
107111
break;
108112
}
109-
});
113+
}
114+
115+
window.addEventListener("message", onMessage);
110116
```
111117

112118
**`main-window.html`**
113119
```js
114-
import { initialize } from 'react-devtools-inline/frontend';
120+
import { initialize } from "react-devtools-inline/frontend";
115121

116-
const iframe = document.getElementById('target');
122+
const iframe = document.getElementById("target");
117123
const { contentWindow } = iframe;
118124

119125
// Initialize DevTools UI to listen to the iframe.
@@ -126,9 +132,9 @@ const DevTools = initialize(contentWindow);
126132
iframe.onload = () => {
127133
contentWindow.postMessage(
128134
{
129-
type: 'activate',
135+
type: "activate-backend"
130136
},
131-
'*'
137+
"*"
132138
);
133139
};
134140
```

packages/react-devtools-inline/src/frontend.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import type { Props } from 'src/devtools/views/DevTools';
1616
export function initialize(
1717
contentWindow: window
1818
): React$AbstractComponent<Props, mixed> {
19-
const onMessage = ({ data, origin, source }) => {
19+
const onMessage = ({ data, source }) => {
20+
if (source === 'react-devtools-content-script') {
21+
// Ignore messages from the DevTools browser extension.
22+
}
23+
2024
switch (data.type) {
2125
case MESSAGE_TYPE_GET_SAVED_PREFERENCES:
2226
// This is the only message we're listening for,

0 commit comments

Comments
 (0)