(prefix, options = {})
| 132 | } |
| 133 | |
| 134 | async function closeTabsByUrlPrefix(prefix, options = {}) { |
| 135 | if (!prefix) return 0; |
| 136 | |
| 137 | const { excludeTabIds = [], excludeUrls = [], excludeLocalhostCallbacks = false } = options; |
| 138 | const excluded = new Set(excludeTabIds.filter((id) => Number.isInteger(id))); |
| 139 | const excludedUrls = new Set((Array.isArray(excludeUrls) ? excludeUrls : []).filter(Boolean)); |
| 140 | const tabs = await chrome.tabs.query({}); |
| 141 | const matchedIds = tabs |
| 142 | .filter((tab) => Number.isInteger(tab.id) && !excluded.has(tab.id)) |
| 143 | .filter((tab) => typeof tab.url === 'string' && !excludedUrls.has(tab.url)) |
| 144 | .filter((tab) => !(excludeLocalhostCallbacks && isLocalhostOAuthCallbackUrl(tab.url))) |
| 145 | .filter((tab) => typeof tab.url === 'string' && tab.url.startsWith(prefix)) |
| 146 | .filter((tab) => !isLocalhostOAuthCallbackUrl(tab.url)) |
| 147 | .map((tab) => tab.id); |
| 148 | |
| 149 | if (!matchedIds.length) return 0; |
| 150 | |
| 151 | await chrome.tabs.remove(matchedIds).catch(() => { }); |
| 152 | await addLog(`已关闭 ${matchedIds.length} 个匹配 ${prefix} 的 localhost 残留标签页。`, 'info'); |
| 153 | return matchedIds.length; |
| 154 | } |
| 155 | |
| 156 | async function pingContentScriptOnTab(tabId) { |
| 157 | if (!Number.isInteger(tabId)) return null; |
no test coverage detected