javascript - 在 Firefox Add-on SDK 中监听固定/取消固定选项卡事件

标签 javascript firefox firefox-addon firefox-addon-sdk

我正在编写一个 Firefox Add-on SDK 扩展。我希望在固定或取消固定任何选项卡时收到通知。不幸的是,sdk/tabs模块不提供此类事件。是否有一些低级 API 提供固定/取消固定选项卡的事件?

最佳答案

没有 Firefox 附加 SDK Low-Level API它提供了一种监听选项卡被固定或取消固定的方法。 “附加 SDK 低级 API”是非常具体的 API 列表,仅在附加 SDK 中定义。您并没有将您的请求限制为仅针对附加 SDK 特定的 API,而是针对在附加 SDK 中运行的所有 Firefox API,这几乎是除 WebExtensions 之外的所有 Firefox API。

您可以通过监听 TabPinned 来获取此类事件和 TabUnpinned XUL 上的事件 <tabs> window 的元素您感兴趣的内容。您可以获取 XUL <tabs> 使用 getTabContainer(window) 元素。如果您想获取所有浏览器窗口中的所有此类事件,则需要将事件添加到 tabContainer ,XUL <tabs>元素,对于每个浏览器窗口。您可以看到 tab specific events in the MDN Event Reference page 的列表.

以下是基于 Firefox Add-on SDK 的扩展,它将监听 TabPinnedTabUnpinned所有窗口中的事件。

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"
    ]
}

关于javascript - 在 Firefox Add-on SDK 中监听固定/取消固定选项卡事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40069179/

相关文章:

javascript - 在 Firefox 24 及更多版本中使用 javascript 关闭浏览器而不显示确认框

firefox - 从 Firefox 扩展操作 Html

javascript - AJAX 中的缓存结果

javascript - jQuery 附加 Handlebars

javascript - 将多个数组参数传递到 setTimeout 进行幻灯片放映时出现问题

javascript - 在Rails中生成VAPID公钥并将其传递给Javascript

javascript - 如何在ajax "success:"之后弹出

html - Firefox:如何将 div 绝对定位在 td 中

javascript - 使用 Firefox 扩展插入 CSS

FireFox SDK keydown/keyup 事件