Firefox扩展开发:获取新标签的URL [英] Firefox extension development : Get URL of new tab

查看:236
本文介绍了Firefox扩展开发:获取新标签的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何获得Firefox上的下一个标签的URL?我现在正在使用它:

  //浏览器对象指向使用
/ /'TabOpen'Event
var browser = gBrowser.getBrowserForTab(event.target);

//从哪里可以得到这个新标签的网址?另外,如何获得
//这个新标签的标题

..

解决方案

让我们一起来看看。首先在google搜索getBrowserForTab来查看它返回的是什么样的对象。您将看到一个页面,其中包含第一个匹配的示例,以及参考页面作为第二击。后者是我们正在寻找的。它说:


浏览器的指定标签元素。

请点击

同样,你会发现它有一个 currentURI 属性,它返回当前加载的URL。返回的对象是一个 nsIURI ,以获得它的字符串表示形式,您需要使用 currentURI.spec nsIURI文档



up:

  var title = browser.contentTitle; //返回当前加载页面的标题
var url = browser.currentURI.spec; //将当前加载的URL作为字符串返回

您也可以获取通过 browser.contentWindow / <$ c $ / 文档 c> browser.contentDocument 并使用您在常规网页中使用的API获取标题/ URL(以及其他内容)。



希望这会有所帮助,下次您提出问题时,您将自己尝试执行此操作(如果找不到文档或难以理解,请指出遇到的具体问题)。

Can anybody tell me how to get the URl of the next tab on firefox ? I am using this now :

//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

Thanks in advance..

解决方案

Let's try to figure this out together. First search google for "getBrowserForTab" to see what kind of object it returns. You'll see a page with examples as the first hit, and the reference page as the second hit. The latter is what we're looking for. It says:

[getBrowserForTab( tab )] Returns a browser for the specified tab element.

Follow the link for browser to see what properties and methods this object has.

You'll see it has a contentTitle property ("This read-only property contains the title of the document object in the browser."), which answers the second part of your question.

Similarly you see it has a currentURI property which returns "the currently loaded URL". The returned object is an nsIURI, to get its string representation you need to use currentURI.spec, as described in the nsIURI documentation.

So to sum up:

var title = browser.contentTitle; // returns the title of the currently loaded page
var url = browser.currentURI.spec; // returns the currently loaded URL as string

You could also just get the window/document objects of the content page via browser.contentWindow/browser.contentDocument and get the title/URL (and other things) using the APIs you would use in a regular web page.

Hope this helps and you'll try to do this yourself next time you ask a question (and if you can't find the documentation or have trouble understanding it, indicate what specific problems you encountered).

这篇关于Firefox扩展开发:获取新标签的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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