好男人感謝XPI,changeLabel
功能改變這一點,我上面真的竊聽。
function changeLabel(str){
var DOMWindows = Services.wm.getEnumerator('navigator:browser');
while (DOMWindows.hasMoreElements()) {
var aDOMWindow = DOMWindows.getNext();
var myWidget = aDOMWindow.document.getElementById('widget:[email protected]');
if (myWidget) {
Services.appShell.hiddenDOMWindow.console.info('myWidget:', myWidget);
myWidget.setAttribute('label', str);
myWidget.setAttribute('tooltiptext', 'tooltip changed');
} else {
Services.appShell.hiddenDOMWindow.console.info('myWidget null:', myWidget);
}
}
}
它也似乎是你的小部件的ID開始與泰斗addon id名稱。
現在我給了你枚舉函數,因爲它覆蓋了所有的窗口,你可以添加事件監聽器。但是,如果你只想定位被點擊的窗口,只需要獲取最近的窗口,因爲我們只需點擊該窗口,事件偵聽器就會觸發點擊,這顯然會在窗口中顯示正確的窗口。
變化changeLabel
這樣:
function changeLabel(str){
var aDOMWindow = Services.wm.getMostRecentWindow('navigator:browser');
var myWidget = aDOMWindow.document.getElementById('widget:[email protected]');
if (myWidget) {
Services.appShell.hiddenDOMWindow.console.info('myWidget:', myWidget);
myWidget.setAttribute('label', str);
myWidget.setAttribute('tooltiptext', 'tooltip changed');
} else {
Services.appShell.hiddenDOMWindow.console.info('myWidget null:', myWidget);
}
}
也是Services.appShell.hiddenDOMWindow.console.info只是一些好聽的調試,我把它在那裏,所以你可以看到它是如何工作的。它會記錄到「瀏覽器控制檯」(Ctrl + Shift + J)。
作爲最後一點,我使用了非sdk解決方案,要求使用chrome。他們勸你不要這麼做,因爲他們希望你使用SDK的功能我不知道SDK,但你可以通過要求window/utils
它看起來像使用的GetEnumerator和recentWindow功能:
讀window/utils article here
如果你可以用這段代碼編譯你的插件並在github上以xpi的形式上傳,它會幫助我幫你 – Noitidart
我導入這樣的服務: const {Cu,Ci} = require('chrome'); Cu.import('resource://gre/modules/Services.jsm');當你運行你的代碼時,firefox會說Services.wm.getWindowEnumerator不是一個函數。(我嘗試了Services.wm.getMostRecentWindow並且它工作)是不是Services.wm.getWindowEnumerator不適用於ff27? – Patchouli
檢查文章[這裏](https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIWindowMediator?redirectlocale=en-US&redirectslug=nsIWindowMediator)它告訴你所有關於Services.wm,如果你編譯它併發布它可以讓你在5分鐘內 – Noitidart