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
|
Components.utils.import('resource://greasemonkey/util.js');
const EXPORTED_SYMBOLS = ['getContents'];
const ioService=Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
const scriptableStream=Components
.classes["@mozilla.org/scriptableinputstream;1"]
.getService(Components.interfaces.nsIScriptableInputStream);
const unicodeConverter = Components
.classes["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
function getContents(aFile, aCharset, aFatal) {
if (!aFile.isFile()) {
throw new Error(
'Greasemonkey tried to get contents of non-file:\n' + aFile.path);
}
unicodeConverter.charset = aCharset || 'UTF-8';
var channel = ioService.newChannelFromURI(GM_util.getUriFromFile(aFile));
try {
var input = channel.open();
} catch (e) {
GM_util.logError(new Error("Could not open file: " + aFile.path));
return "";
}
scriptableStream.init(input);
var str = scriptableStream.read(input.available());
scriptableStream.close();
input.close();
try {
return unicodeConverter.ConvertToUnicode(str);
} catch (e) {
if (aFatal) throw e;
return str;
}
}
|