使用哪种方法访问页面 URL?
Which method to use to access page URLs?
对于 WebExtensions,有几种方法可以检查页面的 URL:
window.location.href
在 content script,
- the
webRequest
API,以及
tabs.onUpdated
.
当您只需要检查 URL 并可能基于它进行重定向时使用哪个?哪个最容易编码?使用其他的有什么理由吗?
对于您的特定用例,最好的方法是
window.location.href
如果您只想获取域名而不是完整的URL,请使用
window.location.hostname
有关 Window 位置 API 的完整指南,请查看 https://developer.mozilla.org/en-US/docs/Web/API/Window/location
注意:您提到的 document.location.href
在某些版本的 Firefox 浏览器上不受支持
对于 WebExtensions,有几种方法可以检查页面的 URL:
window.location.href
在 content script,- the
webRequest
API,以及 tabs.onUpdated
.
当您只需要检查 URL 并可能基于它进行重定向时使用哪个?哪个最容易编码?使用其他的有什么理由吗?
对于您的特定用例,最好的方法是
window.location.href
如果您只想获取域名而不是完整的URL,请使用
window.location.hostname
有关 Window 位置 API 的完整指南,请查看 https://developer.mozilla.org/en-US/docs/Web/API/Window/location
注意:您提到的 document.location.href
在某些版本的 Firefox 浏览器上不受支持