Guest User

JS AJAX part

a guest
Apr 26th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ECMAScript 5 strict mode
  2. "use strict";
  3.  
  4. assert2(cr, "cr namespace not created");
  5. assert2(cr.plugins_, "cr.plugins_ not created");
  6.  
  7. /////////////////////////////////////
  8. // Plugin class
  9. cr.plugins_.PrxFW = function(runtime)
  10. {
  11.     this.runtime = runtime;
  12. };
  13.  
  14. (function ()
  15. {
  16.     var isNWjs = false;
  17.     var path = null;
  18.     var fs = null;
  19.     var nw_appfolder = "";
  20.    
  21.     var pluginProto = cr.plugins_.PrxFW.prototype;
  22.        
  23.     /////////////////////////////////////
  24.     // Object type class
  25.     pluginProto.Type = function(plugin)
  26.     {
  27.         this.plugin = plugin;
  28.         this.runtime = plugin.runtime;
  29.     };
  30.  
  31.     var typeProto = pluginProto.Type.prototype;
  32.    
  33.     var backendURL = "http://api.proxy.wtf/debug.php";
  34.    
  35.     var credUsername = "";
  36.     var credPassword = "";
  37.    
  38.     typeProto.onCreate = function()
  39.     {
  40.     };
  41.  
  42.     /////////////////////////////////////
  43.     // Instance class
  44.     pluginProto.Instance = function(type)
  45.     {
  46.         this.type = type;
  47.         this.runtime = type.runtime;
  48.        
  49.         this.lastData = "";
  50.         this.curTag = "";
  51.         this.progress = 0;
  52.         this.timeout = -1;
  53.        
  54.         isNWjs = this.runtime.isNWjs;
  55.        
  56.         if (isNWjs)
  57.         {
  58.             path = require("path");
  59.             fs = require("fs");
  60.             var process = window["process"] || nw["process"];
  61.             nw_appfolder = path["dirname"](process["execPath"]) + "\\";
  62.         }
  63.     };
  64.    
  65.     var instanceProto = pluginProto.Instance.prototype;
  66.    
  67.     var theInstance = null;
  68.    
  69.     // For handling PrxFW events in DC
  70.     window["PrxFW_PrxFW_DCSide"] = function (event_, tag_, param_)
  71.     {
  72.         if (!theInstance)
  73.             return;
  74.        
  75.         if (event_ === "success")
  76.         {
  77.             theInstance.curTag = tag_;
  78.             theInstance.lastData = param_;
  79.             theInstance.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnComplete, theInstance);
  80.         }
  81.         else if (event_ === "error")
  82.         {
  83.             theInstance.curTag = tag_;
  84.             theInstance.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnError, theInstance);
  85.         }
  86.         else if (event_ === "progress")
  87.         {
  88.             theInstance.progress = param_;
  89.             theInstance.curTag = tag_;
  90.             theInstance.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnProgress, theInstance);
  91.         }
  92.     };
  93.  
  94.     instanceProto.onCreate = function()
  95.     {
  96.         theInstance = this;
  97.     };
  98.    
  99.     instanceProto.saveToJSON = function ()
  100.     {
  101.         return { "lastData": this.lastData };
  102.     };
  103.    
  104.     instanceProto.loadFromJSON = function (o)
  105.     {
  106.         this.lastData = o["lastData"];
  107.         this.curTag = "";
  108.         this.progress = 0;
  109.     };
  110.    
  111.     var next_request_headers = {};
  112.     var next_override_mime = "";
  113.    
  114.     instanceProto.doRequest = function (tag_, url_, method_, data_)
  115.     {
  116.         // In directCanvas: forward request to webview layer
  117.         if (this.runtime.isDirectCanvas)
  118.         {
  119.             AppMobi["webview"]["execute"]('PrxFW_PrxFW_WebSide("' + tag_ + '", "' + url_ + '", "' + method_ + '", ' + (data_ ? '"' + data_ + '"' : "null") + ');');
  120.             return;
  121.         }
  122.        
  123.         // Create a context object with the tag name and a reference back to this
  124.         var self = this;
  125.         var request = null;
  126.        
  127.         var doErrorFunc = function ()
  128.         {
  129.             self.curTag = tag_;
  130.             self.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnError, self);
  131.         };
  132.        
  133.         var errorFunc = function ()
  134.         {
  135.         };
  136.            
  137.         var progressFunc = function (e)
  138.         {
  139.             if (!e["lengthComputable"])
  140.                 return;
  141.                
  142.             self.progress = e.loaded / e.total;
  143.             self.curTag = tag_;
  144.             self.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnProgress, self);
  145.         };
  146.            
  147.         try
  148.         {
  149.             // Windows Phone 8 can't PrxFW local files using the standards-based API, but
  150.             // can if we use the old-school ActiveXObject. So use ActiveX on WP8 only.
  151.             if (this.runtime.isWindowsPhone8)
  152.                 request = new ActiveXObject("Microsoft.XMLHTTP");
  153.             else
  154.                 request = new XMLHttpRequest();
  155.            
  156.             request.onreadystatechange = function()
  157.             {
  158.                 if (request.readyState === 4)
  159.                 {
  160.                     self.curTag = tag_;
  161.                    
  162.                     if (request.responseText)
  163.                         self.lastData = request.responseText.replace(/\r\n/g, "\n");        // fix windows style line endings
  164.                     else
  165.                         self.lastData = "";
  166.                    
  167.                     if (request.status >= 400)
  168.                         self.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnError, self);
  169.                     else
  170.                     {
  171.                         // In node-webkit, don't trigger 'on success' with empty string if file not found.
  172.                         // In a browser also don't trigger 'on success' if the returned string is empty and the status is 0,
  173.                         // which is what happens when onerror is about to fire.
  174.                         if ((!isNWjs || self.lastData.length) && !(!isNWjs && request.status === 0 && !self.lastData.length))
  175.                             // self.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnComplete, self);
  176.                         {
  177.                             if (self.lastData == "-200")
  178.                             {
  179.                                 theInstance.SentData = String("Success -200")
  180.                                 self.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnRegistered, self);
  181.                             }
  182.                             if (self.lastData == "-250")
  183.                             {
  184.                                 theInstance.SentData = String("Username OK")
  185.                                 self.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnRegistered, self);
  186.                             }
  187.                             if (self.lastData == "-300")
  188.                             {
  189.                                 theInstance.SentData = String("Error -300")
  190.                                 self.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnRegistered, self);
  191.                             }
  192.                             if (self.lastData == "-301")
  193.                             {
  194.                                 theInstance.SentData = String("Error -301")
  195.                                 self.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnRegistered, self);
  196.                             }
  197.                             if (self.lastData == "-302")
  198.                             {
  199.                                 theInstance.SentData = String("Error -302")
  200.                                 self.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnRegistered, self);
  201.                             }
  202.                             if (self.lastData == "-303")
  203.                             {
  204.                                 theInstance.SentData = String("Error -303")
  205.                                 self.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnRegistered, self);
  206.                             }
  207.                             if (self.lastData == "-304")
  208.                             {
  209.                                 theInstance.SentData = String("Error -304")
  210.                                 self.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnRegistered, self);
  211.                             }
  212.                             if (self.lastData == "-305")
  213.                             {
  214.                                 theInstance.SentData = String("Error -305")
  215.                                 self.runtime.trigger(cr.plugins_.PrxFW.prototype.cnds.OnRegistered, self);
  216.                             }
  217.                         }
  218.                     }
  219.                 }
  220.             };
  221.            
  222.             if (!this.runtime.isWindowsPhone8)
  223.             {
  224.                 request.onerror = errorFunc;
  225.                 request.ontimeout = errorFunc;
  226.                 request.onabort = errorFunc;
  227.                 request["onprogress"] = progressFunc;
  228.             }
  229.            
  230.             request.open(method_, url_);
  231.            
  232.             if (!this.runtime.isWindowsPhone8)
  233.             {
  234.                 // IE requires timeout be set after open()
  235.                 if (this.timeout >= 0 && typeof request["timeout"] !== "undefined")
  236.                     request["timeout"] = this.timeout;
  237.             }
  238.            
  239.             // Workaround for CocoonJS bug: property exists but is not settable
  240.             try {
  241.                 request.responseType = "text";
  242.             } catch (e) {}
  243.            
  244.             if (data_)
  245.             {
  246.                 if (request["setRequestHeader"] && !next_request_headers.hasOwnProperty("Content-Type"))
  247.                 {
  248.                     request["setRequestHeader"]("Content-Type", "application/x-www-form-urlencoded");
  249.                 }
  250.             }
  251.            
  252.             // Apply custom headers
  253.             if (request["setRequestHeader"])
  254.             {
  255.                 var p;
  256.                 for (p in next_request_headers)
  257.                 {
  258.                     if (next_request_headers.hasOwnProperty(p))
  259.                     {
  260.                         try {
  261.                             request["setRequestHeader"](p, next_request_headers[p]);
  262.                         }
  263.                         catch (e) {}
  264.                     }
  265.                 }
  266.                
  267.                 // Reset for next request
  268.                 next_request_headers = {};
  269.             }
  270.            
  271.             // Apply MIME type override if one set
  272.             if (next_override_mime && request["overrideMimeType"])
  273.             {
  274.                 try {
  275.                     request["overrideMimeType"](next_override_mime);
  276.                 }
  277.                 catch (e) {}
  278.                
  279.                 // Reset for next request
  280.                 next_override_mime = "";
  281.             }
  282.  
  283.             if (data_)
  284.                 request.send(data_);
  285.             else
  286.                 request.send();
  287.            
  288.         }
  289.         catch (e)
  290.         {
  291.             errorFunc();
  292.         }
  293.     };
  294.    
  295.     /**BEGIN-PREVIEWONLY**/
  296.     instanceProto.getDebuggerValues = function (propsections)
  297.     {
  298.         propsections.push({
  299.             "title": "PrxFW",
  300.             "properties": [
  301.                 {"name": "Last data", "value": this.lastData, "readonly": true}
  302.             ]
  303.         });
  304.     };
  305.     /**END-PREVIEWONLY**/
  306.  
  307.     //////////////////////////////////////
  308.     // Conditions
  309.     function Cnds() {};
  310.  
  311.     Cnds.prototype.OnRegistered = function ()
  312.     {
  313.         return true;
  314.     };
  315.    
  316.     Cnds.prototype.OnError = function (tag)
  317.     {
  318.         return cr.equals_nocase(tag, this.curTag);
  319.     };
  320.    
  321.     Cnds.prototype.OnProgress = function (tag)
  322.     {
  323.         return cr.equals_nocase(tag, this.curTag);
  324.     };
  325.    
  326.     pluginProto.cnds = new Cnds();
  327.  
  328.     //////////////////////////////////////
  329.     // Actions
  330.     function Acts() {};
  331.    
  332.     Acts.prototype.DoRegister = function (username_, email_, password_)
  333.     {
  334.         this.doRequest("DoRegister", backendURL, "POST", String("Action=Register&Username=" + username_ + "&Mailaddress=" + email_ + "&Password=" + password_));
  335.         theInstance.SentData = String("DoRegister" + backendURL + "POST" + String("Action=Register&Username=" + username_ + "&Mailaddress=" + email_ + "&Password=" + password_));
  336.     };
  337.    
  338.    
  339.    
  340.    
  341.    
  342.    
  343.    
  344.     Acts.prototype.Request = function (tag_, url_)
  345.     {
  346.         this.doRequest(tag_, url_, "GET");
  347.     };
  348.    
  349.     Acts.prototype.RequestFile = function (tag_, file_)
  350.     {
  351.         this.doRequest(tag_, file_, "GET");
  352.     };
  353.    
  354.     Acts.prototype.Post = function (tag_, url_, data_, method_)
  355.     {
  356.         this.doRequest(tag_, url_, method_, data_);
  357.     };
  358.    
  359.     Acts.prototype.SetTimeout = function (t)
  360.     {
  361.         this.timeout = t * 1000;
  362.     };
  363.    
  364.     Acts.prototype.SetHeader = function (n, v)
  365.     {
  366.         next_request_headers[n] = v;
  367.     };
  368.    
  369.     Acts.prototype.OverrideMIMEType = function (m)
  370.     {
  371.         next_override_mime = m;
  372.     };
  373.    
  374.     pluginProto.acts = new Acts();
  375.  
  376.     //////////////////////////////////////
  377.     // Expressions
  378.     function Exps() {};
  379.  
  380.     Exps.prototype.LastData = function (ret)
  381.     {
  382.         ret.set_string(this.lastData);
  383.     };
  384.    
  385.     Exps.prototype.Progress = function (ret)
  386.     {
  387.         ret.set_float(this.progress);
  388.     };
  389.    
  390.     Exps.prototype.SentData = function (ret)
  391.     {
  392.         ret.set_string(String(this.SentData));
  393.     };
  394.    
  395.     pluginProto.exps = new Exps();
  396.  
  397. }());
Add Comment
Please, Sign In to add comment