在Firefox扩展上使用Javascript动态获取网页的URL [英] Get URL of a webpage dynamically with Javascript on a Firefox extension

查看:127
本文介绍了在Firefox扩展上使用Javascript动态获取网页的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有Firefox扩展的Javascript动态获取网页的URL(即,如果URL更改获取新的URL).

I'd like to get the URL of a webpage dynamically (i.e if the url changes get the new url) using Javascript with a Firefox extension.

到目前为止,我已经尝试使用附加到当前窗口的事件侦听器,但是它不起作用. (使用Firefox扩展程序显示网页当前URL )

So far I've tried to use an event listener attached to the current window but it doesn't work. ( Display Webpage current URL with Firefox extension )

有人可以发布一些代码来向我展示实现此目标的方法吗?

Can someone post some code to show me a way to achieve this please ?

推荐答案

可以将事件侦听器添加到URL栏中(我在评论中解释了为什么您的旧问题答案中的代码无效),但坦率地说-这不是最好的方法.例如,如果用户开始输入,URL栏内容也可以更改.用户甚至可以选择从浏览器窗口中删除URL栏.

You could add an event listener to the URL bar (I explained in a comment why the code in the answer to your old question didn't work) but frankly - this isn't the best way. URL bar contents can also change if the user starts typing into it for example. And the user could even choose to remove the URL bar from the browser window.

实现此目标的最佳方法是实现进度侦听器.您可以在 https://developer.mozilla.org/en/Code_snippets/Progress_Listeners上找到示例代码和说明. .您将对onLocationChange方法的调用感兴趣,该调用将在每次需要更改URL栏内容时发生(同样,当用户在选项卡之间切换时).

The best way to achieve this is implementing a progress listener. You can find example code and explanation on https://developer.mozilla.org/en/Code_snippets/Progress_Listeners. You would be interested in calls to the onLocationChange method, that will happen every time the URL bar contents need to change (also when the user switches between tabs).

这篇关于在Firefox扩展上使用Javascript动态获取网页的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