PageRenderTime 35ms CodeModel.GetById 12ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/webccr/WebContent/js/chrome_plugin.js

https://bitbucket.org/sanliou/synccr
JavaScript | 402 lines | 284 code | 67 blank | 51 comment | 31 complexity | 640c1157d60ec0b5642e3b451089b63f MD5 | raw file
  1// Robert, 2015/04/24 , add for chrome deprecation for NPAPI.
  2
  3function emisUtility() {
  4
  5	console.log('you are using emisUtility for plugin functions');
  6
  7  this.getBasePath = (function(){
  8    var sFullPath = window.document.location.href;
  9    var sUriPath = window.document.location.pathname;
 10    var pos = sFullPath.indexOf(sUriPath);
 11    var prePath = sFullPath.substring(0, pos);
 12    var postPath = sUriPath.substring(0, sUriPath.substr(1).indexOf('/') + 1);
 13    var webPath = prePath + postPath + "/";
 14    return webPath;
 15  })();
 16	
 17	//this.version="3.11"; 
 18	// we use defined prototype to provide 'version' property by call emisServerX.exe to get real version of emisServerX.exe
 19	this.getVersion=function() {
 20	    var version_request = { 
 21			"type":"version"
 22		};	
 23		
 24		if( this.__sendCross(version_request,2000) ) {
 25			return this.jsonReturn.data;
 26		}
 27		return '';
 28	};
 29	
 30	
 31	this.jsonReturn;
 32	
 33	this.copyfile=function(srcFile,destFile) {
 34
 35	    var sendData = { "type":"copyfile" , "param1":srcFile, "param2":destFile };	
 36//		alert("@@chrome_plugin.js 36:sendData=" + sendData);
 37		return this.__send(sendData);
 38	};
 39	
 40	this.exists = function ( target ) {
 41	    var sendData = { "type":"exists" , "param1":target };	
 42		if(this.__send(sendData)) {
 43			if( "true"== this.jsonReturn.data ) {
 44				return true;
 45			}
 46		}
 47		return false;
 48	};
 49	
 50	this.deletefile = function ( target ) {
 51	    var sendData = { "type":"deletefile" , "param1":target };	
 52		return this.__send(sendData);		 
 53	};
 54	
 55	this.filesize = function ( target ) {
 56	    var sendData = { "type":"filesize" , "param1":target };	
 57		if( this.__send(sendData) ) {
 58			return this.jsonReturn.data;
 59		}
 60		return 0;
 61	};
 62	
 63	this.filetime = function( target ) {
 64	    var sendData = { "type":"filetime" , "param1":target };	
 65		if( this.__send(sendData)) {
 66			return this.jsonReturn.data;
 67		}
 68		return "";
 69	};
 70	
 71	this.filetimefmt = function (target,fmt) {
 72	    var sendData = { "type":"filetimefmt" , "param1":target,"param2":fmt };	
 73		if( this.__send(sendData)) {
 74			return this.jsonReturn.data;
 75		}
 76		return "";
 77	};
 78	
 79	this.selectDir=function(caption) {
 80	    var sendData = { "type":"selectDir" , "param1":caption};	
 81		if( this.__send(sendData) ) {
 82			return this.jsonReturn.data;
 83		}
 84		return "";
 85	};
 86	
 87	this.getFileList=function(select_mask) {
 88	    var sendData = { "type":"getFileList" , "param1":select_mask};	
 89		if( this.__send(sendData) ) {
 90			return this.jsonReturn.data;
 91		}
 92		return "";
 93	};
 94	
 95	this.getUser=function() {
 96	    var sendData = { "type":"getUser" };	
 97		if( this.__send(sendData) ) {
 98			return this.jsonReturn.data;
 99		}
100		return "";
101	};
102
103	this.readini = function (file, section, entry ) {
104	    var sendData = { 
105			"type":"readini",
106			"param1":file,
107			"param2":section,
108			"param3":entry
109		};	
110		if( this.__send(sendData) ) {
111			return this.jsonReturn.data;
112		}
113		return "";
114	};
115	
116	// return boolean
117	this.writeini = function (file, section, entry, value ) {
118	    var sendData = { 
119			"type":"writeini", 
120			"param1":file,
121			"param2":section,
122			"param3":entry,
123			"param4":value
124		};	
125		return this.__send(sendData);
126	};
127	
128	this.mkdirs = function ( dir ) {
129	    var sendData = { "type":"mkdirs" , "param1":dir};	
130		return this.__send(sendData);		 
131	};
132	
133	this.writeStringToFile = function(file,value) {
134	    var sendData = { 
135			"type":"writeStringToFile",
136			"param1":file,
137			"param2":value
138		};	
139		return this.__send(sendData);
140	};
141	
142	this.writeStringToUTF8File = function(file,value) {
143	    var sendData = {
144			"type":"writeStringToUTF8File",
145			"param1":file,
146			"param2":value
147		};
148		return this.__send(sendData);
149	};
150
151	this.readFileAsString = function (file) {
152	    var sendData = { 
153			"type":"readFileAsString",
154			"param1":file
155		};	
156		if( this.__send(sendData) ) {
157			return this.jsonReturn.data;
158		}
159		return "";
160	};
161	
162	// return the hander code, if <= 32, then some error happen..... 
163	this.execute = function( executor , parameter ) {
164	    var sendData = { 
165			"type":"execute",
166			"param1":executor,
167			"param2":parameter
168		};	
169		if( this.__send(sendData) ) {
170			return this.jsonReturn.data;
171		} else {
172			return -1;
173		}
174	};
175	
176	// if success ,return Empty String.
177	this.executewait = function( executor , parameter ) {
178	    var sendData = { 
179			"type":"executewait",
180			"param1":executor,
181			"param2":parameter
182		};	
183		if(this.__send(sendData)) {
184			return '';
185		} else {
186			return 'error';
187		}
188	};	
189	
190	this.download = function( url , localfilename ) {
191		return this.downloadx(url,localfilename,'');
192	};		
193	
194	
195	this.downloadx = function( url , localfilename ,sessionId ) {
196		if( sessionId == undefined ) {
197			sessionId = '';
198		}
199	    var sendData = { 
200			"type":"download",
201			"param1":url,
202			"param2":localfilename,
203			"param3":sessionId
204		};	
205		return this.__send(sendData);
206	};
207	
208	// here add two printTicket function for printing ticket 
209	// port config is something like 115200,n,8,1,P
210	// data is hex string for byte data	
211	this.printTicketByCOM = function( comPort, portConfig , data  ) {
212	 //alert(data);
213	    var sendData = { 
214			"type":"printTicketByCOM",
215			"param1":comPort,
216			"param2":portConfig,
217			"param3":data
218		};	 
219		return this.__send(sendData);
220	};
221	
222	this.printTicketBySpool = function( WindowPrinterName , data  ) {
223	    var sendData = { 
224			"type":"printTicketBySpool",
225			"param1":WindowPrinterName,
226			"param2":data
227		};	
228		return this.__send(sendData);
229	};
230	
231	////////////////////////////////////////////////////////////////////
232	// deprecated 
233	//
234	//  these are very old function that download zip from site and unzip 
235	//  as xml string , and pass it to Javascript object.
236	//  we should never need it now.
237	//  I just keep this interface , you don't need to test it.
238	/////////////////////////////////////////////////////////////////////
239	
240	this.xmlData;
241	
242	// copyURL source must be a zip format
243	// copyURL and copyURLx is merged into copyURLx
244	this.copyURL = function( url , extractName ) {
245		return this.copyURLx(url,extractName,'');
246	};		
247	
248	
249	// copyURL source must be a zip format
250	this.copyURLx = function( url , extractName ,sessionId ) {
251		if( sessionId == undefined ) {
252			sessionId = '';
253		}
254		this.xmlData='';
255		
256	    var sendData = { 
257			"type":"copyURL",
258			"param1":url,
259			"param2":extractName,
260			"param3":sessionId
261		};	
262		if( this.__send(sendData) ) {
263			this.xmlData=data;
264			return true;
265		}
266		return false
267		
268	};		
269	
270	
271    this.Get_asText = function () {
272		return this.xmlData;
273	};
274
275	// not used , just keep interface
276    this.Set_asText=function (value) {
277	};
278	
279	
280	// actually delay we don't need a real function
281	// but we do have an implement. here we send to a no response port, so it will just timeout
282	this.delay = function ( seconds ) {
283	    var sendData = { 
284			"type":"delay",
285			"param1":seconds
286		};	
287		this.__send(sendData,seconds * 1000);
288	};
289	
290	this.__send=function(sendData,timeout_v) {
291	
292	    var tokenrequest = { 
293			"type":"gettoken"
294		};	
295		if( this.__sendCross(tokenrequest,timeout_v) ) {
296			var oneTimeToken = this.jsonReturn.data;
297			
298			var encryptRequest= {
299				"token" : oneTimeToken
300			}
301			if( this.__sendLocal( this.getBasePath + 'jsp/genplugintoken.jsp',encryptRequest ) ) {
302				var returnToken = this.jsonReturn.data;
303				sendData.token = returnToken;
304				return this.__sendCross(sendData,timeout_v);
305			}
306		}
307		return false;
308	}
309	
310	this.__sendCross=function(sendData,timeout_v) {
311	
312		this.jsonReturn = null;
313 
314 
315		//Notice , you have to open it, otherwise , IE will not work.
316		jQuery.support.cors = true;
317
318    //var sFullPath = window.document.location.href;
319    var sUrl = "http://localhost:9999/";
320    //if (sFullPath.indexOf("https") > -1) {
321    //  sUrl = "https://localhost/";
322    //}
323
324		var request = $.ajax({
325		  url: sUrl,
326		  method: "POST",
327		  
328	      // Notice:
329		  // The W3C XMLHttpRequest specification dictates that the charset is always UTF-8
330		  // to work with delphi indy, we need set this, don't put charset=utf-8 here, Delphi7 Indy don't know how to handle it.
331		  // and we transfer utf8 to widestring by we self.
332		  contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
333		  
334		  data: sendData,
335		  //crossDomain:true,
336		  //xhrFields: {'withCredentials': true},
337	      //headers: { 'x-my-custom-header': 'some value' },
338
339		  // a default 10 second timeout
340		  timeout: (timeout_v==undefined) ? 10000 : timeout_v,
341		  async : false,
342		  /*
343		  success:function(data)
344          {
345			alert("Data from Server:"+data);
346             //alert("Data from Server"+JSON.stringify(data));
347          },
348		  */
349          error:function(jqXHR,textStatus,errorThrown)
350          {
351			 console.log("Cross Domain AJAX request error "+errorThrown);
352          }
353		  
354		});
355		
356		if( request.status == 200 ) {
357//			alert("@@chrome_plugin.js 329:resp=" + request.responseText);
358			this.jsonReturn = JSON.parse(request.responseText);
359			if( this.jsonReturn.result == "success") {
360				return true;
361			}
362		}
363		return false;
364		
365	};
366
367	this.__sendLocal=function( jsp, sendData,timeout_v) {
368	
369		this.jsonReturn = null;
370		
371		var request = $.ajax({
372		  url: jsp ,
373		  method: "POST",
374		  data: sendData,
375		  async : false,
376          error:function(jqXHR,textStatus,errorThrown)
377          {
378			 console.log("__sendLocal AJAX request error "+errorThrown);
379          }
380		  
381		});
382		
383		if( request.status == 200 ) {
384			this.jsonReturn = JSON.parse(request.responseText);
385			if( this.jsonReturn.result == "success") {
386				return true;
387			}
388		}
389		return false;
390		
391	};	
392
393  this.version = this.getVersion();
394};
395
396/*emisUtility.prototype = {
397	get version (){
398		return this.getVersion();
399	}
400};*/
401
402