JS写入文件

时间:2014-09-20 20:04:43

标签: javascript

我想自动保存文本,我该怎么做

代码;

notifications.notify(
            {
                title: "Click to copy command to clipboard",
                text: trimmedCmd,
                iconURL: hdsEnabled,
                data: command,
                onClick: function (data)
                {
                    clipboard.set(data);
                }
            });

2 个答案:

答案 0 :(得分:1)

Javascript不允许文件系统访问。既然你提到

notifications.notify

这似乎是mozilla的附加sdk的一部分 - 你可能会参考这个api吗?

https://developer.mozilla.org/en-US/Add-ons/SDK/Low-Level_APIs/io_file

答案 1 :(得分:0)

notifications.js

/ * - - 模式:Java; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 - -      * vim:设置ts = 2 sw = 2 sts = 2 et filetype = javascript      *此源代码表格受Mozilla Public的条款约束      *许可证,版本2.0。如果没有分发MPL的副本      *文件,您可以在http://mozilla.org/MPL/2.0/获得一个。 * /

"use strict";

module.metadata = {
  "stability": "stable"
};

const { Cc, Ci, Cr } = require("chrome");
const apiUtils = require("./deprecated/api-utils");
const errors = require("./deprecated/errors");

try {
  let alertServ = Cc["@mozilla.org/alerts-service;1"].
                  getService(Ci.nsIAlertsService);

  // The unit test sets this to a mock notification function.
  var notify = alertServ.showAlertNotification.bind(alertServ);
}
catch (err) {
  // An exception will be thrown if the platform doesn't provide an alert
  // service, e.g., if Growl is not installed on OS X.  In that case, use a
  // mock notification function that just logs to the console.
  notify = notifyUsingConsole;
}

exports.notify = function notifications_notify(options) {
  let valOpts = validateOptions(options);
  let clickObserver = !valOpts.onClick ? null : {
    observe: function notificationClickObserved(subject, topic, data) {
      if (topic === "alertclickcallback")
        errors.catchAndLog(valOpts.onClick).call(exports, valOpts.data);
    }
  };
  function notifyWithOpts(notifyFn) {
    notifyFn(valOpts.iconURL, valOpts.title, valOpts.text, !!clickObserver,
             valOpts.data, clickObserver);
  }
  try {
    notifyWithOpts(notify);
  }
  catch (err if err instanceof Ci.nsIException &&
                err.result == Cr.NS_ERROR_FILE_NOT_FOUND) {
    console.warn("The notification icon named by " + valOpts.iconURL +
                 " does not exist.  A default icon will be used instead.");
    delete valOpts.iconURL;
    notifyWithOpts(notify);
  }
  catch (err) {
    notifyWithOpts(notifyUsingConsole);
  }
};

function notifyUsingConsole(iconURL, title, text) {
  title = title ? "[" + title + "]" : "";
  text = text || "";
  let str = [title, text].filter(function (s) s).join(" ");
  console.log(str);
}

function validateOptions(options) {
  return apiUtils.validateOptions(options, {
    data: {
      is: ["string", "undefined"]
    },
    iconURL: {
      is: ["string", "undefined"]
    },
    onClick: {
      is: ["function", "undefined"]
    },
    text: {
      is: ["string", "undefined"]
    },
    title: {
      is: ["string", "undefined"]
    }
  });
}