所以我收集了5種不同的方法來做到這一點,其中沒有一種方法可以在面板內工作。 Firefox在阻止訪問基本任務方面效果驚人。從Firefox插件面板中獲取當前網址
這是我已經試過:
嘗試1:
var url = window.top.getBrowser().selectedBrowser.contentWindow.location.href;
錯誤:window.top.getBrowser不是一個函數
嘗試2:
var url = window.content.document.location;
錯誤:權限被拒絕訪問屬性 '文件'
嘗試3:
var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) .getInterface(Components.interfaces.nsIWebNavigation) .QueryInterface(Components.interfaces.nsIDocShellTreeItem) .rootTreeItem .QueryInterface(Components.interfaces.nsIInterfaceRequestor) .getInterface(Components.interfaces.nsIDOMWindow); var url = mainWindow.getBrowser().selectedBrowser.contentWindow.location.href;
錯誤:權限被拒絕類的對象創建包裝UnnamedClass
嘗試4:
var url = window.content.location.href;
錯誤:權限被拒絕訪問屬性'href'
嘗試5:
var currentWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser"); var currBrowser = currentWindow.getBrowser(); var url = currBrowser.currentURI.spec;
錯誤:權限被拒絕獲得財產XPCComponents.classes
這個編碼爲Chrome是一件輕而易舉的事。不知道爲什麼這對FF來說太難了。
任何人都有解決方案嗎?
https://developer.mozilla.org/en/Code_snippets/Tabbed_browser&沒有ü嘗試以'alert'的網址? – linguini
錯誤消息顯示您正在運行非特權代碼(嘗試3通常是正確的方法)。不允許未經授權的代碼訪問瀏覽器的用戶界面。如果你解釋你如何運行這個代碼,有人可能會告訴你爲什麼它是無特權的。 –