从 Firefox 侧边栏扩展获取当前页面 URL
我正在为 Firefox 编写一个侧边栏扩展,需要一种方法来获取当前页面的 URL,以便我可以根据数据库检查它并显示结果。 我怎样才能做到这一点?
I'm writing a sidebar extension for Firefox and need a way to get the URL of the current page so I can check it against a database and display the results. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我在寻找同一问题的答案时偶然发现了这篇文章。
实际上,我认为这很简单
另请参阅 https://developer.mozilla.org/en/ DOM/window.content
I stumbled over this post while looking for an answer to the same question.
Actually I think it's as easy as
See also https://developer.mozilla.org/en/DOM/window.content
可能有用,否则我认为你需要使用:
might work, otherwise I think you need to use:
这似乎对我来说效果很好
This seems to work fine for me
https://developer.mozilla.org/En/Working_with_windows_in_chrome_code
如果您需要访问主从侧边栏中运行的代码浏览器中,您会看到类似于 Wimmel 发布的内容,除了最后一行可以简化为
(或者您可以使用返回 nsIURI 的 API)。
根据您的任务,在浏览器窗口中运行代码(例如在页面加载处理程序中)可能是有意义的,然后它可以通过
content
快捷方式访问当前页面,并通过 <代码>document.getElementById("sidebar").contentDocument 或.contentWindow
。https://developer.mozilla.org/En/Working_with_windows_in_chrome_code
If you need to access the main browser from the code running in a sidebar, you'll something like what Wimmel posted, except the last line could be simplified to
(alternatively you could use 's API returning an nsIURI).
Depending on your task, it might make sense to run the code in the browser window instead (e.g. in a page load handler), then it can access the current page via the
content
shortcut and the sidebar viadocument.getElementById("sidebar").contentDocument
or.contentWindow
.如果您只需要域名和子域名;
用法;
代码;
If you need only domain and subdomain;
Usage;
Code;
来自 Firefox 扩展弹出窗口;
您的清单或可能需要
tabs
而不是activeTab
From a Firefox extension popup ;
You'll need
in your manifest or possibly
tabs
instead ofactiveTab
这适用于 Firefox“弹出”扩展。
This works from a firefox "popup" extension.
你好,
我尝试在 JavaScript 中实现这个,因为我的项目中也需要它,但是所有三种可能的解决方案都不起作用。 我还实现了一个小站点来测试它,但这也不起作用。
这是小网站的源代码:
Hallo,
I have tried to implement this in JavaScript, because I need that in my project too, but all three possible solutions didn't work. I have also implemented a small site to test it, but this also didn't work.
Here is the source code of the small site: