onInstalled'在Firefox插件SDK扩展

Execute script on 'onInstalled' in Firefox Add-on SDK extension

本文关键字:插件 SDK 扩展 Firefox onInstalled      更新时间:2023-09-26

我对Mozilla扩展开发非常陌生,甚至我刚开始了解扩展和附加组件开发是不同的我对我在MDN (Mozilla Developer Network)上看到的东西很着迷。我想执行脚本'content_script.js',一旦我的插件安装,这样用户就不需要重新启动浏览器。

我只是拖放我的xpi文件并安装它,然后我点击网页上的一个按钮,该按钮向我的附加组件发送消息,但我的附加组件只有在我重新加载网页后才会收听此消息。

//main.js
       var chrome = require("chrome");      
    chrome.runtime.onInstalled.addListener(function(){
             executeScript (contentscript.js) in tabs});
//also I tried
    browser.runtime.onInstalled.addListener

当我知道它是用于Mozilla扩展而不是插件时,我正在尝试这个,因为它给了我浏览器未定义和chrome.runtime未定义的错误。

然后,我在AddonManager.addAddonListener()里面找到了onInstalled()

但是,我仍然很困惑,我应该如何在我的附加组件中使用它。

无论我怎么尝试,它都给我错误。

//main.js
    const { AddonManager } = require("resource://gre/modules/AddonManager.jsm");
    Cu.import("resource://gre/modules/AddonManager.jsm");

    AddonManager.addAddonListener.onInstalled.addListener(function(){
        console.log('installed');
        tabs.executeScript(tabId, "../data/content_script.js", function(result) { console.log('result ='+result); });           
    });

下面的代码删除了错误,但也不工作:

    const { AddonManager } = require("resource://gre/modules/AddonManager.jsm");
    Cu.import("resource://gre/modules/AddonManager.jsm");

    var listener = {
  onInstalled: function(addon) {
      console.log('installed');
      console.log(addon); 
        tabs.executeScript(tabId, "../data/content_script.js", function(result) { console.log('result ='+result); });           
  }
 };
     AddonManager.addAddonListener(listener);

我的包。Json是这样的

    {
  "title": "test",
  "name": "test",
  "version": "1.0.0",
  "description": "test addon corp",
  "main": "lib/main.js",
  "author": "test",
  "engines": {
    "firefox": ">=38.0a1",
    "fennec": ">=38.0a1"
  },
  "license": "MIT",
  "keywords": [
    "jetpack"
  ]
}

main包括main.js,但仍然没有执行,因为当我重新加载页面时,我的附加组件才有效。

我认为我做得完全错了,我在Chrome扩展中做到了这一点,这很容易,但我对Mozilla插件没有任何想法。我甚至连"安装"都达不到。

看来你有几个问题:

  • 你仍然混合了webeextensions和Add-on SDK
    • 插件SDK没有tabs.executeScript()这样的方法,你正在寻找tab.attach()。对于活动选项卡,它将是tabs.activeTab.attach()
  • 当前正在安装的附加组件的AddonManager onInstalled事件发生先于正在运行的附加组件的任何代码。因此,您将不会收到正在侦听的附加组件的此事件。
    • AddonManager主要用于让您的附加组件能够监视涉及其他附加组件的活动。它在监视进行监视的附加组件的活动方面的有用性是有限的。
    • 你可以得到你的AddonManager onDisabled事件,如果用户禁用之前删除它的附加组件。如果用户直接删除它,你只得到一个onUninstalling事件。
  • 你可能会遇到这样的问题,即console.log()从内容脚本不显示在浏览器控制台,当内容脚本被加载的附加组件作为主要附加组件正在测试jpm run加载时。输出在执行jpm run的控制台窗口中可用。虽然它是由设计输出到控制台窗口,我觉得这是一个错误或错误的功能,输出不去浏览器控制台(除了控制台窗口)。
  • 将内容脚本加载到about:*页面会导致一些不同于将其加载到正常网页的行为。这种行为可以包括使console.log()输出只显示为使用dump()输出(即它不显示在浏览器控制台,但显示在执行Firefox/jpm run的控制台)。如果你想这样做,你需要试验一下你能做什么。
  • 你已经说过,"我认为我的main.js在安装后没有运行。"这种想法可能是由于没有在正确的位置查找console.log()输出,即浏览器控制台。因此,我让下面的附加组件自动打开浏览器控制台。
    • 包中指定的JavaScript文件。json key "main"在安装插件时运行。它也在Firefox启动时运行。
    • 你可以有一个exports.main函数,在你的"main" JavaScript文件中的代码被评估和执行后自动调用。此函数可用于查找执行附加组件的原因。可能原因为:installenablestartupupgradedowngrade。下面的附加组件演示了何时执行此代码并将reason传递给它。

