software:firefox-xpi-programming:0ther
Это старая версия документа!
Содержание
Разные куски кода
Чтение из файла
function getContents(aURL){ var ioService=Components.classes["@mozilla.org/network/io-service;1"] .getService(Components.interfaces.nsIIOService); var scriptableStream=Components .classes["@mozilla.org/scriptableinputstream;1"] .getService(Components.interfaces.nsIScriptableInputStream); var channel=ioService.newChannel(aURL,null,null); var input=channel.open(); scriptableStream.init(input); var str=scriptableStream.read(input.available()); scriptableStream.close(); input.close(); return str; } try{ alert(getContents("chrome://browser/content/browser.css")); }catch(e){alert(e)}
Запись в файл
function doXmlStuff () { var somefile = chromeToPath("chrome://path/to/file"); var xmlDoc; var file = Components.classes["@mozilla.org/file/local;1"] .createInstance(Components.interfaces.nsILocalFile); file.initWithPath(somefile); if (!(file.exists())) { var fcStream = Components.classes["@mozilla.org/network/file-output-stream;1"] .createInstance(Components.interfaces.nsIFileOutputStream); fcStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); // write, create, truncate var data = '<xml><rootnode></rootnode>'; fcStream.write(data,data.length); fcStream.close } xmlDoc = document.implementation.createDocument("","",null); xmlDoc.load("chrome://piratequesting/content/trainingLog.xml"); xmlDoc.onload = function () { xmlDoc.documentElement.appendChild(....whatever you want to append....); var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"] .createInstance(Components.interfaces.nsIFileOutputStream); //clear the file for writing the new doc foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); // write, create, truncate var ser = new XMLSerializer(); //write the serialized XML to file ser.serializeToStream(xmlDoc, foStream, ""); foStream.close(); } }
software/firefox-xpi-programming/0ther.1264754225.txt.gz · Последнее изменение: 2017/03/23 21:57 (внешнее изменение)