我正在創建一個在每個頁面上運行但僅在某些上下文中工作的網絡擴展。這是 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 checkablefunction 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瀏覽器打開的本地文件是否為空 - 沒有本地網絡服務器正在運行。
我可以安全地依賴主機名只為本地文件未定義(即導航到本地文件)嗎?
慕虎7371278
2021-12-23 10:35:44