为了演示各种各样的东西,我编写了一个Firefox插件SDK扩展,它既加载了几个内容脚本,又监听了AddonManager事件。我制作了这个插件的两个版本,它们只是在各自的包中分配给每个插件的名称和ID不同。json 文件。这两个附加组件是installinfo@ex1installinfo@ex2

第一个附加组件installinfo@ex1通过在其目录中运行jpm run来加载。第二个附加组件installinfo@ex2通过拖放来安装。xpijpm xpi创建。使用Firefox UI,我立即导航到about:addons (Ctrl-Shift-ACmd-Shift-A在OSX上)并继续首先禁用installinfo@ex2;然后"移除"installinfo@ex2;然后刷新about:addons页面,使其无法"撤销"删除。然后退出Firefox主浏览器窗口。

外接程序有大量输出到控制台,因此您可以查看事情发生的顺序以及哪个外接程序能够接收哪个AddonManager事件。控制台输出如下[括号内的是我对用户界面所做的操作,以及一些注释]:

[User action: Start Firefox in installinfo@ex1 directory]
    installinfo@ex1: In index.js
    installinfo@ex1: In installAddonListener: Adding add-on listener
    installinfo@ex1: Attaching content script A
    installinfo@ex1: In exports.main: This add-on is being loaded for reason= install Object { loadReason: "install", staticArgs: undefined }
    installinfo@ex1: Attaching content script B
    installinfo@ex1: In exports.main: was passed callbacks= Object { print: print(), quit: function () }
[Note: no console.log output from within conentScriptA loading]
    installinfo@ex1: received message from contentScriptA: Is Loaded
[Note: no console.log output from within conentScriptB loading]
    installinfo@ex1: received message from contentScriptB: Is Loaded
[User action: Drag and drop .xpi for installinfo@ex2 onto Firefox]
    installinfo@ex1: AddonManager Event: Installing addon ID: installinfo@ex2 ::needsRestart= false ::addon object: Object {  }
    installinfo@ex1: AddonManager Event: Installed addon ID: installinfo@ex2 ::addon object: Object {  }
            installinfo@ex2: In index.js
            installinfo@ex2: In installAddonListener: Adding add-on listener
            installinfo@ex2: Attaching content script A
            installinfo@ex2: In exports.main: This add-on is being loaded for reason= install Object { loadReason: "install", staticArgs: undefined }
            installinfo@ex2: Attaching content script B
            installinfo@ex2: In exports.main: was passed callbacks= Object { print: print(_), quit: function () }
                installinfo@ex2: In contentScriptA: Loaded
            installinfo@ex2: received message from contentScriptA: Is Loaded
                installinfo@ex2: In contentScriptB: Loaded
            installinfo@ex2: received message from contentScriptB: Is Loaded
[User action: Navigate to about:addons]
[User action: Disable installinfo@ex2]
    installinfo@ex1: AddonManager Event: Disabling addon ID: installinfo@ex2 ::needsRestart= false ::addon object: Object {  }
            installinfo@ex2: AddonManager Event: Disabling addon ID: installinfo@ex2 ::needsRestart= false ::addon object: Object {  }
            installinfo@ex2: In exports.onUnload: This add-on is being unloaded for reason= disable
            installinfo@ex2: In removeAddonListener: Removing add-on listener
    installinfo@ex1: AddonManager Event: Disabled addon ID: installinfo@ex2 ::addon object: Object {  }
            installinfo@ex2: AddonManager Event: Disabled addon ID: installinfo@ex2 ::addon object: Object {  }
    installinfo@ex1: AddonManager Event: Uninstalling addon ID: installinfo@ex2 ::needsRestart= true ::addon object: Object {  }
[Get a warning in Browser Console because installinfo@ex2 did not remove its AddonManager listeners, and AddonManager is still trying to call them.]
            1472563865661   addons.manager   WARN   AddonListener threw exception when calling onUninstalling: TypeError: can't access dead object (resource://gre/modules/AddonManager.jsm:1756:1) JS Stack trace: AddonManagerInternal.callAddonListeners@AddonManager.jsm:1756:1 < this.AddonManagerPrivate.callAddonListeners@AddonManager.jsm:3075:5 < this.XPIProvider.uninstallAddon@XPIProvider.jsm:5041:7 < AddonWrapper.prototype.uninstall@XPIProvider.jsm:7484:5 < uninstall@extensions.xml:1548:13 < oncommand@about:addons:1:1
