我正在创建一个在每个页面上运行但仅在某些上下文中工作的网络扩展。这是 isSupported() 方法:
// return a boolean value that is true if the domain/page is supported, element name matches
// supported types, and content is marked as spell checkable
function isSupported (node, hostname) {
const supportedNodeNames = ['TEXTAREA', 'DIV']
const supportedDomains = ['mail.google.com', 'github.com']
if (node.spellcheck && node.isContentEditable) {
if (node.nodeName === 'TEXTAREA') {
return true
}
if (supportedNodeNames.contains(node.nodeName) && supportedDomains.contains(hostname)) {
return true
}
}
return false
}
不幸的是,这段代码会阻止扩展程序在我的本地测试页面上运行,即当 URI 为 file:///home/username/github/multi-dict/test_page/test-page.html
我可以安全地依赖window.location.hostname未定义*并允许扩展在它出现时运行吗?我检查了MDN 上的文档和规范,但我不太清楚在什么确切的上下文中主机名是未定义的。
提前致谢!
*它实际上是一个空字符串,保留在读者/答案上下文的原始描述中。所以问题是 - 我可以安全地依赖window.location.hostname浏览器打开的本地文件是否为空 - 没有本地网络服务器正在运行。
浮云间
相关分类