2016-10-16 11 views
0

私はFirefoxアドオンSDKエクステンションを書いています。タブが固定または固定解除されたときに通知を受けたい。残念ながら、sdk/tabsモジュールはそのようなイベントを提供しません。一部の低レベルのAPIは、ピンを固定/解除するためのイベントを提供していますか?FirefoxアドオンSDKのピン/ピン解除イベントを聞く

答えて

2

FirefoxアドオンSDK Low-Level APIは、固定または固定解除されているタブをリッスンする方法を提供します。 「アドオンSDK低レベルAPI」は、アドオンSDK内でのみ定義されるAPIの非常に特定したリストです。アドオンSDK固有のAPIだけでなく、WebExtensionsを除くほとんどすべてのFirefox APIであるアドオンSDK内で機能するすべてのFirefox APIにリクエストを限定しました。

あなたが関心のあるwindowためのXUL <tabs>要素にTabPinnedTabUnpinnedイベントをリスニングすることにより、このようなイベントを取得することができます。 getTabContainer(window)を使用すると、XUL <tabs>要素を取得できます。すべてのブラウザウィンドウ内でそのようなイベントをすべて取得する場合は、ブラウザウィンドウごとにtabContainer、XUL <tabs>要素にイベントを追加する必要があります。 tab specific events in the MDN Event Reference pageのリストが表示されます。

以下は、すべてのウィンドウでTabPinnedイベントとTabUnpinnedイベントをリッスンするFirefoxアドオンSDKベースの拡張機能です。

index.js

var sdkWinUtils = require('sdk/window/utils'); 
var sdkTabsUtils = require('sdk/tabs/utils'); 
var sdkWindows = require("sdk/windows"); 

//For testing, open the Browser Console 
sdkWinUtils.getMostRecentBrowserWindow() 
      .document.getElementById('menu_browserConsole').doCommand(); 

function tabPinned(event) { 
    let tabId = sdkTabsUtils.getTabId(event.target); 
    console.log('Pinned Tab ID:',tabId); 
} 

function tabUnpinned(event) { 
    let tabId = sdkTabsUtils.getTabId(event.target); 
    console.log('Unpinned Tab ID:',tabId); 
} 

function addTabPinUnpinListenersInWindow(win){ 
    //win can be either a browserWindow provided by the 
    // sdkWindows.browserWindows.on('open',); 
    // event, or nsIDOMWindow instances provided in the array from 
    // sdkWinUtils.windows() 
    // We can distinguish between the two by using 
    // sdkWinUtils.isBrowser(win) 
    let container; 
    if(sdkWinUtils.isBrowser(win)){ 
     container = sdkTabsUtils.getTabContainer(win); 
    }else{ 
     let { viewFor } = require("sdk/view/core"); 
     container = sdkTabsUtils.getTabContainer(viewFor(win)); 
    } 
    container.addEventListener("TabPinned", tabPinned, false); 
    container.addEventListener("TabUnpinned", tabUnpinned, false); 
} 

function removeTabPinUnpinListenersInWindow(win){ 
    let container = sdkTabsUtils.getTabContainer(win); 
    container.removeEventListener("TabPinned", tabPinned, false); 
    container.removeEventListener("TabUnpinned", tabUnpinned, false); 
} 

function forEachOpenWindow(func){ 
    let allWindows = sdkWinUtils.windows('navigator:browser', {includePrivate:true}); 
    for(let win of allWindows){ 
     func(win); 
    } 
} 

function forEachOpenWindowAndAllNewWindows(func){ 
    forEachOpenWindow(func); 
    //Listen for new windows opening and add tab pin/unpin listeners: 
    sdkWindows.browserWindows.on('open',func); 
} 

function removeAllListeners(){ 
    //To be called upon shutdown for add-on upgrade/disable/removal 
    //Remove new window open listener 
    sdkWindows.browserWindows.off('open',addTabPinUnpinListenersInWindow); 
    //Remove tab pin/unpin listeners 
    forEachOpenWindow(removeTabPinUnpinListenersInWindow); 
} 

//Add listeners to all open windows and all which open 
forEachOpenWindowAndAllNewWindows(addTabPinUnpinListenersInWindow); 

//Clean up if Firefox is not shutting down (e.g. the add-on is disabled by user). 
exports.onUnload = reason => { 
    if(reason !== 'shutdown'){ 
     //The add-on is being unloaded and it is not because Firefox is sutting down. 
     removeAllListeners(); 
    } 
}; 

package.json

{ 
    "title": "Listen to tab pin/unpin", 
    "name": "listen-to-tab-pin-unpin", 
    "version": "0.0.1", 
    "description": "Output to console when a tab is pinned or unpinned.", 
    "main": "index.js", 
    "author": "Makyen", 
    "permissions": {"private-browsing": true}, 
    "engines": { 
     "firefox": ">=38.0a1", 
     "fennec": ">=38.0a1" 
    }, 
    "license": "MIT", 
    "keywords": [ 
     "jetpack" 
    ] 
} 
関連する問題