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