mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2026-03-27 02:33:31 +01:00
Add params and data parameters to util.httpRequest params object. Add docs.
This commit is contained in:
@@ -85,6 +85,7 @@ var Services = Module("Services", {
|
|||||||
this.addClass("FileInStream", "@mozilla.org/network/file-input-stream;1", "nsIFileInputStream", "init", false);
|
this.addClass("FileInStream", "@mozilla.org/network/file-input-stream;1", "nsIFileInputStream", "init", false);
|
||||||
this.addClass("FileOutStream","@mozilla.org/network/file-output-stream;1", "nsIFileOutputStream", "init", false);
|
this.addClass("FileOutStream","@mozilla.org/network/file-output-stream;1", "nsIFileOutputStream", "init", false);
|
||||||
this.addClass("Find", "@mozilla.org/embedcomp/rangefind;1", "nsIFind");
|
this.addClass("Find", "@mozilla.org/embedcomp/rangefind;1", "nsIFind");
|
||||||
|
this.addClass("FormData", "@mozilla.org/files/formdata;1", "nsIDOMFormData");
|
||||||
this.addClass("HtmlConverter","@mozilla.org/widget/htmlformatconverter;1", "nsIFormatConverter");
|
this.addClass("HtmlConverter","@mozilla.org/widget/htmlformatconverter;1", "nsIFormatConverter");
|
||||||
this.addClass("HtmlEncoder", "@mozilla.org/layout/htmlCopyEncoder;1", "nsIDocumentEncoder");
|
this.addClass("HtmlEncoder", "@mozilla.org/layout/htmlCopyEncoder;1", "nsIDocumentEncoder");
|
||||||
this.addClass("InterfacePointer", "@mozilla.org/supports-interface-pointer;1", "nsISupportsInterfacePointer", "data");
|
this.addClass("InterfacePointer", "@mozilla.org/supports-interface-pointer;1", "nsISupportsInterfacePointer", "data");
|
||||||
@@ -116,7 +117,7 @@ var Services = Module("Services", {
|
|||||||
|
|
||||||
let res = Cc[service.class][service.method || "getService"]();
|
let res = Cc[service.class][service.method || "getService"]();
|
||||||
if (!service.interfaces.length)
|
if (!service.interfaces.length)
|
||||||
return res.wrappedJSObject;
|
return res.wrappedJSObject || res;
|
||||||
|
|
||||||
service.interfaces.forEach(function (iface) res.QueryInterface(Ci[iface]));
|
service.interfaces.forEach(function (iface) res.QueryInterface(Ci[iface]));
|
||||||
if (service.init && args.length) {
|
if (service.init && args.length) {
|
||||||
|
|||||||
@@ -665,7 +665,30 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
|
|||||||
* argument.
|
* argument.
|
||||||
*
|
*
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {function(XMLHttpRequest)} callback
|
* @param {object} params Optional parameters for this request:
|
||||||
|
* method: {string} The request method. @default "GET"
|
||||||
|
*
|
||||||
|
* params: {object} Parameters to append to *url*'s query string.
|
||||||
|
* data: {*} POST data to send to the server. Ordinary objects
|
||||||
|
* are converted to FormData objects, with one datum
|
||||||
|
* for each property/value pair.
|
||||||
|
*
|
||||||
|
* onload: {function(XMLHttpRequest, Event)} The request's load event handler.
|
||||||
|
* onerror: {function(XMLHttpRequest, Event)} The request's error event handler.
|
||||||
|
* callback: {function(XMLHttpRequest, Event)} An event handler
|
||||||
|
* called for either error or load events.
|
||||||
|
*
|
||||||
|
* background: {boolean} Whether to perform the request in the
|
||||||
|
* background. @default true
|
||||||
|
*
|
||||||
|
* mimeType: {string} Override the response mime type with the
|
||||||
|
* given value.
|
||||||
|
* responseType: {string} Override the type of the "response"
|
||||||
|
* property.
|
||||||
|
*
|
||||||
|
* user: {string} The user name to send via HTTP Authentication.
|
||||||
|
* pass: {string} The password to send via HTTP Authentication.
|
||||||
|
*
|
||||||
* @returns {XMLHttpRequest}
|
* @returns {XMLHttpRequest}
|
||||||
*/
|
*/
|
||||||
httpGet: function httpGet(url, callback, self) {
|
httpGet: function httpGet(url, callback, self) {
|
||||||
@@ -675,13 +698,32 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
let xmlhttp = services.Xmlhttp();
|
let xmlhttp = services.Xmlhttp();
|
||||||
xmlhttp.mozBackgroundRequest = true;
|
xmlhttp.mozBackgroundRequest = Set.has(params, "background") ? params.background : true;
|
||||||
|
|
||||||
let async = params.callback || params.onload || params.onerror;
|
let async = params.callback || params.onload || params.onerror;
|
||||||
if (async) {
|
if (async) {
|
||||||
xmlhttp.onload = function handler(event) { util.trapErrors(params.onload || params.callback, params, xmlhttp, event) };
|
xmlhttp.onload = function handler(event) { util.trapErrors(params.onload || params.callback, params, xmlhttp, event) };
|
||||||
xmlhttp.onerror = function handler(event) { util.trapErrors(params.onerror || params.callback, params, xmlhttp, event) };
|
xmlhttp.onerror = function handler(event) { util.trapErrors(params.onerror || params.callback, params, xmlhttp, event) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (isObject(params.params)) {
|
||||||
|
let data = [encodeURIComponent(k) + "=" + encodeURIComponent(v)
|
||||||
|
for ([k, v] in iter(params.params))];
|
||||||
|
let uri = util.newURI(url);
|
||||||
|
uri.query += (uri.query ? "&" : "") + data.join("&");
|
||||||
|
|
||||||
|
url = uri.spec;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isObject(params.data) && !(params.data instanceof Ci.nsISupports)) {
|
||||||
|
let data = services.FormData();
|
||||||
|
for (let [k, v] in iter(params.data))
|
||||||
|
data.append(k, v);
|
||||||
|
params.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (params.mimeType)
|
if (params.mimeType)
|
||||||
xmlhttp.overrideMimeType(params.mimeType);
|
xmlhttp.overrideMimeType(params.mimeType);
|
||||||
|
|
||||||
@@ -691,7 +733,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
|
|||||||
xmlhttp.open(params.method || "GET", url, async,
|
xmlhttp.open(params.method || "GET", url, async,
|
||||||
params.user, params.pass);
|
params.user, params.pass);
|
||||||
|
|
||||||
xmlhttp.send(null);
|
xmlhttp.send(params.data);
|
||||||
return xmlhttp;
|
return xmlhttp;
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user