[User action: Refresh about:addons page to remove "undo" posibility for installinfo@ex2]
    installinfo@ex1: Uninstalled addon ID: installinfo@ex2 ::addon object: Object {  }
[Get a warning in Browser Console because installinfo@ex2 did not remove its AddonManager listeners, and AddonManager is still trying to call them.]
            1472563873408   addons.manager   WARN   AddonListener threw exception when calling onUninstalled: TypeError: can't access dead object (resource://gre/modules/AddonManager.jsm:1756:1) JS Stack trace: AddonManagerInternal.callAddonListeners@AddonManager.jsm:1756:1 < this.AddonManagerPrivate.callAddonListeners@AddonManager.jsm:3075:5 < this.XPIProvider.uninstallAddon@XPIProvider.jsm:5096:7 < AddonWrapper.prototype.uninstall@XPIProvider.jsm:7484:5 < doPendingUninstalls@extensions.js:1740:5 < gListView.hide@extensions.js:2733:5 < gViewController.shutdown@extensions.js:651:7 < shutdown@extensions.js:184:3 < EventListener.handleEvent*@extensions.js:84:1
[User action: Close main Firefox browser window]
[User action: Close Firefox Browser Console window]
    (via dump):installinfo@ex1: In exports.onUnload: This add-on is being unloaded for reason= shutdown

我建议你尝试下面的附加组件,以了解AddonManager事件的可能性以及代码何时在您的附加组件中执行。

附加文件:

index.js :

/* Firefox Add-on SDK test when code is executed upon install*/
//For testing:
var doNotRemoveAddonManagerListenersUponUninstall = true
var self = require("sdk/self");
var tabs = require("sdk/tabs");
var myId = self.id;
let myIdText = myId;
if(myId.indexOf('2') > -1){
    //Indent console logs for version 2
    myIdText = ''t't' + myIdText ;
}
var utils = require('sdk/window/utils');
activeWin = utils.getMostRecentBrowserWindow();
//This will execute every time the add-on is loaded (e.g. install, startup, enable, etc).
myLog('In index.js');
//Open the Browser Console
activeWin.document.getElementById('menu_browserConsole').doCommand();
function myLog(){
    // Using activeWin.console.log() is needed to have output to the
    // Browser Console when installed as an .xpi file.  In that case,
    // console is mapped to dump(), which does not output to the console. 
    // This is done, I assume, to not polute the console from SDK add-ons
    // that are logging information when they should not.  Using jpm run,
    // console.log outputs to the Browser Console.
    let activeWin = require('sdk/window/utils').getMostRecentBrowserWindow();
    // If Firefox is exiting (and some other conditions), there is
    // no active window.  At such times, we must use the version
    // of console.log() aliases to dump().
    let useConsole = console;
    if(activeWin ){
        //useConsole = activeWin.console;
    }
    useConsole.log(myIdText +':',...arguments);
}
function attachScript(script){
    let tabWorker = tabs.activeTab.attach({
        contentScriptFile: script,
        contentScriptOptions: {
            //extra 't because content script console.log (from .xpi) doesn't prefix with
            //  add-on name.
            idText: ''t't't' + myIdText
        }
    });
    tabWorker.port.on('consoleLog',logMessage);
    return tabWorker;
}
function logMessage(message){
    myLog(message);
}
exports.main = function (options,callbacks) {
    myLog('In exports.main: This add-on is being loaded for reason=', options.loadReason
          , options);
    myLog('Attaching content script B');
    attachScript('./contentScriptB.js');
    if(typeof callbacks !== 'undefined'){
        myLog('In exports.main: was passed callbacks=', callbacks);
    }
    switch (options.loadReason) {
        case 'install':
            //Do actions upon install
            break;
        case 'enable':
            //Do actions upon enable
            break;
        case 'startup':
            //Do actions upon startup
            break;
        case 'upgrade':
            //Do actions upon upgrade
            break;
        case 'downgrade':
            //Do actions upon downgrade
            break;
        default:
            throw 'Unknown load reason:' + options.loadReason;
            break; //Not needed, throw will exit
    }
};
exports.onUnload = function (reason) {
    myLog('In exports.onUnload: This add-on is being unloaded for reason=',reason);
    //Your add-on listeners are NOT automatically removed when
    //  your add-on is disabled/uninstalled. 
    //You MUST remove them in exports.onUnload if the reason is
    //  not 'shutdown'.  If you don't, errors will be shown in the
    //  console for all events for which you registered a listener.
    if(reason !== 'shutdown') {
        uninstallAddonListener();
    }
};

