Firefox扩展开发:获取新标签的URL

cod*_*der 2 firefox-addon

任何人都可以告诉我如何在Firefox上获取下一个选项卡的URl?我现在正在使用它:

//The browser object points to the new tab which I capture using the
//'TabOpen' Event
var browser = gBrowser.getBrowserForTab(event.target);

//From where can I get the URL of this new tab ? Also, how to I get
//the Title of this new Tab
Run Code Online (Sandbox Code Playgroud)

提前致谢..

Nic*_*lay 14

让我们一起来解决这个问题.首先搜索谷歌"getBrowserForTab",看看它返回什么样的对象.您将看到一个页面,其中第一个命中为示例,第二个命中为参考页面.后者是我们正在寻找的.它说:

[getBrowserForTab(tab)]返回指定选项卡元素的浏览器.

按照浏览器的链接查看此对象具有的属性和方法.

您将看到它具有contentTitle属性("此只读属性包含浏览器中文档对象的标题."),它回答了问题的第二部分.

同样,您会看到它有一个currentURI属性,返回"当前加载的URL".返回的对象是一个nsIURI,以获取您需要使用的字符串表示形式currentURI.spec,如nsIURI文档中所述.

总结一下:

var title = browser.contentTitle; // returns the title of the currently loaded page
var url = browser.currentURI.spec; // returns the currently loaded URL as string
Run Code Online (Sandbox Code Playgroud)

您还可以通过/ 获取内容页面的window/ document对象,并使用您在常规网页中使用的API获取标题/ URL(以及其他内容).browser.contentWindowbrowser.contentDocument

希望这会有所帮助,下次你提出问题时你会尝试自己这样做(如果你找不到文档或者无法理解它,请说明你遇到的具体问题).