12
} from 'react-devtools-shared/src/storage';
13
import DevTools from 'react-devtools-shared/src/devtools/views/DevTools';
14
import {
15
- __DEBUG__,
15
LOCAL_STORAGE_SUPPORTS_PROFILING_KEY,
16
LOCAL_STORAGE_TRACE_UPDATES_ENABLED_KEY,
17
} from 'react-devtools-shared/src/constants';
23
} from './elementSelection';
24
import {startReactPolling} from './reactPolling';
25
import cloneStyleTags from './cloneStyleTags';
26
+import fetchFileWithCaching from './fetchFileWithCaching';
27
import injectBackendManager from './injectBackendManager';
28
import syncSavedPreferences from './syncSavedPreferences';
29
import registerEventsLogger from './registerEventsLogger';
158
}
159
};
160
161
- let debugIDCounter = 0;
162
-
163
- // For some reason in Firefox, chrome.runtime.sendMessage() from a content script
164
- // never reaches the chrome.runtime.onMessage event listener.
165
- let fetchFileWithCaching = null;
166
- if (IS_CHROME) {
167
- const fetchFromNetworkCache = (url, resolve, reject) => {
168
- // Debug ID allows us to avoid re-logging (potentially long) URL strings below,
169
- // while also still associating (potentially) interleaved logs with the original request.
170
- let debugID = null;
171
-
172
- if (__DEBUG__) {
173
- debugID = debugIDCounter++;
174
- console.log(`[main] fetchFromNetworkCache(${debugID})`, url);
175
- }
176
-
177
- chrome.devtools.network.getHAR(harLog => {
178
- for (let i = 0; i < harLog.entries.length; i++) {
179
- const entry = harLog.entries[i];
180
- if (url === entry.request.url) {
181
- if (__DEBUG__) {
182
- console.log(
183
- `[main] fetchFromNetworkCache(${debugID}) Found matching URL in HAR`,
184
- url,
185
- );
186
- }
187
-
188
- entry.getContent(content => {
189
- if (content) {
190
- if (__DEBUG__) {
191
- console.log(
192
- `[main] fetchFromNetworkCache(${debugID}) Content retrieved`,
193
- );
194
- }
195
-
196
- resolve(content);
197
- } else {
198
- if (__DEBUG__) {
199
- console.log(
200
- `[main] fetchFromNetworkCache(${debugID}) Invalid content returned by getContent()`,
201
- content,
202
- );
203
- }
204
-
205
- // Edge case where getContent() returned null; fall back to fetch.
206
- fetchFromPage(url, resolve, reject);
207
- }
208
- });
209
-
210
- return;
211
- }
212
- }
213
-
214
- if (__DEBUG__) {
215
- console.log(
216
- `[main] fetchFromNetworkCache(${debugID}) No cached request found in getHAR()`,
217
- );
218
- }
219
-
220
- // No matching URL found; fall back to fetch.
221
- fetchFromPage(url, resolve, reject);
222
- });
223
- };
224
-
225
- const fetchFromPage = (url, resolve, reject) => {
226
- if (__DEBUG__) {
227
- console.log('[main] fetchFromPage()', url);
228
- }
229
-
230
- function onPortMessage({payload, source}) {
231
- if (source === 'react-devtools-content-script') {
232
- switch (payload?.type) {
233
- case 'fetch-file-with-cache-complete':
234
- chrome.runtime.onMessage.removeListener(onPortMessage);
235
- resolve(payload.value);
236
- break;
237
- case 'fetch-file-with-cache-error':
238
- chrome.runtime.onMessage.removeListener(onPortMessage);
239
- reject(payload.value);
240
- break;
241
- }
242
- }
243
- }
244
-
245
- chrome.runtime.onMessage.addListener(onPortMessage);
246
-
247
- chrome.devtools.inspectedWindow.eval(`
248
- window.postMessage({
249
- source: 'react-devtools-extension',
250
- payload: {
251
- type: 'fetch-file-with-cache',
252
- url: "${url}",
253
- },
254
- });
255
- `);
256
- };
257
-
258
- // Fetching files from the extension won't make use of the network cache
259
- // for resources that have already been loaded by the page.
260
- // This helper function allows the extension to request files to be fetched
261
- // by the content script (running in the page) to increase the likelihood of a cache hit.
262
- fetchFileWithCaching = url => {
263
- return new Promise((resolve, reject) => {
264
- // Try fetching from the Network cache first.
265
- // If DevTools was opened after the page started loading, we may have missed some requests.
266
- // So fall back to a fetch() from the page and hope we get a cached response that way.
267
- fetchFromNetworkCache(url, resolve, reject);
268
- });
269
- };
270
- }
271
-
161
// TODO (Webpack 5) Hopefully we can remove this prop after the Webpack 5 migration.
162
const hookNamesModuleLoaderFunction = () =>
163
import(