1
0

catchremoteimage.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ///import core
  2. ///commands 远程图片抓取
  3. ///commandsName catchRemoteImage,catchremoteimageenable
  4. ///commandsTitle 远程图片抓取
  5. /**
  6. * 远程图片抓取,当开启本插件时所有不符合本地域名的图片都将被抓取成为本地服务器上的图片
  7. */
  8. UE.plugins["catchremoteimage"] = function() {
  9. var me = this,
  10. ajax = UE.ajax;
  11. /* 设置默认值 */
  12. if (me.options.catchRemoteImageEnable === false) return;
  13. me.setOpt({
  14. catchRemoteImageEnable: false
  15. });
  16. me.addListener("afterpaste", function() {
  17. me.fireEvent("catchRemoteImage");
  18. });
  19. me.addListener("catchRemoteImage", function() {
  20. var catcherLocalDomain = me.getOpt("catcherLocalDomain"),
  21. catcherActionUrl = me.getActionUrl(me.getOpt("catcherActionName")),
  22. catcherUrlPrefix = me.getOpt("catcherUrlPrefix"),
  23. catcherFieldName = me.getOpt("catcherFieldName");
  24. var remoteImages = [],
  25. loadingIMG = me.options.themePath + me.options.theme + '/images/spacer.gif',
  26. imgs = me.document.querySelectorAll('[style*="url"],img'),
  27. test = function(src, urls) {
  28. if (src.indexOf(location.host) != -1 || /(^\.)|(^\/)/.test(src)) {
  29. return true;
  30. }
  31. if (urls) {
  32. for (var j = 0, url; (url = urls[j++]); ) {
  33. if (src.indexOf(url) !== -1) {
  34. return true;
  35. }
  36. }
  37. }
  38. return false;
  39. };
  40. for (var i = 0, ci; (ci = imgs[i++]); ) {
  41. if (ci.getAttribute("word_img")) {
  42. continue;
  43. }
  44. if(ci.nodeName == "IMG"){
  45. var src = ci.getAttribute("_src") || ci.src || "";
  46. if (/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {
  47. remoteImages.push(src);
  48. // 添加上传时的uploading动画
  49. domUtils.setAttributes(ci, {
  50. class: "loadingclass",
  51. _src: src,
  52. src: loadingIMG
  53. })
  54. }
  55. } else {
  56. // 获取背景图片url
  57. var backgroundImageurl = ci.style.cssText.replace(/.*\s?url\([\'\"]?/, '').replace(/[\'\"]?\).*/, '');
  58. if (/^(https?|ftp):/i.test(backgroundImageurl) && !test(backgroundImageurl, catcherLocalDomain)) {
  59. remoteImages.push(backgroundImageurl);
  60. ci.style.cssText = ci.style.cssText.replace(backgroundImageurl, loadingIMG);
  61. domUtils.setAttributes(ci, {
  62. "data-background": backgroundImageurl
  63. })
  64. }
  65. }
  66. }
  67. if (remoteImages.length) {
  68. catchremoteimage(remoteImages, {
  69. //成功抓取
  70. success: function(r) {
  71. try {
  72. var info = r.state !== undefined
  73. ? r
  74. : eval("(" + r.responseText + ")");
  75. } catch (e) {
  76. return;
  77. }
  78. /* 获取源路径和新路径 */
  79. var i,
  80. j,
  81. ci,
  82. cj,
  83. oldSrc,
  84. newSrc,
  85. list = info.list;
  86. /* 抓取失败统计 */
  87. var catchFailList = [];
  88. /* 抓取成功统计 */
  89. var catchSuccessList = [];
  90. /* 抓取失败时显示的图片 */
  91. var failIMG = me.options.themePath + me.options.theme + '/images/img-cracked.png';
  92. for (i = 0; ci = imgs[i++];) {
  93. oldSrc = ci.getAttribute("_src") || ci.src || "";
  94. oldBgIMG = ci.getAttribute("data-background") || "";
  95. for (j = 0; cj = list[j++];) {
  96. if (oldSrc == cj.source && cj.state == "SUCCESS") {
  97. newSrc = catcherUrlPrefix + cj.url;
  98. // 上传成功是删除uploading动画
  99. domUtils.removeClasses( ci, "loadingclass" );
  100. domUtils.setAttributes(ci, {
  101. "src": newSrc,
  102. "_src": newSrc,
  103. "data-catchResult":"img_catchSuccess" // 添加catch成功标记
  104. });
  105. catchSuccessList.push(ci);
  106. break;
  107. } else if (oldSrc == cj.source && cj.state == "FAIL") {
  108. // 替换成统一的失败图片
  109. domUtils.removeClasses( ci, "loadingclass" );
  110. domUtils.setAttributes(ci, {
  111. "src": failIMG,
  112. "_src": failIMG,
  113. "data-catchResult":"img_catchFail" // 添加catch失败标记
  114. });
  115. catchFailList.push(ci);
  116. break;
  117. } else if (oldBgIMG == cj.source && cj.state == "SUCCESS") {
  118. newBgIMG = catcherUrlPrefix + cj.url;
  119. ci.style.cssText = ci.style.cssText.replace(loadingIMG, newBgIMG);
  120. domUtils.removeAttributes(ci,"data-background");
  121. domUtils.setAttributes(ci, {
  122. "data-catchResult":"img_catchSuccess" // 添加catch成功标记
  123. });
  124. catchSuccessList.push(ci);
  125. break;
  126. } else if (oldBgIMG == cj.source && cj.state == "FAIL"){
  127. ci.style.cssText = ci.style.cssText.replace(loadingIMG, failIMG);
  128. domUtils.removeAttributes(ci,"data-background");
  129. domUtils.setAttributes(ci, {
  130. "data-catchResult":"img_catchFail" // 添加catch失败标记
  131. });
  132. catchFailList.push(ci);
  133. break;
  134. }
  135. }
  136. }
  137. // 监听事件添加成功抓取和抓取失败的dom列表参数
  138. me.fireEvent('catchremotesuccess',catchSuccessList,catchFailList);
  139. },
  140. //回调失败,本次请求超时
  141. error: function() {
  142. me.fireEvent("catchremoteerror");
  143. }
  144. });
  145. }
  146. function catchremoteimage(imgs, callbacks) {
  147. var params =
  148. utils.serializeParam(me.queryCommandValue("serverparam")) || "",
  149. url = utils.formatUrl(
  150. catcherActionUrl +
  151. (catcherActionUrl.indexOf("?") == -1 ? "?" : "&") +
  152. params
  153. ),
  154. isJsonp = utils.isCrossDomainUrl(url),
  155. opt = {
  156. method: "POST",
  157. dataType: isJsonp ? "jsonp" : "",
  158. timeout: 60000, //单位:毫秒,回调请求超时设置。目标用户如果网速不是很快的话此处建议设置一个较大的数值
  159. onsuccess: callbacks["success"],
  160. onerror: callbacks["error"]
  161. };
  162. opt[catcherFieldName] = imgs;
  163. ajax.request(url, opt);
  164. }
  165. });
  166. };