const { AddonManager } = require("resource://gre/modules/AddonManager.jsm");
let addonListener = {
    onEnabling: function(addon, needsRestart){
        myLog('AddonManager Event: Enabliling addon ID: ' + addon.id 
              + ' ::needsRestart= ' + needsRestart + ' ::addon object:', addon);
    },
    onEnabled: function(addon){
        myLog('AddonManager Event: Enabled addon ID: ' + addon.id 
              + ' ::addon object:', addon);
    },
    onDisabling: function(addon, needsRestart){
        myLog('AddonManager Event: Disabling addon ID: ' + addon.id 
              + ' ::needsRestart= ' + needsRestart + ' ::addon object:', addon);
    },
    onDisabled: function(addon){
        myLog('AddonManager Event: Disabled addon ID: ' + addon.id 
              + ' ::addon object:', addon);
    },
    onInstalling: function(addon, needsRestart){
        myLog('AddonManager Event: Installing addon ID: ' + addon.id 
              + ' ::needsRestart= ' + needsRestart + ' ::addon object:', addon);
    },
    onInstalled: function(addon){
        myLog('AddonManager Event: Installed addon ID: ' + addon.id 
              + ' ::addon object:', addon);
    },
    onUninstalling: function(addon, needsRestart){
        myLog('AddonManager Event: Uninstalling addon ID: ' + addon.id 
              + ' ::needsRestart= ' + needsRestart + ' ::addon object:', addon);
    },
    onUninstalled: function(addon){
        myLog('AddonManager Event: Uninstalled addon ID: ' + addon.id 
              + ' ::addon object:', addon);
    },
    onOperationCancelled: function(addon){
        myLog('AddonManager Event: Add-on Operation Canceled addon ID: ' 
              + addon.id + ' ::addon object:', addon);
    },
    onPropertyChanged: function(addon, properties){
        myLog('AddonManager Event: Add-on Property Changed addon ID: ' + addon.id 
              + ' ::properties= ', properties, ' ::addon object:', addon);
    }
}

function installAddonListener(){
    //Using an AddonManager listener is not effective to listen for your own add-on's
    //  install event.  The event happens prior to you adding the listener.
    myLog('In installAddonListener: Adding add-on listener');
    AddonManager.addAddonListener(addonListener);
}
function uninstallAddonListener(){
    if(doNotRemoveAddonManagerListenersUponUninstall === true){
        //Do the WRONG thing to demonstrate what happens when you don't
        //  remove AddonManager listeners upon your add-on being disabled.
        return;
    }
    //Using an AddonManager listener is not effective to listen for your own add-on's
    //  install event.  The event happens prior to you adding the listener.
    myLog('In removeAddonListener: Removing add-on listener');
    AddonManager.removeAddonListener(addonListener);
}
installAddonListener();
myLog('Attaching content script A');
attachScript('./contentScriptA.js');

数据/contentScriptA :

//console.log is ailiased to dump() when running under jpm run. Thus,
//  you will not see the output from this line in the Browser Console
//  when run under jpm run.  It will appear in the console window from 
//  which you executed 'jpm run'
//  From an .xpi it outputs to the Browser Console, as expected.
console.log(self.options.idText + ': In contentScriptA: Loaded');
//Send a message to the background script that this content script is loaded.
self.port.emit('consoleLog', 'received message from contentScriptA: Is Loaded');

数据/contentScriptB :

//console.log is ailiased to dump() when running under jpm run. Thus,
//  you will not see the output from this line in the Browser Console
//  when run under jpm run.  It will appear in the console window from 
//  which you executed 'jpm run'
//  From an .xpi it outputs to the Browser Console, as expected.
console.log(self.options.idText + ': In contentScriptB: Loaded');
//Send a message to the background script that this content script is loaded.
self.port.emit('consoleLog', 'received message from contentScriptB: Is Loaded');

包。Json (对于installinfo@ex1):

{
    "title": "Demo when code executes re install 1",
    "name": "installinfo1",
    "id": "installinfo@ex1",
    "version": "0.0.1",
    "description": "Demo when execute various code with respect to install",
    "main": "index.js",
    "author": "Makyen",
    "engines": {
        "firefox": ">=38.0a1",
        "fennec": ">=38.0a1"
    },
    "license": "MIT",
    "keywords": [
        "jetpack"
    ]
}

包。Json (对于installinfo@ex2):

{
    "title": "Demo when code executes re install 2",
    "name": "installinfo2",
    "id": "installinfo@ex2",
    "version": "0.0.1",
    "description": "Demo when execute various code with respect to install",
    "main": "index.js",
    "author": "Makyen",
    "engines": {
        "firefox": ">=38.0a1",
        "fennec": ">=38.0a1"
    },
    "license": "MIT",
    "keywords": [
        "jetpack"
    ]
}