首次运行Mozilla附加组件时执行代码 [英] Mozilla Add-On Execute code on first run

查看:109
本文介绍了首次运行Mozilla附加组件时执行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在构建一个附加组件,我想在第一次运行时执行特定的代码.更具体地说,我想单击添加按钮,浏览我的文件并选择一个可执行文件.此浏览过程仅应在首次运行时执行,因为我希望我的按钮记住"在第一次运行后打开此特定文件.

I am currently building an add on and I wanted to execute specific code on the first run. More specifically I want to click on my add on button, browse through my files and select an executable file. This browsing process should only be done on the first run as I want my button to "remember" to open this specific file after the first run.

jetpack.future.import("me");

var buttons = require('sdk/ui/button/action');

var button = buttons.ActionButton({
  id: "execute-jar",
  label: "Download Report",
  icon: {
    "16": "./icon-16.png",
    "32": "./icon-32.png",
    "64": "./icon-64.png"
  },
  onClick: handleClick
});

jetpack.me.onFirstRun(function(){    jetpack.notifications.show("Oh boy, I'm installed!");});

function handleClick(state) {   

    let {Cc, Ci} = require('chrome');
    var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
    file.initWithPath("C:\\Users\\QaziWa\\DownloadReportPPE.jar");

    if(file.exists()){
        file.reveal();
        file.launch();
    }
    else {
        console.log('Failed.');
    }
}

我在MDN上发现了此问题: https://developer.mozilla.org/zh-CN/docs/Archive/Mozilla/Jetpack/Meta/Me

I found this on MDN: https://developer.mozilla.org/en-US/docs/Archive/Mozilla/Jetpack/Meta/Me

但是,这是已存档的文件,当我尝试执行此操作时,我的代码未成功获得:消息:ReferenceError:未定义jetpack".

However, this is something that was archived and when I tried this my code didn't succeed and got: "Message: ReferenceError: jetpack is not defined".

在这一点上,我很困惑,因为我查看了一些我可以找到的与我想要的东西相关的问题,但是我无法弄清楚如何在我的附加组件中实现这一急需的功能.有人可以向我指出正确的方向,还是提供一些有效的代码示例?

I'm pretty stumped at this point as I have looked at the few questions I could find that were relevant to what I want but I cannot figure out how to implement this much needed feature in my add-on. Could someone point me in the right direction or provide an example of some code that works?

为清楚起见:我不想对路径进行硬编码.我打算添加一些功能,使用户可以选择他们希望执行的文件.我的主要问题是如何只在第一次运行时执行某些代码块?

For clarity: I do not want to hardcode a path. I intend to add functionality that will allow users to select the file they wish to execute. My main question is how do I execute a certain block of code only on the first run?

推荐答案

具有首次运行代码的一般情况是,您使用存储在附录B:安装和卸载脚本,但是该页面不是特定于SDK的.当您的附加代码开始运行时,然后进行测试以查看该标志是否表明您已经在运行首次运行的代码.如果该标志指示您尚未运行首次运行的代码,请运行该代码并设置标志以指示其已运行.您可以对此进行一些修改,以使代码仅在升级到新版本(只是另一个标志)等后才能运行.

The general case of having first-run code is that you use some sort of flag that is stored in a preference to indicate if you have run the code, or not. Preferences are what are used within Firefox to store simple data across application restarts. The flag can be any of multiple types (string, number, boolean, etc.). You just have to have a value which you can test to see if you have run your first-run code. The general case of this is discussed on MDN in Appendix B: Install and Uninstall Scripts, but that page is not SDK specific. When your add-on code starts running you then test to see if the flag indicates that you have already run your first-run code. If the flag indicates that you have not run the first-run code, you run that code and set the flag to indicate it has been run. You can modify this a bit to have code that is only run upon upgrading to a new version (just another flag), etc.

在这种情况下,我们还需要在Firefox重新启动时存储用户选择的文件名/路径.鉴于我们已经需要存储该信息,并且这是我们需要获得首次运行代码的条件,因此chosenFilename的有效性也可以用作运行首次运行代码的标志.因此,您存储选择为首选项.如果该首选项不包含文件名,则可以运行文件的选择逻辑/浏览(您的首次运行代码).

In this case, we also need to store a user chosen filename/path across Firefox restarts. Given that we already need to store that information, and it is what we need the first-run code to get, the validity of the chosenFilename can also be used as the flag to run the first-run code. So, you store the full filename/path of the file chosen as a preference. If that preference does not contain a filename, then you run the selection logic/browse for the file (your first-run code).

在这种情况下,如果在尝试打开文件时发现存储的文件不存在(例如,用户删除/移动了文件),我们还将运行代码以浏览文件.显然,您还应该为用户提供一种手动启动文件选择器的方法.幸运的是,简单偏好系统当我们声明 type作为file .然后,一个"浏览"按钮将将显示在选项对话框中,用户可以使用该对话框手动选择其他文件.

In this instance, we also run the code to browse for a file if it is found that the file stored does not exist when we attempt to open it (e.g. the user deleted/moved the file). Obviously, you should also have a way for the user to manually start the file picker. Fortunately, the simple-prefs system takes care of that for us when we declare the type as file. A "Browse" button will then be displayed in the options dialog which the user can use to manually select a different file.

类似的东西:

let {Cc, Ci} = require('chrome');
let preferences = require("sdk/simple-prefs").prefs;
let winUtils = require("sdk/window/utils");
const nsIFilePicker = Ci.nsIFilePicker;

function chooseFilename(){
    //If you only want .jar files then you would specify that in the filter(s):
    preferences.chosenFilename = browseForFilePath(nsIFilePicker.filterAll);
    //Just assume that it is valid. Should verify here that filename is valid.
}

function openChosenFilename() {   
    let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
    file.initWithPath(preferences.chosenFilename);
    while(!file.exists()){
        //The file we had is no longer valid. We need a valid filename so try choosing it
        // again. Keep doing so until it actually exists.
        chooseFilename();
        file.initWithPath(preferences.chosenFilename);
    }
    return file;
}

//The following function was copied from promptForFile(), then modified: 
//https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Creating_reusable_modules
function browseForFilePath(fileFilters) {
    let filePicker = Cc["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
    let recentWindow = winUtils.getMostRecentBrowserWindow();
    filePicker.init(recentWindow, "Select a file", nsIFilePicker.modeOpen);
    filePicker.appendFilters(fileFilters);
    let path = "";
    let status = filePicker.show();
    if (status === nsIFilePicker.returnOK || status === nsIFilePicker.returnReplace) {
        // Get the path as string.
        path = filePicker.file.path;
    }
    return path; //"" if invalid
}

if(preferences.chosenFilename.length<5) {
    //This is our first run code. We only run it if the length of the chosenFilename 
    //  is less than 5 (which is assumed to be invalid).
    //  You can have any first run code here. You just need to have the 
    //  preference you test for (which could be a string, boolean value, number, whatever) 
    //  be set prior to exiting this if statement. In this specific case,
    //  it is a filename. Given that we also want to store the filename persistent
    //  across Firefox restarts we are checking for filename being stored as a preference
    //  instead of an additional flag that says we have already run this code.
    chooseFilename();
}

let file = openChosenFilename();
file.reveal();
file.launch();

您的 package.json 还需要:

"preferences": [{
    "name": "chosenFilename",
    "title": "Filename that has been chosen",
    "description": "A file the user has chosen",
    "type": "file",
    "value": ""
},

这篇关于首次运行Mozilla附加组件时执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