我正在構建一個使用瀏覽器overlay.xul的附加組件,該組件包含js文件。我發現在網頁上修改HTML的最簡單方法就是我在相信的mdn上找到的這段代碼。即時插入html和標籤
const STATE_START = Components.interfaces.nsIWebProgressListener.STATE_START;
const STATE_STOP = Components.interfaces.nsIWebProgressListener.STATE_STOP;
var myListener = {
QueryInterface: function(aIID) {
if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
aIID.equals(Components.interfaces.nsISupports))
return this;
throw Components.results.NS_NOINTERFACE;
},
onStateChange: function(aWebProgress, aRequest, aFlag, aStatus) { },
onLocationChange: function(aProgress, aRequest, aURI) {
PageLoad.initialzed = false;
PageLoad.init();
},
onProgressChange: function(aWebProgress, aRequest, curSelf, maxSelf, curTot, maxTot) { },
onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) { },
onSecurityChange: function(aWebProgress, aRequest, aState) { }
};
gBrowser.addProgressListener(myListener);
var PageLoad = {
initialzed : false,
browser : null,
domain : null,
appcontent : null,
init : function() {
PageLoad.appcontent = document.getElementById("appcontent");
if (PageLoad.appcontent) {
PageLoad.appcontent.addEventListener("DOMContentLoaded", PageLoad.load, true);
}
},
load : function() {
//I do this so it only calls the function once
if (PageLoad.initialzed == false) {
PageLoad.initialzed = true;
PageLoad.browser = gBrowser.contentDocument;
PageLoad.domain = (PageLoad.browser.location.host.match(/([^.]+)\.\w{2,3}(?:\.\w{2})?$/) || [])[1];
myFunctiontoInsertHTML();
}
}
};
然後我可以在我的功能..這工作不錯迄今使用PageLoad.browser.createElement('div')
等等,但我所遇到的一個問題,即有多個標籤打開,我PageLoad.domain
變量包含最後加載的標籤的域或網頁(這反過來導致在2個或更多不同頁面/標籤上插入html的錯誤)。所以我想改變的是每次用戶點擊不同的標籤或重新加載我想調用該功能的頁面,但我已經陷入了死衚衕。
檢出[在頁面加載](https://developer.mozilla.org/en/Code_snippets/On_page_load)和[url欄列表](https://developer.mozilla.org/en/Code_snippets/Progress_Listeners#例如:_Notification_when_the_value_in_Address_Bar_changes) – 2012-03-13 06:29:14
我正在使用自己的'在頁面加載'鏈接上的變體,但是這個網址欄監聽器正是我所需要的,我想。 – thefoyer 2012-03-19 18:54:28