javascript - 无法使用xul写入文件

标签 javascript firefox-addon xul xulrunner

尝试使用 XUL 代码在 Windows 临时目录中写入文件:

function writeFile_launch_application(utility_name,utility_path)
{
var data ='tasklist  /nh /fi "imagename eq '+utility_name+'" | find /i "'+utility_name+'">nul && (echo alerady running) || ("'+utility_path+'")';
//alert(data);
var file = Cc["@mozilla.org/file/directory_service;1"].
       getService(Ci.nsIProperties).
       get("TmpD", Ci.nsIFile);
        file.append("launch_application.bat");
        file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);

        // Then, we need an output stream to our output file.
        var ostream = Cc["@mozilla.org/network/file-output-stream;1"].
                      createInstance(Ci.nsIFileOutputStream);
        ostream.init(file, -1, -1, 0);

        // Finally, we need an input stream to take data from.
        const TEST_DATA = data;
        let istream = Cc["@mozilla.org/io/string-input-stream;1"].
                      createInstance(Ci.nsIStringInputStream);
        istream.setData(TEST_DATA, TEST_DATA.length);

        NetUtil.asyncCopy(istream, ostream, function(aResult) {
          if (!Components.isSuccessCode(aResult)) {
            // an error occurred!
          }
        })
}

但是出现错误:

Timestamp: 11/29/2012 11:03:09 PM
Error: ReferenceError: Cc is not defined
Source File: chrome://myaddon/content/overlay.js
Line: 199

我还尝试在我的代码顶部添加以下行,但它没有解决上述错误:

Components.utils.import("resource://gre/modules/NetUtil.jsm");

Components.utils.import("resource://gre/modules/FileUtils.jsm");

最佳答案

Cc 和 Ci 分别是 Components.classes 和 Components.interfaces 的别名。

根据上下文,它们可能(或可能没有)已经定义。

无论如何

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cr = Components.results;

(你不应该将你的问题标记为 firefox-addon-sdk)

关于javascript - 无法使用xul写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13640539/

相关文章:

javascript - 从 DIV 获取 GoogleDocs 选择并替换它(JAVASCRIPT、XUL)

javascript - 如何在dynamodb中更新 map 属性

javascript - JQuery 自动完成下拉选择功能

javascript - iPhone/iOS 6/Mobile Safari : Is there a way to detect "in browser fullscreen" mode? Android 兼容性?

javascript - Firefox 插件在指定页面上执行 Javascript?

javascript - 如何在 Firefox 扩展中跨选项卡平滑打开 Pane ?

android - 移动 Firefox 插件开发 : window. BrowserApp.deck 为空

javascript - 无法通过 <editor> 元素启用 XUL 中的 Midas(Gecko 富文本编辑器)

javascript - 如何在 Firefox 扩展中更改状态栏面板的背景颜色

javascript - 如何在 React 中将参数传递给事件监听器?