Reputation: 8808
I am writing a Firefox Add-on SDK extension. I want to be notified when any tab is pinned or unpinned. Unfortunately, the sdk/tabs
module doesn't provide such events. Is there some low-level API which provides events for pinning/unpinning tabs?
Upvotes: 0
Views: 168
Reputation: 33296
There is no Firefox Add-on SDK Low-Level API which provides a way to listen for tabs being pinned, or unpinned. The "Add-on SDK Low-Level APIs" are a very specific list of APIs which are defined only within the Add-on SDK. You did not limit your request to just the Add-on SDK specific APIs, but to all Firefox APIs which function within the Add-on SDK, which is almost all Firefox APIs, except WebExtensions.
You can get such events by listening for the TabPinned
and TabUnpinned
events on the XUL <tabs>
element for the window
in which you are interested. You can get the XUL <tabs>
element by using getTabContainer(window)
. If you want to get all such events within all browser windows, you will need to add the events to the tabContainer
, the XUL <tabs>
element, for each browser window. You can see a list of the tab specific events in the MDN Event Reference page.
The following is a Firefox Add-on SDK based extension which will listen for TabPinned
and TabUnpinned
events in all windows.
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"
]
}
Upvotes: 2