Skip to content

Commit d07da7b

Browse files
committed
fix(extension): tie the probed origin to the panel and time out slow probes
1 parent 1afafb4 commit d07da7b

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

‎extension/panel-bridge.js‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ const LOCAL_HOSTS = ['localhost', '127.0.0.1'];
1010
// Where devframe may be mounted.
1111
const PATHS = ['/__ng-devtools/', '/__devframe/', '/'];
1212
const CONNECTION_FILES = ['__devframe/__connection.json', '__connection.json'];
13+
const PROBE_TIMEOUT_MS = 1500;
14+
15+
let detection = 0;
1316

1417
// Look for a devframe connection, but only on a loopback page: nothing else
1518
// can be connected to, so nothing else is worth probing.
1619
function detectConnection() {
20+
const run = ++detection;
1721
chrome.devtools.inspectedWindow.eval('location.origin', (origin, error) => {
22+
if (run !== detection) return;
1823
if (error || typeof origin !== 'string') {
1924
loadPanel(null);
2025
return;
@@ -33,7 +38,9 @@ function detectConnection() {
3338
return;
3439
}
3540

36-
findConnection(origin).then(loadPanel);
41+
findConnection(origin).then((base) => {
42+
if (run === detection) loadPanel(base, origin);
43+
});
3744
});
3845
}
3946

@@ -45,6 +52,7 @@ async function findConnection(origin) {
4552
const response = await fetch(new URL(base + file, origin), {
4653
credentials: 'omit',
4754
cache: 'no-store',
55+
signal: AbortSignal.timeout(PROBE_TIMEOUT_MS),
4856
});
4957
if (!response.ok) continue;
5058
await response.json();
@@ -57,26 +65,21 @@ async function findConnection(origin) {
5765
return null;
5866
}
5967

60-
function loadPanel(baseURL) {
68+
function loadPanel(baseURL, origin) {
6169
status.classList.add('hidden');
6270
frame.style.display = 'block';
6371

6472
// The SPA is bundled inside the extension at ui/index.html
6573
const panelUrl = chrome.runtime.getURL('ui/index.html');
6674

67-
if (baseURL) {
68-
// Get the inspected page's origin to build the full baseURL
69-
chrome.devtools.inspectedWindow.eval('location.origin', (origin) => {
70-
const url = new URL(baseURL, origin);
71-
if (!LOCAL_HOSTS.includes(url.hostname)) {
72-
frame.src = panelUrl;
73-
return;
74-
}
75-
frame.src = `${panelUrl}?baseURL=${encodeURIComponent(url.href)}`;
76-
});
77-
} else {
75+
if (!baseURL || !origin) {
7876
frame.src = panelUrl;
77+
return;
7978
}
79+
const url = new URL(baseURL, origin);
80+
frame.src = LOCAL_HOSTS.includes(url.hostname)
81+
? `${panelUrl}?baseURL=${encodeURIComponent(url.href)}`
82+
: panelUrl;
8083
}
8184

8285
// Start detection after a short delay to let the page settle

0 commit comments

Comments
 (0)