mod_muc_webchat_url: Fix default url
[prosody-modules.git] / mod_json_streams / strophe.jsonstreams.js
blobfd355c5dc5ce185c9020ad1123a92c917c344187
2 /* jsonstreams plugin
3 **
4 ** This plugin upgrades Strophe to support XEP-0295: JSON Encodings for XMPP
5 **
6 */
8 Strophe.addConnectionPlugin('jsonstreams', {
9 init: function (conn) {
11 var parseXMLString = function(xmlStr) {
12 var xmlDoc = null;
13 if (window.ActiveXObject) {
14 xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
15 xmlDoc.async=false;
16 xmlDoc.loadXML(xmlStr);
17 } else {
18 var parser = new DOMParser();
19 xmlDoc = parser.parseFromString(xmlStr, "text/xml");
21 return xmlDoc;
24 // replace Strophe.Request._newXHR with new jsonstreams version
25 // if JSON is detected
26 if (window.JSON) {
27 var _newXHR = Strophe.Request.prototype._newXHR;
28 Strophe.Request.prototype._newXHR = function () {
29 var _xhr = _newXHR.apply(this, arguments);
30 var xhr = {
31 readyState: 0,
32 responseText: null,
33 responseXML: null,
34 status: null,
35 open: function(a, b, c) { return _xhr.open(a, b, c) },
36 abort: function() { _xhr.abort(); },
37 send: function(data) {
38 data = JSON.stringify({"s":data});
39 return _xhr.send(data);
42 var req = this;
43 xhr.onreadystatechange = this.func.bind(null, this);
44 _xhr.onreadystatechange = function() {
45 xhr.readyState = _xhr.readyState;
46 if (xhr.readyState != 4) {
47 xhr.status = 0;
48 xhr.responseText = "";
49 xhr.responseXML = null;
50 } else {
51 xhr.status = _xhr.status;
52 xhr.responseText = _xhr.responseText;
53 xhr.responseXML = _xhr.responseXML;
54 if (_xhr.responseText && !(_xhr.responseXML
55 && _xhr.responseXML.documentElement
56 && _xhr.responseXML.documentElement.tagName != "parsererror")) {
57 var data = JSON.parse(_xhr.responseText);
58 if (data && data.s) {
59 xhr.responseText = data.s;
60 xhr.responseXML = parseXMLString(data.s);
64 if ("function" == typeof xhr.onreadystatechange) { xhr.onreadystatechange(req); }
66 return xhr;
68 } else {
69 Strophe.error("jsonstreams plugin loaded, but JSON not found." +
70 " Falling back to native XHR implementation.");
73 });