Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/utils/page-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const OBSERVABLE_REQUEST_PROTOCOLS = new Set(['http:', 'https:', 'ws:', 'wss:'])
export type PageSupport = {
supported: boolean
reason: string
reasonCode?: 'noPage' | 'browserBuiltin' | 'extensionPage' | 'about' | 'viewSource' | 'devtools' | 'localFile' | 'dataBlob' | 'unsupportedProtocol'
}

const getProtocol = (url: unknown): string => {
Expand All @@ -23,22 +24,22 @@ export const isObservableRequestUrl = (url: unknown): boolean => OBSERVABLE_REQU

export const checkPageSupport = (url: unknown): PageSupport => {
const protocol = getProtocol(url)
if (!protocol) return { supported: false, reason: '当前标签页还没有加载网页。' }
if (protocol === 'chrome:') return { supported: false, reason: 'Chrome 浏览器内置页面无法注入检测脚本。' }
if (protocol === 'edge:') return { supported: false, reason: 'Edge 浏览器内置页面无法注入检测脚本。' }
if (!protocol) return { supported: false, reason: '当前标签页还没有加载网页。', reasonCode: 'noPage' }
if (protocol === 'chrome:') return { supported: false, reason: 'Chrome 浏览器内置页面无法注入检测脚本。', reasonCode: 'browserBuiltin' }
if (protocol === 'edge:') return { supported: false, reason: 'Edge 浏览器内置页面无法注入检测脚本。', reasonCode: 'browserBuiltin' }
if (protocol === 'brave:' || protocol === 'opera:' || protocol === 'vivaldi:') {
return { supported: false, reason: '浏览器内置页面无法注入检测脚本。' }
return { supported: false, reason: '浏览器内置页面无法注入检测脚本。', reasonCode: 'browserBuiltin' }
}
if (protocol === 'chrome-extension:' || protocol === 'moz-extension:' || protocol === 'safari-web-extension:') {
return { supported: false, reason: '扩展程序内部页面无法识别。' }
return { supported: false, reason: '扩展程序内部页面无法识别。', reasonCode: 'extensionPage' }
}
if (protocol === 'about:') return { supported: false, reason: '浏览器内部页面无法注入检测脚本。' }
if (protocol === 'view-source:') return { supported: false, reason: '查看源码页面不支持检测。' }
if (protocol === 'about:') return { supported: false, reason: '浏览器内部页面无法注入检测脚本。', reasonCode: 'about' }
if (protocol === 'view-source:') return { supported: false, reason: '查看源码页面不支持检测。', reasonCode: 'viewSource' }
if (protocol === 'devtools:' || protocol === 'chrome-search:' || protocol === 'chrome-untrusted:') {
return { supported: false, reason: '当前页面不支持检测。' }
return { supported: false, reason: '当前页面不支持检测。', reasonCode: 'devtools' }
}
if (protocol === 'file:') return { supported: false, reason: '本地文件页面暂不检测。' }
if (protocol === 'data:' || protocol === 'blob:') return { supported: false, reason: '当前页面类型不支持检测。' }
if (!DETECTABLE_PROTOCOLS.has(protocol)) return { supported: false, reason: '当前页面类型不支持检测。' }
if (protocol === 'file:') return { supported: false, reason: '本地文件页面暂不检测。', reasonCode: 'localFile' }
if (protocol === 'data:' || protocol === 'blob:') return { supported: false, reason: '当前页面类型不支持检测。', reasonCode: 'dataBlob' }
if (!DETECTABLE_PROTOCOLS.has(protocol)) return { supported: false, reason: '当前页面类型不支持检测。', reasonCode: 'unsupportedProtocol' }
return { supported: true, reason: '' }
}
54 changes: 54 additions & 0 deletions tests/page-support.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import { test } from 'node:test'
import { transform } from 'esbuild'

const source = await readFile(new URL('../src/utils/page-support.ts', import.meta.url), 'utf8')
const { code } = await transform(source, { loader: 'ts', format: 'esm', target: 'es2022' })
const { checkPageSupport } = await import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`)

test('unsupported protocols expose the localization key already consumed by the popup', () => {
const fallbackReasons = {
noPage: '当前标签页还没有加载网页。',
browserBuiltin: '浏览器内置页面无法注入检测脚本。',
extensionPage: '扩展程序内部页面无法识别。',
about: '浏览器内部页面无法注入检测脚本。',
viewSource: '查看源码页面不支持检测。',
devtools: '当前页面不支持检测。',
localFile: '本地文件页面暂不检测。',
dataBlob: '当前页面类型不支持检测。',
unsupportedProtocol: '当前页面类型不支持检测。'
}
for (const [url, reasonCode] of [
['', 'noPage'],
['chrome://settings', 'browserBuiltin'],
['edge://settings', 'browserBuiltin'],
['brave://settings', 'browserBuiltin'],
['opera://settings', 'browserBuiltin'],
['vivaldi://settings', 'browserBuiltin'],
['chrome-extension://id/popup.html', 'extensionPage'],
['moz-extension://id/popup.html', 'extensionPage'],
['safari-web-extension://id/popup.html', 'extensionPage'],
['about:blank', 'about'],
['view-source:https://example.com', 'viewSource'],
['devtools://devtools', 'devtools'],
['chrome-search://local-ntp', 'devtools'],
['chrome-untrusted://new-tab-page', 'devtools'],
['file:///tmp/a', 'localFile'],
['data:text/html,test', 'dataBlob'],
['blob:https://example.com/id', 'dataBlob'],
['ftp://example.com', 'unsupportedProtocol']
]) {
assert.equal(checkPageSupport(url).supported, false)
assert.equal(checkPageSupport(url).reasonCode, reasonCode)
const reason = url.startsWith('chrome:')
? 'Chrome 浏览器内置页面无法注入检测脚本。'
: url.startsWith('edge:')
? 'Edge 浏览器内置页面无法注入检测脚本。'
: fallbackReasons[reasonCode]
assert.equal(checkPageSupport(url).reason, reason)
}
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
for (const url of ['http://example.com', 'https://example.com']) {
assert.deepEqual(checkPageSupport(url), { supported: true, reason: '' })
}
})