File size: 1,795 Bytes
14a2165 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
var spawn = require('child_process').spawn;
var command;
switch(process.platform) {
case 'darwin':
command = 'open';
break;
case 'win32':
command = 'explorer.exe';
break;
case 'linux':
command = 'xdg-open';
break;
default:
throw new Error('Unsupported platform: ' + process.platform);
}
/**
* Error handling is deliberately minimal, as this function is to be easy to use for shell scripting
*
* @param url The URL to open
* @param callback A function with a single error argument. Optional.
*/
function open(url, callback) {
var child = spawn(command, [url]);
var errorText = "";
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
errorText += data;
});
child.stderr.on('end', function () {
if (errorText.length > 0) {
var error = new Error(errorText);
if (callback) {
callback(error);
} else {
throw error;
}
} else if (callback) {
callback(error);
}
});
}
/**
* @param fields Common fields are: "subject", "body".
* Some email apps let you specify arbitrary headers here.
* @param recipientsSeparator Default is ",". Use ";" for Outlook.
*/
function mailto(recipients, fields, recipientsSeparator, callback) {
recipientsSeparator = recipientsSeparator || ",";
var url = "mailto:"+recipients.join(recipientsSeparator);
Object.keys(fields).forEach(function (key, index) {
if (index === 0) {
url += "?";
} else {
url += "&";
}
url += key + "=" + encodeURIComponent(fields[key]);
});
open(url, callback);
}
exports.open = open;
exports.mailto = mailto; |