2011-06-15 70 views
0

我從插件開發網站的示例代碼已成功地將一個按鈕放到FF :) 現在我想讓該按鈕做一些有趣的事情,所以我想我會運行一個提醒與目前在欄中的地址...但是這不工作:Firefox插件/ javascript:從網址獲取網址

CustomButton = { 

1: function() { 
    alert("Just testing 1"+document.location.href); 
    }, 

} 

除了+document.location.href這是確切的演示代碼我從開發站點了...

回答

1

那些工作我,你在用什麼語境/你如何調用函數?

> document.location.href 
< "http://stackoverflow.com/questions/6352035/firefox-addon-javascript-get-url-from-bar" 

您還可以使用window.location.href

> window.location.href 
< "http://stackoverflow.com/questions/6352035/firefox-addon-javascript-get-url-from-bar" 
+0

當我嘗試,我在我的警告得到這樣的: 只是測試1chrome://browser/content/browser.xul – Ryan 2011-06-15 02:03:22

+1

如果我猜的話聽起來就像你在窗口的鑲邊(菜單,按鈕等)的上下文中執行的那樣,而不是實際的頁面。你可能想看看http://blog.mozilla.com/addons/2009/01/28/how-to-develop-a-firefox-extension/ – Robert 2011-06-15 02:06:16

+0

好吧,這樣做的工作: var urlbar = window.content.location.href; alert(「Just testing 1」+ urlbar); 感謝您的回覆! – Ryan 2011-06-15 02:06:29

2

你想獲取當前文檔或在地址欄中的字符串的位置?

對於當前文檔

content.location.href 

的位置在位置欄中的字符串

document.getElementById("urlbar").value 

gURLBar.value 
3

你應該注意到,在擴展開發,文檔和窗口變量是指到主機瀏覽器而不是包含該網站的瀏覽器。 你應該使用

gBrowser.selectedTab

用於獲取當前選項卡,然後使用

currentURI.host

用於獲取URL主機 還指出,selectedTab返回一個標籤變量,那麼你應該得到該標籤的窗口。 那麼整個代碼將是:

gBrowser.getBrowserForTab(gBrowser.selectedTab).currentURI.host

0

花了一段時間來弄清楚這一切了:

gBrowser.currentURI.spec; 

是怎麼你做吧。

下面是一些代碼,你可能會發現有用

window.addEventListener('load', function (e) { 

    var href = gBrowser.currentURI.spec; 

    if (href.match(/website.com/)) { 

    var contentDoc = content.document; 
    // Do some stuff to the current website.com's DOM 

    } 

}, true);