我正在创建一个web扩展,它运行在每个页面上,但只在特定的上下文中工作。下面是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
和
the spec
但我不太清楚在什么样的上下文下主机名是未定义的。
提前谢谢!
窗口.location.hostname
仅当浏览器打开的本地文件为空时-没有运行本地Web服务器。