fix(tabs): stop fallback outside locked window

This commit is contained in:
QLHazyCoder
2026-05-12 15:35:43 +08:00
parent 7a5b7a26cf
commit 8b9adb9085
6 changed files with 142 additions and 12 deletions
+10 -5
View File
@@ -35,10 +35,15 @@ function normalizeAutomationWindowId(value) {
if (value === null || value === undefined || value === '') {
return null;
}
const numeric = Math.floor(Number(value));
const numeric = Number(value);
return Number.isInteger(numeric) && numeric >= 0 ? numeric : null;
}
function buildAutomationWindowUnavailableError(error) {
const suffix = error?.message ? ` 原因:${error.message}` : '';
return new Error(`自动任务窗口已不可用,请在目标 Chrome 窗口重新打开侧边栏并启动任务。${suffix}`);
}
async function createAutomationScopedTab(createProperties = {}, options = {}) {
const windowId = normalizeAutomationWindowId(
options?.automationWindowId
@@ -55,8 +60,8 @@ async function createAutomationScopedTab(createProperties = {}, options = {}) {
...(createProperties || {}),
windowId,
});
} catch {
return chrome.tabs.create(createProperties || {});
} catch (error) {
throw buildAutomationWindowUnavailableError(error);
}
}
@@ -78,8 +83,8 @@ async function queryAutomationScopedTabs(queryInfo = {}, options = {}) {
delete scopedQuery.currentWindow;
try {
return await chrome.tabs.query(scopedQuery);
} catch {
return chrome.tabs.query(queryInfo || {});
} catch (error) {
throw buildAutomationWindowUnavailableError(error);
}
}
+1 -1
View File
@@ -214,7 +214,7 @@
if (value === null || value === undefined || value === '') {
return null;
}
const numeric = Math.floor(Number(value));
const numeric = Number(value);
return Number.isInteger(numeric) && numeric >= 0 ? numeric : null;
}
+8 -5
View File
@@ -23,10 +23,15 @@
if (value === null || value === undefined || value === '') {
return null;
}
const numeric = Math.floor(Number(value));
const numeric = Number(value);
return Number.isInteger(numeric) && numeric >= 0 ? numeric : null;
}
function buildAutomationWindowUnavailableError(error) {
const suffix = error?.message ? ` 原因:${error.message}` : '';
return new Error(`自动任务窗口已不可用,请在目标 Chrome 窗口重新打开侧边栏并启动任务。${suffix}`);
}
async function getAutomationWindowId(options = {}) {
const directWindowId = normalizeAutomationWindowId(
options.automationWindowId ?? options.windowId ?? null
@@ -58,8 +63,7 @@
return await chrome.tabs.query(scopedQuery);
} catch (error) {
if (Object.prototype.hasOwnProperty.call(scopedQuery, 'windowId')) {
await setState({ automationWindowId: null }).catch(() => {});
return chrome.tabs.query(queryInfo || {});
throw buildAutomationWindowUnavailableError(error);
}
throw error;
}
@@ -76,8 +80,7 @@
return await chrome.tabs.create(properties);
} catch (error) {
if (windowId !== null) {
await setState({ automationWindowId: null }).catch(() => {});
return chrome.tabs.create(createProperties || {});
throw buildAutomationWindowUnavailableError(error);
}
throw error;
}
+1 -1
View File
@@ -1155,7 +1155,7 @@ function normalizeAutomationWindowId(value) {
if (value === null || value === undefined || value === '') {
return null;
}
const numeric = Math.floor(Number(value));
const numeric = Number(value);
return Number.isInteger(numeric) && numeric >= 0 ? numeric : null;
}
+43
View File
@@ -44,6 +44,8 @@ ${coreSource}
return {
applyExitRegionExpectation,
buildIpProxyPacScript,
chrome,
createAutomationScopedTab,
buildIpProxyRoutingStatePatch,
applyTargetReachabilityExpectation,
getAccountModeProxyPoolFromState,
@@ -51,6 +53,7 @@ return {
normalizeProxyPoolEntries,
parseProxyExitProbePayload,
parseIpProxyLine,
queryAutomationScopedTabs,
resolveExitProbeEndpoints,
resolveIpProxyAutoSwitchThreshold,
resolveTargetReachabilityEndpoints,
@@ -135,6 +138,46 @@ test('IP proxy routing state patch keeps exit probe endpoint for diagnostics', (
assert.equal(patch.ipProxyAppliedExitEndpoint, 'https://ipinfo.io/json');
});
test('IP proxy page probes do not fall back to other windows when the locked window is unavailable', async () => {
const api = loadIpProxyCore();
const created = [];
const queries = [];
api.chrome.tabs = {
create: async (payload) => {
created.push(payload);
if (payload.windowId === 77) {
throw new Error('No window with id: 77');
}
return { id: 12, windowId: payload.windowId, url: payload.url };
},
query: async (queryInfo) => {
queries.push(queryInfo);
if (queryInfo.windowId === 77) {
throw new Error('No window with id: 77');
}
return [{ id: 99, windowId: 1, url: 'https://ipinfo.io/json' }];
},
};
await assert.rejects(
() => api.createAutomationScopedTab(
{ url: 'https://ipinfo.io/json', active: false },
{ state: { automationWindowId: 77 } }
),
/自动任务窗口已不可用/
);
await assert.rejects(
() => api.queryAutomationScopedTabs(
{ url: 'https://ipinfo.io/*' },
{ state: { automationWindowId: 77 } }
),
/自动任务窗口已不可用/
);
assert.deepEqual(created, [{ url: 'https://ipinfo.io/json', active: false, windowId: 77 }]);
assert.deepEqual(queries, [{ url: 'https://ipinfo.io/*', windowId: 77 }]);
});
test('711 fixed-account mode applies region and sticky session parameters', () => {
const api = loadIpProxyCore();
const pool = api.getAccountModeProxyPoolFromState({
@@ -260,3 +260,82 @@ test('tab runtime scopes tab queries to the locked automation window', async ()
assert.deepEqual(queries[0], { active: true, windowId: 22 });
});
test('tab runtime does not create tabs outside an unavailable locked window', async () => {
const source = fs.readFileSync('background/tab-runtime.js', 'utf8');
const globalScope = {};
const api = new Function('self', `${source}; return self.MultiPageBackgroundTabRuntime;`)(globalScope);
const created = [];
const runtime = api.createTabRuntime({
LOG_PREFIX: '[test]',
addLog: async () => {},
chrome: {
tabs: {
create: async (payload) => {
created.push(payload);
if (payload.windowId === 44) {
throw new Error('No window with id: 44');
}
return { id: 99, windowId: payload.windowId, url: payload.url };
},
get: async () => ({ id: 1, windowId: 44, url: 'https://example.com' }),
query: async () => [],
},
},
getSourceLabel: (sourceName) => sourceName || 'unknown',
getState: async () => ({
automationWindowId: 44,
tabRegistry: {},
sourceLastUrls: {},
}),
matchesSourceUrlFamily: () => false,
setState: async () => {},
throwIfStopped: () => {},
});
await assert.rejects(
() => runtime.createAutomationTab({ url: 'https://example.com', active: true }),
/自动任务窗口已不可用/
);
assert.deepEqual(created, [{ url: 'https://example.com', active: true, windowId: 44 }]);
});
test('tab runtime does not query all windows when the locked window is unavailable', async () => {
const source = fs.readFileSync('background/tab-runtime.js', 'utf8');
const globalScope = {};
const api = new Function('self', `${source}; return self.MultiPageBackgroundTabRuntime;`)(globalScope);
const queries = [];
const runtime = api.createTabRuntime({
LOG_PREFIX: '[test]',
addLog: async () => {},
chrome: {
tabs: {
get: async () => ({ id: 1, windowId: 55, url: 'https://example.com' }),
query: async (queryInfo) => {
queries.push(queryInfo);
if (queryInfo.windowId === 55) {
throw new Error('No window with id: 55');
}
return [{ id: 7, windowId: 1, url: 'https://other.example/' }];
},
},
},
getSourceLabel: (sourceName) => sourceName || 'unknown',
getState: async () => ({
automationWindowId: 55,
tabRegistry: {},
sourceLastUrls: {},
}),
matchesSourceUrlFamily: () => false,
setState: async () => {},
throwIfStopped: () => {},
});
await assert.rejects(
() => runtime.queryTabsInAutomationWindow({ active: true }),
/自动任务窗口已不可用/
);
assert.deepEqual(queries, [{ active: true, windowId: 55 }]);
});