Fragmenty kodu:Różne
z Mozilla Developer Center, polskiego centrum programistów Mozilli.
UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...
This page contains small, self-explanatory code snippets.
[edytuj] Zapisywanie strony internetowej do lokalnego pliku
Although the following code does not prompt the user for a filename, you can do so using the file picker component.
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("C:\\filename.html");
var wbp = Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1']
.createInstance(Components.interfaces.nsIWebBrowserPersist);
wbp.saveDocument(content.document, file, null, null, null, null);
[edytuj] Wykrywanie systemu operacyjnego
// Zwraca WINNT, gdy jest to Windows XP, 2000, NT Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULRuntime).OS;
You can see the list at of responses using LXR: http://lxr.mozilla.org/mozilla/source/configure.in#910.
[edytuj] Wykrywanie głównej aplikacji i jej wersji
var info = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo); // Pobiera nazwę uruchomionej aplikacji info.name; // Zwraca "Firefox" dla Firefoksa info.version; // Zwraca "2.0.0.1" dla Firefoksa w wersji 2.0.0.1
[edytuj] Odzyskiwanie wersji rozszerzenia jaka jest określona w pliku install.rdf rozszerzenia
var em = Components.classes["@mozilla.org/extensions/manager;1"]
.getService(Components.interfaces.nsIExtensionManager);
// Change extension-guid to the GUID of the extension whose version you want to retrieve;
// np. foxyproxy@eric.h.jung dla FoxyProxy
var addon = em.getItemForID("<extension-guid>");
var version = addon.version;
[edytuj] Kopiowanie ze strumienia wejścia i wyjścia
// istream jest nsIInputStream i ostream jest nsIOutputStream
// strumień wyjścia potrzebuje buforowania do swojej pracy.
var bostream = Components.classes["@mozilla.org/network/buffered-output-stream;1"]
.createInstance(Components.interfaces.nsIBufferedOutputStream);
bostream.init(ostream, 0x8000);
// make a stream pump and a stream listener to read from the input stream for us
var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"]
.createInstance(Components.interfaces.nsIInputStreamPump);
pump.init(istream, -1, -1, 0, 0, true);
/* we need our own observer to know when to close the file */
var observer = {
onStartRequest: function(aRequest, aContext) {},
onStopRequest: function(aRequest, aContext, aStatusCode) {
bostream.close();
}
};
// make a simple stream listener to do the writing to output stream for us
var listener = Components.classes["@mozilla.org/network/simple-stream-listener;1"]
.createInstance(Components.interfaces.nsISimpleStreamListener);
listener.init(bostream, observer);
// rozpoczyna kopiowanie
pump.asyncRead(listener, null);
[edytuj] Ponowne uruchamianie Firefoksa/Thunderbird
var nsIAppStartup = Components.interfaces.nsIAppStartup;
Components.classes["@mozilla.org/toolkit/app-startup;1"].getService(nsIAppStartup)
.quit(nsIAppStartup.eForceQuit | nsIAppStartup.eRestart);
[edytuj] Symulacja zdarzeń myszy i klawiatury
Interfejs nsIDOMWindowUtils stanowi metodę pomocy w symulacji zdarzeniami myszy i klawiatury.
Nowość w Firefoksie 3 / Gecko 1.9
var req = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
var utils = req.getInterface(Components.interfaces.nsIDOMWindowUtils);
utils.sendMouseEvent("mousedown", 10, 10, 0, 1, 0);
utils.sendMouseEvent("mouseup", 10, 10, 0, 1, 0);