2010-02-09 87 views
2

任何人都可以告訴我如何獲得Firefox上下一個標籤的URl嗎?提前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 

謝謝..

回答

14

讓我們試着想出解決辦法在一起:我現在用的這個。先看看search google for "getBrowserForTab"看看它返回什麼樣的對象。你會看到一個頁面,其中有第一個命中的例子,第二個命中的是reference page。後者是我們正在尋找的。它說:

[getBrowserForTab(tab)]返回指定tab元素的browser

請查看browser的鏈接,瞭解此對象具有哪些屬性和方法。

您會看到它有一個contentTitle屬性(「此只讀屬性包含瀏覽器中文檔對象的標題」),它將回答問題的第二部分。

同樣,你會看到它有一個currentURI屬性,它返回「當前加載的URL」。返回的對象是nsIURI,要獲取其字符串表示形式,您需要使用currentURI.spec,如nsIURI documentation中所述。

所以總結起來:

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

你也只是通過browser.contentWindow/browser.contentDocument獲得內容頁面的window/document對象,並使用你將這些API拿到冠軍/ URL(和其他東西)在常規網頁中使用。

希望這會有所幫助,下次您提出問題時(如果找不到文檔或無法理解它,請指出遇到的具體問題),您將自行嘗試執行此操作。

+0

哇..非常感謝。 :) – coder 2010-02-10 22:13:12