2014-03-04 24 views
1

我想換一個小部件的標籤,當用戶單擊它,然後我寫的代碼如下所示:如何改變控件的標籤(Firefox插件SDK)

var widgets = require("sdk/widget"); 
var statusBar = widgets.Widget({ 
    id: "patchouliStatus", 
    label: "Wait Page Loading...", 
    contentURL: "http://www.mozilla.org/favicon.ico", 
    onClick: function(){ 
     this.contentURL = "http://www.google.com/favicon.ico"; 
     this.label = "Clicked"; 
    } 
}); 

當我點擊小部件,圖標發生了變化,但標籤沒有任何變化。我將鼠標移動到小部件上,它仍顯示「等待頁面加載...」。是否有辦法動態更改標籤?

火狐:v27.0.1

加載項SDK:V1.15

回答

1

Widget的標籤是隻讀的。您必須使用tooltip屬性顯示用戶鼠標懸停文本,這樣說:

var widgets = require("sdk/widget"); 
var statusBar = widgets.Widget({ 
    id: "patchouliStatus", 
    label: "Wait Page Loading...", 
    contentURL: "http://www.mozilla.org/favicon.ico", 
    onClick: function(){ 
     this.contentURL = "http://www.google.com/favicon.ico"; 
     this.tooltip = "Clicked"; 
    } 
}); 

由於docs says somewhere in this section - 我認爲這可能是更清楚documented-,提示值是「可選文本顯示給用戶時的鼠標懸停在小部件上,如果沒有給出,則使用該標籤「。此外,該部分中的示例並不像我認爲應該那樣清楚。

-1

我會在這裏給你非SDK解決方案,但有人會來幫助其轉換爲SDK解決方案。你可以將它粘貼到你的代碼中,但它可以工作。

我不知道如何將元素插入到DOM,但我猜。

var {Cu, Ci} = require('chrome'); //if you want to paste this into scratchpad with with Environemnt set to Browser than dont need this line, this line is for sdk 
var DOMWindows = Services.wm.getWindowEnumerator(null); 
while (DOMWindows.hasMoreElements()) { 
var aDOMWindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow); 
    var myWidget = aDOMWindow.querySelector('#patchouliStatus'); //im not exactly sure how the element is inserted in the dom but im guessing here 
    if (myWidget) { 
    myWidget.label = 'rawr'; 
    } 
} 
+0

如果你可以用這段代碼編譯你的插件並在github上以xpi的形式上傳,它會幫助我幫你 – Noitidart

+0

我導入這樣的服務: const {Cu,Ci} = require('chrome'); Cu.import('resource://gre/modules/Services.jsm');當你運行你的代碼時,firefox會說Services.wm.getWindowEnumerator不是一個函數。(我嘗試了Services.wm.getMostRecentWindow並且它工作)是不是Services.wm.getWindowEnumerator不適用於ff27? – Patchouli

+0

檢查文章[這裏](https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIWindowMediator?redirectlocale=en-US&redirectslug=nsIWindowMediator)它告訴你所有關於Services.wm,如果你編譯它併發布它可以讓你在5分鐘內 – Noitidart

0

好男人感謝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