You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wpsjsrpcsdk.js 49KB

7 months ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. (function (global, factory) {
  2. "use strict";
  3. if (typeof module === "object" && typeof module.exports === "object") {
  4. module.exports = factory(global, true);
  5. } else {
  6. factory(global);
  7. }
  8. })(typeof window !== "undefined" ? window : this, function (window, noGlobal) {
  9. "use strict";
  10. var bFinished = true;
  11. /**
  12. * 兼容IE低版本的创建httpobj对象的方法
  13. * @returns httpobj,可用于进行数据传输的http的对象
  14. */
  15. function getHttpObj() {
  16. var httpobj = null;
  17. if (IEVersion() < 10) {
  18. try {
  19. httpobj = new XDomainRequest();
  20. } catch (e1) {
  21. httpobj = new createXHR();
  22. }
  23. } else {
  24. httpobj = new createXHR();
  25. }
  26. return httpobj;
  27. }
  28. //兼容IE低版本的创建xmlhttprequest对象的方法
  29. /**
  30. * 兼容IE低版本的创建xmlhttprequest对象的方法
  31. * @returns xmlhttprequest对象,兼容低版本IE
  32. */
  33. function createXHR() {
  34. if (typeof XMLHttpRequest != 'undefined') { //兼容高版本浏览器
  35. return new XMLHttpRequest();
  36. } else if (typeof ActiveXObject != 'undefined') { //IE6 采用 ActiveXObject, 兼容IE6
  37. var versions = [ //由于MSXML库有3个版本,因此都要考虑
  38. 'MSXML2.XMLHttp.6.0',
  39. 'MSXML2.XMLHttp.3.0',
  40. 'MSXML2.XMLHttp'
  41. ];
  42. for (var i = 0; i < versions.length; i++) {
  43. try {
  44. return new ActiveXObject(versions[i]);
  45. } catch (e) {
  46. //跳过
  47. }
  48. }
  49. } else {
  50. throw new Error('您的浏览器不支持XHR对象');
  51. }
  52. }
  53. /**
  54. * 通过该接口给服务器发请求
  55. * @param {*} options 参数对象,具体包含的参数如下:
  56. * @param {*} url 网页路径,传输地址
  57. * @param {*} type 传输类型,POST / GET / PUT
  58. * @param {*} sendData 传输的数据
  59. * @param {*} callback 回调函数
  60. * @param {*} tryCount 请求失败后的尝试次数
  61. * @param {*} bPop 是否弹出浏览器提示对话框
  62. * @param {*} timeout 请求等待响应的时间,超过时间请求实效
  63. * @param {*} concurrent 请求是否同步发送
  64. * @param {*} client wpsclient对象
  65. * @returns NULL
  66. */
  67. function startWps(options) {
  68. if (!bFinished && !options.concurrent) {
  69. if (options.callback)
  70. options.callback({
  71. status: 1,
  72. message: "上一次请求没有完成"
  73. });
  74. return;
  75. }
  76. bFinished = false;
  77. function startWpsInnder(tryCount) {
  78. if (tryCount <= 0) {
  79. if (bFinished)
  80. return;
  81. bFinished = true;
  82. if (options.callback)
  83. options.callback({
  84. status: 2,
  85. message: "请允许浏览器打开WPS Office"
  86. });
  87. return;
  88. }
  89. var xmlReq = getHttpObj();
  90. //WPS客户端提供的接收参数的本地服务,HTTP服务端口为58890,HTTPS服务端口为58891
  91. //这俩配置,取一即可,不可同时启用
  92. xmlReq.open('POST', options.url);
  93. xmlReq.onload = function (res) {
  94. bFinished = true;
  95. if (initCloudsvr == true) {
  96. initCloudsvr = false;
  97. }
  98. if (options.callback) {
  99. if(res.target.response == "{\"data\": \"Failed to send message to WPS.\"}"){
  100. options.callback({
  101. status: 1,
  102. message: IEVersion() < 10 ? xmlReq.responseText : res.target.response
  103. });
  104. }
  105. else {
  106. options.callback({
  107. status: 0,
  108. response: IEVersion() < 10 ? xmlReq.responseText : res.target.response
  109. });
  110. }
  111. }
  112. }
  113. xmlReq.ontimeout = xmlReq.onerror = function (res) {
  114. xmlReq.bTimeout = true;
  115. if (tryCount == options.tryCount && options.bPop && initCloudsvr == false) { //打开wps并传参
  116. InitWpsCloudSvr();
  117. }
  118. setTimeout(function () {
  119. startWpsInnder(tryCount - 1)
  120. }, 1000);
  121. }
  122. if (IEVersion() < 10) {
  123. xmlReq.onreadystatechange = function () {
  124. if (xmlReq.readyState != 4)
  125. return;
  126. if (xmlReq.bTimeout) {
  127. return;
  128. }
  129. if (xmlReq.status === 200)
  130. xmlReq.onload();
  131. else
  132. xmlReq.onerror();
  133. }
  134. }
  135. xmlReq.timeout = options.timeout;
  136. xmlReq.send(options.sendData)
  137. }
  138. startWpsInnder(options.tryCount);
  139. return;
  140. }
  141. var fromCharCode = String.fromCharCode;
  142. // encoder stuff
  143. var cb_utob = function (c) {
  144. if (c.length < 2) {
  145. var cc = c.charCodeAt(0);
  146. return cc < 0x80 ? c :
  147. cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6)) +
  148. fromCharCode(0x80 | (cc & 0x3f))) :
  149. (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f)) +
  150. fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) +
  151. fromCharCode(0x80 | (cc & 0x3f)));
  152. } else {
  153. var cc = 0x10000 +
  154. (c.charCodeAt(0) - 0xD800) * 0x400 +
  155. (c.charCodeAt(1) - 0xDC00);
  156. return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07)) +
  157. fromCharCode(0x80 | ((cc >>> 12) & 0x3f)) +
  158. fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) +
  159. fromCharCode(0x80 | (cc & 0x3f)));
  160. }
  161. };
  162. var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
  163. var utob = function (u) {
  164. return u.replace(re_utob, cb_utob);
  165. };
  166. var _encode = function (u) {
  167. var isUint8Array = Object.prototype.toString.call(u) === '[object Uint8Array]';
  168. if (isUint8Array)
  169. return u.toString('base64')
  170. else
  171. return btoa(utob(String(u)));
  172. }
  173. if (typeof window.btoa !== 'function') window.btoa = func_btoa;
  174. function func_btoa(input) {
  175. var str = String(input);
  176. var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  177. for (
  178. // initialize result and counter
  179. var block, charCode, idx = 0, map = chars, output = '';
  180. // if the next str index does not exist:
  181. // change the mapping table to "="
  182. // check if d has no fractional digits
  183. str.charAt(idx | 0) || (map = '=', idx % 1);
  184. // "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
  185. output += map.charAt(63 & block >> 8 - idx % 1 * 8)
  186. ) {
  187. charCode = str.charCodeAt(idx += 3 / 4);
  188. if (charCode > 0xFF) {
  189. throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
  190. }
  191. block = block << 8 | charCode;
  192. }
  193. return output;
  194. }
  195. /**
  196. * 将字符串进行Base64编码
  197. * @param {*} u 需要编码的数据
  198. * @param {*} urisafe 返回值,编码后的数据
  199. * @returns urisafe
  200. */
  201. var encode = function (u, urisafe) {
  202. return !urisafe ?
  203. _encode(u) :
  204. _encode(String(u)).replace(/[+\/]/g, function (m0) {
  205. return m0 == '+' ? '-' : '_';
  206. }).replace(/=/g, '');
  207. };
  208. /**
  209. * 获取IE浏览器版本
  210. * @returns IE浏览器版本
  211. */
  212. function IEVersion() {
  213. var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
  214. var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
  215. var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
  216. var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
  217. if (isIE) {
  218. var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
  219. reIE.test(userAgent);
  220. var fIEVersion = parseFloat(RegExp["$1"]);
  221. if (fIEVersion == 7) {
  222. return 7;
  223. } else if (fIEVersion == 8) {
  224. return 8;
  225. } else if (fIEVersion == 9) {
  226. return 9;
  227. } else if (fIEVersion == 10) {
  228. return 10;
  229. } else {
  230. return 6; //IE版本<=7
  231. }
  232. } else if (isEdge) {
  233. return 20; //edge
  234. } else if (isIE11) {
  235. return 11; //IE11
  236. } else {
  237. return 30; //不是ie浏览器
  238. }
  239. }
  240. /**
  241. * 启动wps客户端,加载项执行操作,无返回值
  242. * @param {*} options 参数对象,详情见下:
  243. * @param {*} clientType 加载项类型, wps / wpp / et
  244. * @param {*} name 加载项名称
  245. * @param {*} func 客户端加载项要执行的方法
  246. * @param {*} param 客户端家乡执行方法的参数
  247. * @param {*} urlBase 网页路径前缀
  248. * @param {*} callback 回调函数
  249. * @param {*} tryCount 请求失败后的尝试次数
  250. * @param {*} bPop 是否弹出浏览器提示对话框
  251. * @param {*} wpsclient wpsclient对象
  252. */
  253. function WpsStart(options) {
  254. var startInfo = {
  255. "name": options.name,
  256. "function": options.func,
  257. "info": options.param.param,
  258. "jsPluginsXml": options.param.jsPluginsXml
  259. };
  260. var strData = JSON.stringify(startInfo);
  261. if (IEVersion() < 10) {
  262. try {
  263. eval("strData = '" + JSON.stringify(startInfo) + "';");
  264. } catch (err) {
  265. }
  266. }
  267. var baseData = encode(strData);
  268. var url = options.urlBase + "/" + options.clientType + "/runParams";
  269. var data = "ksowebstartup" + options.clientType + "://" + baseData;
  270. startWps({
  271. url: url,
  272. sendData: data,
  273. callback: options.callback,
  274. tryCount: options.tryCount,
  275. bPop: options.bPop,
  276. timeout: 5000,
  277. concurrent: false,
  278. client: options.wpsclient
  279. });
  280. }
  281. /**
  282. * 服务端版本为空时,通过该接口启动wps
  283. * @param {*} options 参数对象,详情见下:
  284. * @param {*} clientType 加载项类型, wps / wpp / et
  285. * @param {*} name 加载项名称
  286. * @param {*} func 客户端加载项要执行的方法
  287. * @param {*} param 客户端家乡执行方法的参数
  288. * @param {*} urlBase 网页路径前缀
  289. * @param {*} callback 回调函数
  290. * @param {*} wpsclient wpsclient对象
  291. * @param {*} concurrent 请求是否同步发送
  292. */
  293. function WpsStartWrap(options) {
  294. WpsStart({
  295. clientType: options.clientType,
  296. name: options.name,
  297. func: options.func,
  298. param: options.param,
  299. urlBase: options.urlBase,
  300. callback: options.callback,
  301. tryCount: 4,
  302. bPop: true,
  303. wpsclient: options.wpsclient,
  304. })
  305. }
  306. /**
  307. * 支持浏览器触发,WPS有返回值的启动
  308. *
  309. * @param {*} clientType 组件类型
  310. * @param {*} name WPS加载项名称
  311. * @param {*} func WPS加载项入口方法
  312. * @param {*} param 参数:包括WPS加载项内部定义的方法,参数等
  313. * @param {*} callback 回调函数
  314. * @param {*} tryCount 重试次数
  315. * @param {*} bPop 是否弹出浏览器提示对话框
  316. */
  317. var exId = 0;
  318. function WpsStartWrapExInner(options) {
  319. var infocontent = options.param.param;
  320. if (!options.wpsclient || options.wpsclient.single) {
  321. infocontent = JSON.stringify(options.param.param);
  322. var rspUrl = options.urlBase + "/transferEcho/runParams";
  323. var time = new Date();
  324. var cmdId = "js" + time.getTime() + "_" + exId;
  325. var funcEx = "var res = " + options.func;
  326. var cbCode = "var xhr = new XMLHttpRequest();xhr.open('POST', '" + rspUrl + "');xhr.send(JSON.stringify({id: '" + cmdId + "', response: res}));" //res 为func执行返回值
  327. var infoEx = infocontent + ");" + cbCode + "void(0";
  328. options.func = funcEx;
  329. infocontent = infoEx;
  330. }
  331. var startInfo = {
  332. "name": options.name,
  333. "function": options.func,
  334. "info": infocontent,
  335. "showToFront": options.param.showToFront,
  336. "jsPluginsXml": options.param.jsPluginsXml,
  337. };
  338. var strData = JSON.stringify(startInfo);
  339. if (IEVersion() < 10) {
  340. try {
  341. eval("strData = '" + JSON.stringify(startInfo) + "';");
  342. } catch (err) {
  343. }
  344. }
  345. var baseData = encode(strData);
  346. var wrapper;
  347. if (!options.wpsclient|| options.wpsclient.single) {
  348. var url = options.urlBase + "/transfer/runParams";
  349. var data = "ksowebstartup" + options.clientType + "://" + baseData;
  350. wrapper = {
  351. id: cmdId,
  352. app: options.clientType,
  353. data: data,
  354. serverId: serverId,
  355. mode: options.silentMode ? "true" : "false",
  356. };
  357. }
  358. else {
  359. var url = options.urlBase + "/transferEx/runParams";
  360. wrapper = {
  361. id: options.wpsclient.clientId,
  362. app: options.clientType,
  363. data: baseData,
  364. mode: options.wpsclient.silentMode ? "true" : "false",
  365. serverId: serverId
  366. };
  367. }
  368. wrapper = JSON.stringify(wrapper);
  369. startWps({
  370. url: url,
  371. sendData: wrapper,
  372. callback: options.callback,
  373. tryCount: options.tryCount,
  374. bPop: options.bPop,
  375. timeout: 0,
  376. concurrent: options.concurrent,
  377. client: options.wpsclient
  378. });
  379. }
  380. var serverVersion = "wait"
  381. var cloudSvrStart = true;
  382. var initCloudsvr = false;
  383. /**
  384. * 获取服务端版本号的接口
  385. * @param {*} options 参数对象,详情见下:
  386. * @param {*} clientType 加载项类型, wps / wpp / et
  387. * @param {*} name 加载项名称
  388. * @param {*} func 客户端加载项要执行的方法
  389. * @param {*} param 客户端家乡执行方法的参数
  390. * @param {*} urlBase 网页路径前缀
  391. * @param {*} callback 回调函数
  392. * @param {*} wpsclient wpsclient对象
  393. * @param {*} concurrent 请求是否同步发送
  394. */
  395. function WpsStartWrapVersionInner(options) {
  396. if (serverVersion == "wait") {
  397. if (cloudSvrStart == false) {
  398. InitWpsCloudSvr();
  399. initCloudsvr = true;
  400. }
  401. startWps({
  402. url: options.urlBase + '/version',
  403. sendData: JSON.stringify({ serverId: serverId }),
  404. callback: function (res) {
  405. if (res.status !== 0) {
  406. options.callback(res)
  407. return;
  408. }
  409. serverVersion = res.response;
  410. cloudSvrStart = true;
  411. options.tryCount = 1
  412. options.bPop = false
  413. if (serverVersion === "") {
  414. WpsStart(options)
  415. } else if (serverVersion < "1.0.1" && options.wpsclient) {
  416. options.wpsclient.single = true;
  417. WpsStartWrapExInner(options);
  418. } else {
  419. WpsStartWrapExInner(options);
  420. }
  421. },
  422. tryCount: 4,
  423. bPop: true,
  424. timeout: 5000,
  425. concurrent: options.concurrent
  426. });
  427. } else {
  428. options.tryCount = 4
  429. options.bPop = true
  430. if (serverVersion === "") {
  431. WpsStartWrap(options)
  432. } else if (serverVersion < "1.0.1" && options.wpsclient) {
  433. options.wpsclient.single = true;
  434. WpsStartWrapExInner(options);
  435. } else {
  436. WpsStartWrapExInner(options);
  437. }
  438. }
  439. }
  440. var HeartBeatCode =
  441. "function getHttpObj() {\n"
  442. + " var httpobj = null;\n"
  443. + " if (IEVersion() < 10) {\n"
  444. + " try {\n"
  445. + " httpobj = new XDomainRequest();\n"
  446. + " } catch (e1) {\n"
  447. + " httpobj = new createXHR();\n"
  448. + " }\n"
  449. + " } else {\n"
  450. + " httpobj = new createXHR();\n"
  451. + " }\n"
  452. + " return httpobj;\n"
  453. + " }\n"
  454. + " \n"
  455. + " function createXHR() {\n"
  456. + " if (typeof XMLHttpRequest != 'undefined') {\n"
  457. + " return new XMLHttpRequest();\n"
  458. + " } else if (typeof ActiveXObject != 'undefined') {\n"
  459. + " var versions = [\n"
  460. + " 'MSXML2.XMLHttp.6.0',\n"
  461. + " 'MSXML2.XMLHttp.3.0',\n"
  462. + " 'MSXML2.XMLHttp'\n"
  463. + " ];\n"
  464. + " \n"
  465. + " for (var i = 0; i < versions.length; i++) {\n"
  466. + " try {\n"
  467. + " return new ActiveXObject(versions[i]);\n"
  468. + " } catch (e) {\n"
  469. + " \n"
  470. + " }\n"
  471. + " }\n"
  472. + " } else {\n"
  473. + " throw new Error('您的浏览器不支持XHR对象');\n"
  474. + " }\n"
  475. + " }\n"
  476. + " \n"
  477. + " function IEVersion() {\n"
  478. + " var userAgent = navigator.userAgent; \n"
  479. + " var isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1;\n"
  480. + " var isEdge = userAgent.indexOf('Edge') > -1 && !isIE; \n"
  481. + " var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1;\n"
  482. + " if (isIE) {\n"
  483. + " var reIE = new RegExp('MSIE (\\d+\\.\\d+);');\n"
  484. + " reIE.test(userAgent);\n"
  485. + " var fIEVersion = parseFloat(RegExp['$1']);\n"
  486. + " if (fIEVersion == 7) {\n"
  487. + " return 7;\n"
  488. + " } else if (fIEVersion == 8) {\n"
  489. + " return 8;\n"
  490. + " } else if (fIEVersion == 9) {\n"
  491. + " return 9;\n"
  492. + " } else if (fIEVersion == 10) {\n"
  493. + " return 10;\n"
  494. + " } else {\n"
  495. + " return 6; \n"
  496. + " }\n"
  497. + " } else if (isEdge) {\n"
  498. + " return 20; \n"
  499. + " } else if (isIE11) {\n"
  500. + " return 11; \n"
  501. + " } else {\n"
  502. + " return 30; \n"
  503. + " }\n"
  504. + " }\n"
  505. + " var heartBeatStart = false;\n"
  506. + " function checkLastRegTime() {\n"
  507. + " var now = new Date().valueOf();\n"
  508. + " var TimeGap = now - LastRegTime;\n"
  509. + " if (TimeGap > 5000 && !heartBeatStart) {\n"
  510. + " HeartBeat();\n"
  511. + " heartBeatStart = true;\n"
  512. + " }\n"
  513. + " }\n"
  514. + " \n"
  515. + " function HeartBeat() {\n"
  516. + " var heartBeatItem = function () {\n"
  517. + " var xhr = getHttpObj();\n"
  518. + " xhr.onload = function (e) {\n"
  519. + " self.setTimeout(heartBeatItem, 5000);\n"
  520. + " }\n"
  521. + " xhr.onerror = function (e) {\n"
  522. + " self.setTimeout(heartBeatItem, 5000);\n"
  523. + " }\n"
  524. + " xhr.ontimeout = function (e) {\n"
  525. + " self.setTimeout(heartBeatItem, 5000);\n"
  526. + " }\n"
  527. + " xhr.open('POST', 'http://127.0.0.1:58890/askwebnotify', true);\n"
  528. + " xhr.timeout = 2000;\n"
  529. + " xhr.send(JSON.stringify(paramStr));\n"
  530. + " }\n"
  531. + " heartBeatItem();\n"
  532. + " }\n"
  533. + " \n"
  534. + " var paramStr;\n"
  535. + " var startCheck = false;\n"
  536. + " self.addEventListener('message', function (event) {\n"
  537. + " var data = event.data;\n"
  538. + " paramStr = data.param\n"
  539. + " paramStr.heartBeat = true\n"
  540. + " LastRegTime = data.LastRegTime;\n"
  541. + " if (!startCheck) {\n"
  542. + " startCheck = true;\n"
  543. + " self.setInterval(checkLastRegTime, 5000)\n"
  544. + " }\n"
  545. + " }, false);\n"
  546. /**
  547. * 生成guid的接口
  548. * @returns guid
  549. */
  550. function guid() {
  551. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  552. var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  553. return v.toString(16);
  554. });
  555. }
  556. /**
  557. * 开启多用户的接口
  558. */
  559. var serverId = undefined
  560. function EnableMultiUser() {
  561. serverId = getServerId();
  562. }
  563. /**
  564. * 自定义协议启动服务端
  565. * 默认不带参数serverId,linux未升级之前不要使用多用户
  566. */
  567. function InitWpsCloudSvr () {
  568. if(serverId == undefined)
  569. window.location.href = "ksoWPSCloudSvr://start=RelayHttpServer"//是否启动wps弹框
  570. else
  571. window.location.href = "ksoWPSCloudSvr://start=RelayHttpServer" + "&serverId=" + serverId //是否启动wps弹框
  572. }
  573. /**
  574. * 获取serverId的接口
  575. * @returns serverId
  576. */
  577. function getServerId() {
  578. if (window.localStorage) {
  579. if (localStorage.getItem("serverId")) {
  580. //
  581. }
  582. else {
  583. localStorage.setItem("serverId", guid());
  584. }
  585. return localStorage.getItem("serverId");
  586. }
  587. else {
  588. return guid();
  589. }
  590. }
  591. /**
  592. * 将字符串转成二进制,这里用来将字符串化后的js代码转成二进制文件
  593. * @param {*} code
  594. * @returns js文件对象的url
  595. */
  596. function codeToBlob(code) {
  597. var blob = new Blob([code], { type: 'text/javascript' }); // 生成js文件对象
  598. var objectURL = window.URL.createObjectURL(blob); // 生成js文件的url
  599. return objectURL;
  600. }
  601. var RegWebNotifyMap = { wps: {}, wpp: {}, et: {} }
  602. var bWebNotifyUseTimeout = true
  603. function WebNotifyUseTimeout(value) {
  604. bWebNotifyUseTimeout = value ? true : false
  605. }
  606. var g_businessId = Number(Math.random().toString().substr(3, 5) + Date.parse(new Date())).toString(36);
  607. var HeartBeatWorker
  608. if (window.Worker) {
  609. try {
  610. HeartBeatWorker = new Worker(codeToBlob(HeartBeatCode));
  611. } catch (error) {
  612. //
  613. }
  614. }
  615. var g_LastRegTime;
  616. /**
  617. * 注册一个前端页面接收WPS传来消息的方法
  618. * @param {*} clientType wps | et | wpp
  619. * @param {*} name WPS加载项的名称
  620. * @param {*} callback 回调函数
  621. * @param {*} wpsclient wpsclient对象
  622. */
  623. function RegWebNotify(clientType, name, callback, wpsclient) {
  624. if (clientType != "wps" && clientType != "wpp" && clientType != "et")
  625. return;
  626. var paramStr = {}
  627. if (wpsclient) {
  628. if (wpsclient.notifyRegsitered == true) {
  629. return
  630. }
  631. wpsclient.notifyRegsitered = true;
  632. paramStr = {
  633. clientId: wpsclient.clientId,
  634. name: name,
  635. type: clientType,
  636. serverId: serverId
  637. }
  638. if (HeartBeatWorker)
  639. paramStr.businessId = g_businessId
  640. }
  641. else {
  642. if (typeof callback != 'function')
  643. return
  644. if (RegWebNotifyMap[clientType][name]) {
  645. RegWebNotifyMap[clientType][name] = callback;
  646. return
  647. }
  648. var RegWebNotifyID = new Date().valueOf() + ''
  649. paramStr = {
  650. id: RegWebNotifyID,
  651. name: name,
  652. type: clientType,
  653. serverId: serverId
  654. }
  655. if (HeartBeatWorker)
  656. paramStr.businessId = g_businessId
  657. RegWebNotifyMap[clientType][name] = callback
  658. }
  659. var askItem = function () {
  660. var xhr = getHttpObj()
  661. xhr.onload = function (e) {
  662. if (xhr.responseText == "WPSInnerMessage_quit") {
  663. return;
  664. }
  665. try {
  666. var resText = JSON.parse(xhr.responseText);
  667. if (typeof resText == 'object') {
  668. paramStr.messageId = resText.msgId;
  669. }
  670. if (wpsclient) {
  671. if (typeof resText.data == 'object') // 如果发的数据是字符串化后的json对象,这里的resText.data就是一个json对象,可以输出自己想要的json数据
  672. wpsclient.OnRegWebNotify(resText.data.data)
  673. else
  674. wpsclient.OnRegWebNotify(resText.data)
  675. } else {
  676. var func = RegWebNotifyMap[clientType][name]
  677. if (typeof resText.data == 'object') // 如果发的数据是字符串化后的json对象,这里的resText.data就是一个json对象,可以输出自己想要的json数据
  678. func(resText.data.data)
  679. else
  680. func(resText.data)
  681. }
  682. }
  683. catch (e) {
  684. // 这里做一个容错,即使json解析失败,也要把msgId提取出来,发回给服务端,避免消息清不掉一直重复发送
  685. // 同时把data也取出来,但是格式无法保证
  686. var str = xhr.responseText
  687. var idx1 = str.indexOf(":")
  688. var idx2 = str.indexOf(",")
  689. paramStr.messageId = parseInt(str.substring(idx1 + 1, idx2))
  690. var idx3 = str.indexOf("\"data\"")
  691. var idx4 = str.indexOf("}")
  692. var data = str.substring(idx3, idx4)
  693. if (wpsclient) {
  694. if (data)
  695. wpsclient.OnRegWebNotify(data)
  696. else
  697. wpsclient.OnRegWebNotify(xhr.responseText)
  698. } else {
  699. var func = RegWebNotifyMap[clientType][name]
  700. if (data)
  701. func(data)
  702. else
  703. func(xhr.responseText)
  704. }
  705. }
  706. window.setTimeout(askItem, 300)
  707. }
  708. xhr.onerror = function (e) {
  709. if (bWebNotifyUseTimeout)
  710. window.setTimeout(askItem, 1000)
  711. else
  712. window.setTimeout(askItem, 10000)
  713. }
  714. xhr.ontimeout = function (e) {
  715. if (bWebNotifyUseTimeout)
  716. window.setTimeout(askItem, 300)
  717. else
  718. window.setTimeout(askItem, 10000)
  719. }
  720. if (IEVersion() < 10) {
  721. xhr.onreadystatechange = function () {
  722. if (xhr.readyState != 4)
  723. return;
  724. if (xhr.bTimeout) {
  725. return;
  726. }
  727. if (xhr.status === 200)
  728. xhr.onload();
  729. else
  730. xhr.onerror();
  731. }
  732. }
  733. xhr.open('POST', GetUrlBase() + '/askwebnotify', true)
  734. if (bWebNotifyUseTimeout)
  735. xhr.timeout = 2000;
  736. if (HeartBeatWorker) {
  737. g_LastRegTime = new Date().valueOf();
  738. var param = {
  739. param: {
  740. name: name,
  741. type: clientType,
  742. businessId: g_businessId,
  743. serverId: serverId
  744. },
  745. LastRegTime: g_LastRegTime
  746. }
  747. HeartBeatWorker.postMessage(param)
  748. }
  749. xhr.send(JSON.stringify(paramStr));
  750. }
  751. window.setTimeout(askItem, 2000)
  752. }
  753. /**
  754. * 获取网页路径前缀
  755. * @returns url前缀
  756. */
  757. function GetUrlBase() {
  758. if (location.protocol == "https:")
  759. return "https://127.0.0.1:58891"
  760. return "http://127.0.0.1:58890"
  761. }
  762. /**
  763. * 获取服务端版本号的接口,这里主要是初始化一些参数
  764. * @param {*} clientType 加载项类型, wps / wpp / et
  765. * @param {*} name 加载项名称
  766. * @param {*} func 客户端加载项要执行的方法
  767. * @param {*} param 客户端家乡执行方法的参数
  768. * @param {*} callback 回调函数
  769. * @param {*} showToFront 设置客户端是否显示到前面
  770. * @param {*} jsPluginsXml 设置加载项路径
  771. * @param {*} silentMode 静默启动WPS
  772. */
  773. function WpsStartWrapVersion(clientType, name, func, param, callback, showToFront, jsPluginsXml,silentMode) {
  774. var paramEx = {
  775. jsPluginsXml: jsPluginsXml ? jsPluginsXml : "",
  776. showToFront: typeof (showToFront) == 'boolean' ? showToFront : true,
  777. param: (typeof (param) == 'object' ? param : JSON.parse(param))
  778. }
  779. var options = {
  780. clientType: clientType,
  781. name: name,
  782. func: func,
  783. param: paramEx,
  784. urlBase: GetUrlBase(),
  785. callback: callback,
  786. wpsclient: undefined,
  787. concurrent: true,
  788. silentMode:silentMode
  789. }
  790. WpsStartWrapVersionInner(options);
  791. }
  792. //从外部浏览器远程调用 WPS 加载项中的方法
  793. var WpsInvoke = {
  794. InvokeAsHttp: WpsStartWrapVersion,
  795. InvokeAsHttps: WpsStartWrapVersion,
  796. RegWebNotify: RegWebNotify,
  797. ClientType: {
  798. wps: "wps",
  799. et: "et",
  800. wpp: "wpp"
  801. },
  802. CreateXHR: getHttpObj,
  803. IsClientRunning: IsClientRunning
  804. }
  805. window.wpsclients = [];
  806. /**
  807. * @constructor WpsClient wps客户端
  808. * @param {string} clientType 必传参数,加载项类型,有效值为"wps","wpp","et";分别表示文字,演示,电子表格
  809. */
  810. function WpsClient(clientType) {
  811. /**
  812. * 设置RegWebNotify的回调函数,加载项给业务端发消息通过该函数
  813. * @memberof WpsClient
  814. * @member onMessage
  815. */
  816. this.onMessage;
  817. /**
  818. * 设置加载项路径
  819. * @memberof WpsClient
  820. * @member jsPluginsXml
  821. */
  822. this.jsPluginsXml;
  823. /**
  824. * 内部成员,外部无需调用
  825. */
  826. this.notifyRegsitered = false;
  827. this.clientId = "";
  828. this.concurrent = false;
  829. this.clientType = clientType;
  830. this.firstRequest = true;
  831. /**
  832. * 内部函数,外部无需调用
  833. * @param {*} options
  834. */
  835. this.initWpsClient = function (options) {
  836. options.clientType = this.clientType
  837. options.wpsclient = this
  838. options.concurrent = this.firstRequest ? true : this.concurrent
  839. this.firstRequest = false;
  840. WpsStartWrapVersionInner(options)
  841. }
  842. /**
  843. * 以http启动
  844. * @param {string} name 加载项名称
  845. * @param {string} func 要调用的加载项中的函数行
  846. * @param {string} param 在加载项中执行函数func要传递的数据
  847. * @param {function({int, string})} callback 回调函数,status = 0 表示成功,失败请查看message信息
  848. * @param {bool} showToFront 设置wps是否显示到前面来
  849. * @return {string} "Failed to send message to WPS." 发送消息失败,客户端已关闭;
  850. * "WPS Addon is not response." 加载项阻塞,函数执行失败
  851. */
  852. this.InvokeAsHttp = function (name, func, param, callback, showToFront) {
  853. function clientCallback(res) {
  854. //this不是WpsClient
  855. if (res.status !== 0 || serverVersion < "1.0.1") {
  856. if (callback)
  857. callback(res);
  858. RegWebNotify(clientType, name, this.client.onMessage)
  859. return;
  860. }
  861. var resObject = JSON.parse(res.response);
  862. if (this.client.clientId == "") {
  863. this.client.clientId = resObject.clientId;
  864. }
  865. this.client.concurrent = true;
  866. if (typeof resObject.data == "object")
  867. res.response = JSON.stringify(resObject.data);
  868. else
  869. res.response = resObject.data;
  870. if (IEVersion() < 10)
  871. eval(" res.response = '" + res.response + "';");
  872. if (callback)
  873. callback(res);
  874. this.client.RegWebNotify(name);
  875. }
  876. var paramEx = {
  877. jsPluginsXml: this.jsPluginsXml ? this.jsPluginsXml : "",
  878. showToFront: typeof (showToFront) == 'boolean' ? showToFront : true,
  879. param: (typeof (param) == 'object' ? param : JSON.parse(param))
  880. }
  881. this.initWpsClient({
  882. name: name,
  883. func: func,
  884. param: paramEx,
  885. urlBase: GetUrlBase(),
  886. callback: clientCallback
  887. })
  888. }
  889. /**
  890. * 以https启动
  891. * @param {string} name 加载项名称
  892. * @param {string} func 要调用的加载项中的函数行
  893. * @param {string} param 在加载项中执行函数func要传递的数据
  894. * @param {function({int, string})} callback 回调函数,status = 0 表示成功,失败请查看message信息
  895. * @param {bool} showToFront 设置wps是否显示到前面来
  896. */
  897. this.InvokeAsHttps = function (name, func, param, callback, showToFront) {
  898. var paramEx = {
  899. jsPluginsXml: this.jsPluginsXml ? this.jsPluginsXml : "",
  900. showToFront: typeof (showToFront) == 'boolean' ? showToFront : true,
  901. param: (typeof (param) == 'object' ? param : JSON.parse(param))
  902. }
  903. this.initWpsClient({
  904. name: name,
  905. func: func,
  906. param: paramEx,
  907. urlBase: GetUrlBase(),
  908. callback: callback
  909. })
  910. }
  911. /**
  912. * 内部函数,外部无需调用
  913. * @param {*} name
  914. */
  915. this.RegWebNotify = function (name) {
  916. RegWebNotify(this.clientType, name, null, this);
  917. }
  918. /**
  919. * 消息注册函数的回调函数
  920. * @param {*} message 客户端发来的消息
  921. */
  922. this.OnRegWebNotify = function (message) {
  923. if (this.onMessage)
  924. this.onMessage(message)
  925. }
  926. /**
  927. * 以静默模式启动客户端
  928. * @param {string} name 必传参数,加载项名称
  929. * @param {function({int, string})} [callback] 回调函数,status = 0 表示成功,失败请查看message信息
  930. */
  931. this.StartWpsInSilentMode = function (name, callback) {
  932. function initCallback(res) {
  933. //this不是WpsClient
  934. if (res.status !== 0 || serverVersion < "1.0.1") {
  935. if (callback)
  936. callback(res);
  937. RegWebNotify(clientType, name, this.client.onMessage)
  938. return;
  939. }
  940. if (this.client.clientId == "") {
  941. this.client.clientId = JSON.parse(res.response).clientId;
  942. window.wpsclients[window.wpsclients.length] = { name: name, client: this.client };
  943. }
  944. res.response = JSON.stringify(JSON.parse(res.response).data);
  945. this.client.concurrent = true;
  946. if (callback) {
  947. callback(res);
  948. }
  949. this.client.RegWebNotify(name);
  950. }
  951. var paramEx = {
  952. jsPluginsXml: this.jsPluginsXml,
  953. showToFront: false,
  954. param: { status: "InitInSilentMode" }
  955. }
  956. this.silentMode = true;
  957. this.initWpsClient({
  958. name: name,
  959. func: "",
  960. param: paramEx,
  961. urlBase: GetUrlBase(),
  962. callback: initCallback
  963. })
  964. }
  965. /**
  966. * 显示客户端到最前面
  967. * @param {string} name 必传参数,加载项名称
  968. * @param {function({int, string})} [callback] 回调函数
  969. */
  970. this.ShowToFront = function (name, callback) {
  971. if (serverVersion < "1.0.1") {
  972. if (callback) {
  973. callback({
  974. status: 4,
  975. message: "当前客户端不支持,请升级客户端"
  976. });
  977. return;
  978. }
  979. return;
  980. }
  981. if (this.clientId == "") {
  982. if (callback) callback({
  983. status: 3,
  984. message: "没有静默启动客户端"
  985. });
  986. return;
  987. }
  988. var paramEx = {
  989. jsPluginsXml: "",
  990. showToFront: true,
  991. param: { status: "ShowToFront" }
  992. }
  993. this.initWpsClient({
  994. name: name,
  995. func: "",
  996. param: paramEx,
  997. urlBase: GetUrlBase(),
  998. callback: callback
  999. })
  1000. }
  1001. /**
  1002. * 关闭未显示出来的静默启动客户端
  1003. * @param {string} name 必传参数,加载项名称
  1004. * @param {function({int, string})} [callback] 回调函数
  1005. */
  1006. this.CloseSilentClient = function (name, callback) {
  1007. if (serverVersion < "1.0.1") {
  1008. if (callback) {
  1009. callback({
  1010. status: 4,
  1011. message: "当前客户端不支持,请升级客户端"
  1012. });
  1013. return;
  1014. }
  1015. return;
  1016. }
  1017. if (this.clientId == "") {
  1018. if (callback) callback({
  1019. status: 3,
  1020. message: "没有静默启动客户端"
  1021. });
  1022. return;
  1023. }
  1024. var paramEx = {
  1025. jsPluginsXml: "",
  1026. showToFront: false,
  1027. param: undefined
  1028. }
  1029. var func;
  1030. if (this.clientType == "wps")
  1031. func = "wps.WpsApplication().Quit"
  1032. else if (this.clientType == "et")
  1033. func = "wps.EtApplication().Quit"
  1034. else if (this.clientType == "wpp")
  1035. func = "wps.WppApplication().Quit"
  1036. function closeSilentClient(res) {
  1037. if (res.status == 0)
  1038. this.client.clientId = ""
  1039. if (callback) callback(res);
  1040. return;
  1041. }
  1042. this.initWpsClient({
  1043. name: name,
  1044. func: func,
  1045. param: paramEx,
  1046. urlBase: GetUrlBase(),
  1047. callback: closeSilentClient
  1048. })
  1049. }
  1050. /**
  1051. * 当前客户端是否在运行,使用WpsClient.IsClientRunning()进行调用
  1052. * @param {function({int, string})} [callback] 回调函数,"Client is running." 客户端正在运行
  1053. * "Client is not running." 客户端没有运行
  1054. */
  1055. this.IsClientRunning = function (callback) {
  1056. if (serverVersion < "1.0.1") {
  1057. if (callback) {
  1058. callback({
  1059. status: 4,
  1060. message: "当前客户端不支持,请升级客户端"
  1061. });
  1062. return;
  1063. }
  1064. return;
  1065. }
  1066. IsClientRunning(this.clientType, callback, this)
  1067. }
  1068. }
  1069. /**
  1070. * 初始化sdk,用来减少在服务进程启动时自定义协议弹框出现的次数
  1071. */
  1072. function InitSdk() {
  1073. var url = GetUrlBase() + "/version";
  1074. startWps({
  1075. url: url,
  1076. callback: function (res) {
  1077. if (res.status !== 0) {
  1078. cloudSvrStart = false;
  1079. return;
  1080. }
  1081. if (serverVersion == "wait") {
  1082. InitMultiUser();
  1083. }
  1084. },
  1085. tryCount: 1,
  1086. bPop: false,
  1087. timeout: 1000
  1088. });
  1089. }
  1090. InitSdk();
  1091. /**
  1092. * 初始化多用户模式
  1093. */
  1094. function InitMultiUser() {
  1095. var url = GetUrlBase() + "/version";
  1096. startWps({
  1097. url: url,
  1098. sendData: JSON.stringify({ serverId: serverId }),
  1099. callback: function (res) {
  1100. if (res.status !== 0) {
  1101. cloudSvrStart = false;
  1102. return;
  1103. }
  1104. if (serverVersion == "wait") {
  1105. serverVersion = res.response;
  1106. cloudSvrStart = true;
  1107. }
  1108. },
  1109. tryCount: 1,
  1110. bPop: false,
  1111. timeout: 1000
  1112. });
  1113. }
  1114. if (typeof noGlobal === "undefined") {
  1115. window.WpsInvoke = WpsInvoke;
  1116. window.WpsClient = WpsClient;
  1117. window.WebNotifyUseTimeout = WebNotifyUseTimeout;
  1118. window.EnableMultiUser = EnableMultiUser;
  1119. }
  1120. /**
  1121. * 当前客户端是否在运行,使用WpsInvoke.IsClientRunning()进行调用
  1122. * @param {string} clientType 加载项类型
  1123. * @param {function} [callback] 回调函数,"Client is running." 客户端正在运行
  1124. * "Client is not running." 客户端没有运行
  1125. */
  1126. function IsClientRunning(clientType, callback, wpsclient) {
  1127. var url = GetUrlBase() + "/isRunning";
  1128. var wrapper = {
  1129. id: wpsclient == undefined ? undefined : wpsclient.clientId,
  1130. app: clientType,
  1131. serverId: serverId
  1132. }
  1133. wrapper = JSON.stringify(wrapper);
  1134. startWps({
  1135. url: url,
  1136. sendData: wrapper,
  1137. callback: callback,
  1138. tryCount: 1,
  1139. bPop: false,
  1140. timeout: 2000,
  1141. concurrent: true,
  1142. client: wpsclient
  1143. });
  1144. }
  1145. /**
  1146. * 获取publish.xml的内容
  1147. * @param {*} callBack 回调函数
  1148. */
  1149. function WpsAddonGetAllConfig(callBack) {
  1150. var baseData = JSON.stringify({ serverId: serverId });
  1151. startWps({
  1152. url: GetUrlBase() + "/publishlist",
  1153. type: "POST",
  1154. sendData: baseData,
  1155. callback: callBack,
  1156. tryCount: 3,
  1157. bPop: true,
  1158. timeout: 5000,
  1159. concurrent: true
  1160. });
  1161. }
  1162. /**
  1163. * 检查ribbon.xml文件是否有效
  1164. * @param {*} element 参数对象
  1165. * @param {*} callBack 回调函数
  1166. */
  1167. function WpsAddonVerifyStatus(element, callBack) {
  1168. var xmlReq = getHttpObj();
  1169. var offline = element.online === "false";
  1170. var url = offline ? element.url : element.url + "ribbon.xml";
  1171. xmlReq.open("POST", GetUrlBase() + "/redirect/runParams");
  1172. xmlReq.onload = function (res) {
  1173. if (offline && !res.target.response.startsWith("7z")) {
  1174. callBack({ status: 1, msg: "不是有效的7z格式" + url });
  1175. } else if (!offline && !res.target.response.startsWith("<customUI")) {
  1176. callBack({ status: 1, msg: "不是有效的ribbon.xml, " + url })
  1177. } else {
  1178. callBack({ status: 0, msg: "OK" })
  1179. }
  1180. }
  1181. xmlReq.onerror = function (res) {
  1182. xmlReq.bTimeout = true;
  1183. callBack({ status: 2, msg: "网页路径不可访问,如果是跨域问题,不影响使用" + url })
  1184. }
  1185. xmlReq.ontimeout = function (res) {
  1186. xmlReq.bTimeout = true;
  1187. callBack({ status: 3, msg: "访问超时" + url })
  1188. }
  1189. if (IEVersion() < 10) {
  1190. xmlReq.onreadystatechange = function () {
  1191. if (xmlReq.readyState != 4)
  1192. return;
  1193. if (xmlReq.bTimeout) {
  1194. return;
  1195. }
  1196. if (xmlReq.status === 200)
  1197. xmlReq.onload();
  1198. else
  1199. xmlReq.onerror();
  1200. }
  1201. }
  1202. xmlReq.timeout = 5000;
  1203. var data = {
  1204. method: "get",
  1205. url: url,
  1206. data: ""
  1207. }
  1208. var sendData = FormatSendData(data)
  1209. xmlReq.send(sendData);
  1210. }
  1211. /**
  1212. * 部署加载项,包括启动enable / disable禁用 / disableall禁用所有
  1213. * @param {*} element 参数对象
  1214. * @param {*} cmd 具体操作,enable / disable / disableall
  1215. * @param {*} callBack 回调函数
  1216. */
  1217. function WpsAddonHandleEx(element, cmd, callBack) {
  1218. var data = FormatData(element, cmd);
  1219. startWps({
  1220. url: GetUrlBase() + "/deployaddons/runParams",
  1221. type: "POST",
  1222. sendData: data,
  1223. callback: callBack,
  1224. tryCount: 3,
  1225. bPop: true,
  1226. timeout: 5000,
  1227. concurrent: true
  1228. });
  1229. }
  1230. /**
  1231. * 启用加载项
  1232. * @param {*} element 参数对象
  1233. * @param {*} callBack 回调函数
  1234. */
  1235. function WpsAddonEnable(element, callBack) {
  1236. WpsAddonHandleEx(element, "enable", callBack)
  1237. }
  1238. /**
  1239. * 禁用加载项
  1240. * @param {*} element 参数对象
  1241. * @param {*} callBack 回调函数
  1242. */
  1243. function WpsAddonDisable(element, callBack) {
  1244. WpsAddonHandleEx(element, "disable", callBack)
  1245. }
  1246. /**
  1247. * 生成json格式的数据
  1248. * @param {*} element 参数对象
  1249. * @param {*} cmd 具体操作,enable / disable / disableall
  1250. * @returns base64编码后的数据
  1251. */
  1252. function FormatData(element, cmd) {
  1253. var data = {
  1254. "cmd": cmd, //"enable", 启用, "disable", 禁用, "disableall", 禁用所有
  1255. "name": element.name,
  1256. "url": element.url,
  1257. "addonType": element.addonType,
  1258. "online": element.online,
  1259. "version": element.version
  1260. }
  1261. return FormatSendData(data);
  1262. }
  1263. /**
  1264. * 将json格式的数据字符串化,并进行base64编码
  1265. * @param {*} data 数据
  1266. * @returns base64编码后的数据
  1267. */
  1268. function FormatSendData(data) {
  1269. var strData = JSON.stringify(data);
  1270. if (IEVersion() < 10)
  1271. eval("strData = '" + JSON.stringify(strData) + "';");
  1272. if (serverVersion == "1.0.2") {
  1273. var base64Data = encode(strData);
  1274. return JSON.stringify({
  1275. serverId: serverId,
  1276. data: base64Data
  1277. })
  1278. }
  1279. else {
  1280. return encode(strData);
  1281. }
  1282. }
  1283. //管理 WPS 加载项
  1284. var WpsAddonMgr = {
  1285. getAllConfig: WpsAddonGetAllConfig,
  1286. verifyStatus: WpsAddonVerifyStatus,
  1287. enable: WpsAddonEnable,
  1288. disable: WpsAddonDisable,
  1289. }
  1290. if (typeof noGlobal === "undefined") {
  1291. window.WpsAddonMgr = WpsAddonMgr;
  1292. }
  1293. return { WpsInvoke: WpsInvoke, WpsAddonMgr: WpsAddonMgr, version: "1.0.21" };
  1294. });