LodopFuncs.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //==本JS是加载Lodop插件或Web打印服务CLodop/Lodop7的综合示例==
  2. //用双端口加载主JS文件LodopFuncs.js,以防其中某端口被占:
  3. var MainJS = "CLodopfuncs.js",
  4. URL_WS1 = "http://127.0.0.1:8000/" + MainJS,
  5. URL_WS2 = "http://127.0.0.1:18000/" + MainJS,
  6. URL_HTTP1 = "http://127.0.0.1:8000/" + MainJS,
  7. URL_HTTP2 = "http://127.0.0.1:18000/" + MainJS;
  8. var CreatedOKLodopObject, CLodopIsLocal, LoadJsState;
  9. //==判断是否需要CLodop(那些不支持插件的浏览器):==
  10. function needCLodop() {
  11. try {
  12. var ua = navigator.userAgent;
  13. if (ua.match(/Windows\sPhone/i) ||
  14. ua.match(/iPhone|iPod|iPad/i) ||
  15. ua.match(/Android/i) ||
  16. ua.match(/Edge\D?\d+/i))
  17. return true;
  18. var verTrident = ua.match(/Trident\D?\d+/i);
  19. var verIE = ua.match(/MSIE\D?\d+/i);
  20. var verOPR = ua.match(/OPR\D?\d+/i);
  21. var verFF = ua.match(/Firefox\D?\d+/i);
  22. var x64 = ua.match(/x64/i);
  23. if ((!verTrident) && (!verIE) && (x64)) return true;
  24. else if (verFF) {
  25. verFF = verFF[0].match(/\d+/);
  26. if ((verFF[0] >= 41) || (x64)) return true;
  27. } else if (verOPR) {
  28. verOPR = verOPR[0].match(/\d+/);
  29. if (verOPR[0] >= 32) return true;
  30. } else if ((!verTrident) && (!verIE)) {
  31. var verChrome = ua.match(/Chrome\D?\d+/i);
  32. if (verChrome) {
  33. verChrome = verChrome[0].match(/\d+/);
  34. if (verChrome[0] >= 41) return true;
  35. }
  36. }
  37. return false;
  38. } catch (err) {
  39. return true;
  40. }
  41. }
  42. //==检查加载成功与否,如没成功则用http(s)再试==
  43. function checkOrTryHttp() {
  44. if (window.getCLodop) {
  45. LoadJsState = "complete";
  46. return true;
  47. }
  48. if (LoadJsState == "loadingB" || LoadJsState == "complete") return;
  49. LoadJsState = "loadingB";
  50. var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
  51. var JS1 = document.createElement("script")
  52. ,JS2 = document.createElement("script");
  53. JS1.src = URL_HTTP1;
  54. JS2.src = URL_HTTP2;
  55. // 添加 CORS 错误处理
  56. JS1.onerror = function(e) {
  57. console.warn("加载 CLodopfuncs.js (端口8000) 失败:", e);
  58. // 尝试备用端口
  59. head.insertBefore(JS2, head.firstChild);
  60. };
  61. JS2.onerror = function(e) {
  62. console.warn("加载 CLodopfuncs.js (端口18000) 失败:", e);
  63. LoadJsState = "complete";
  64. // 显示 CORS 错误提示
  65. if (window.location.protocol === 'http:') {
  66. console.warn("CORS 错误: 公共 HTTP 网站无法访问本地资源,请使用 HTTPS 或修改浏览器设置");
  67. }
  68. };
  69. JS1.onload = JS2.onload = function() {
  70. LoadJsState = "complete";
  71. };
  72. head.insertBefore(JS1,head.firstChild);
  73. }
  74. //==加载Lodop对象的主过程:==
  75. (function loadCLodop(){
  76. if (!needCLodop()) return;
  77. CLodopIsLocal = !!((URL_WS1 + URL_WS2).match(/\/\/localho|\/\/127.0.0./i));
  78. LoadJsState = "loadingA";
  79. if (!window.WebSocket && window.MozWebSocket) window.WebSocket=window.MozWebSocket;
  80. // 直接走 HTTP 方式,不再尝试 WebSocket(HTTP 环境下 WS 必然被拦截)
  81. checkOrTryHttp();
  82. })();
  83. //==获取LODOP对象主过程,判断是否安装、需否升级:==
  84. function getLodop(oOBJECT, oEMBED) {
  85. var strFontTag = "<br><font color='#FF00FF'>打印控件";
  86. var strLodopInstall = strFontTag + "未安装!点击这里<a href='install_lodop32.exe' target='_self'>执行安装</a>";
  87. var strLodopUpdate = strFontTag + "需要升级!点击这里<a href='install_lodop32.exe' target='_self'>执行升级</a>";
  88. var strLodop64Install = strFontTag + "未安装!点击这里<a href='install_lodop64.exe' target='_self'>执行安装</a>";
  89. var strLodop64Update = strFontTag + "需要升级!点击这里<a href='install_lodop64.exe' target='_self'>执行升级</a>";
  90. var strLodop7Install_X86 = strFontTag + "未安装!点击这里<a href='http://www.c-lodop.com/download.html' target='_blank'>执行安装</a>";
  91. var strLodop7Install_ARM = strFontTag + "未安装!点击这里<a href='http://www.c-lodop.com/download.html' target='_blank'>执行安装</a>";
  92. var strCLodopInstallA = "<br><font color='#FF00FF'>Web打印服务CLodop未安装启动,点击这里<a href='http://www.c-lodop.com/download.html' target='_blank'>下载执行安装</a>";
  93. var strCLodopInstallB = "<br>(若此前已安装过,可<a href='CLodop.protocol:setup' target='_self'>点这里直接再次启动</a>)";
  94. var strCorsError = "<br><font color='#FF0000'>成功后请刷新本页面或重启浏览器</font>";
  95. var strCorsSolution = "<br><font color='#0066CC'></font>";
  96. var strInstallOK = "</font>";
  97. var LODOP;
  98. try {
  99. var isWinIE = (/MSIE/i.test(navigator.userAgent)) || (/Trident/i.test(navigator.userAgent));
  100. var isWinIE64 = isWinIE && (/x64/i.test(navigator.userAgent));
  101. var isLinuxX86 = (/Linux/i.test(navigator.platform)) && (/x86/i.test(navigator.platform));
  102. var isLinuxARM = (/Linux/i.test(navigator.platform)) && (/aarch/i.test(navigator.platform));
  103. if (needCLodop() || isLinuxX86 || isLinuxARM) {
  104. try {
  105. LODOP = window.getCLodop();
  106. } catch (err) {}
  107. if (!LODOP && LoadJsState !== "complete") {
  108. if (!LoadJsState)
  109. alert("未曾加载Lodop主JS文件,请先调用loadCLodop过程."); else
  110. alert("网页还没下载完毕,请稍等一下再操作.");
  111. return;
  112. }
  113. var strAlertMessage;
  114. if (!LODOP) {
  115. if (isLinuxX86)
  116. strAlertMessage = strLodop7Install_X86;
  117. else if (isLinuxARM)
  118. strAlertMessage = strLodop7Install_ARM;
  119. else {
  120. strAlertMessage = strCLodopInstallA + (CLodopIsLocal ? strCLodopInstallB : "");
  121. // 添加 CORS 错误提示
  122. if (window.location.protocol === 'http:') {
  123. strAlertMessage += strCorsError + strCorsSolution;
  124. }
  125. }
  126. document.body.innerHTML = strAlertMessage + strInstallOK + document.body.innerHTML;
  127. return;
  128. }
  129. } else {
  130. if (oOBJECT || oEMBED) {
  131. LODOP = isWinIE ? oOBJECT : oEMBED;
  132. } else if (!CreatedOKLodopObject) {
  133. LODOP = document.createElement("object");
  134. LODOP.setAttribute("width", 0);
  135. LODOP.setAttribute("height", 0);
  136. LODOP.setAttribute("style", "position:absolute;left:0px;top:-100px;width:0px;height:0px;");
  137. if (isWinIE)
  138. LODOP.setAttribute("classid", "clsid:2105C259-1E0C-4534-8141-A753534CB4CA");
  139. else
  140. LODOP.setAttribute("type", "application/x-print-lodop");
  141. document.documentElement.appendChild(LODOP);
  142. CreatedOKLodopObject = LODOP;
  143. } else
  144. LODOP = CreatedOKLodopObject;
  145. if ((!LODOP) || (!LODOP.VERSION)) {
  146. document.body.innerHTML = (isWinIE64 ? strLodop64Install : strLodopInstall) + strInstallOK + document.body.innerHTML;
  147. return LODOP;
  148. }
  149. if (LODOP.VERSION < "6.2.2.6") {
  150. document.body.innerHTML = (isWinIE64 ? strLodop64Update : strLodopUpdate) + strInstallOK + document.body.innerHTML;
  151. }
  152. }
  153. return LODOP;
  154. } catch (err) {
  155. console.error("getLodop出错:", err);
  156. return {
  157. SET_LICENSES: function(){},
  158. PRINT_INIT: function(){},
  159. SET_PRINT_STYLE: function(){},
  160. SET_PRINT_PAGESIZE: function(){},
  161. ADD_PRINT_HTM: function(){},
  162. PREVIEW: function(){},
  163. PRINT: function(){}
  164. };
  165. }
  166. }