catchremoteimage.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. imgs = domUtils.getElementsByTagName(me.document, "img"),
  26. test = function (src, urls) {
  27. if (src.indexOf(location.host) != -1 || /(^\.)|(^\/)/.test(src)) {
  28. return true;
  29. }
  30. if (urls) {
  31. for (var j = 0, url; url = urls[j++];) {
  32. if (src.indexOf(url) !== -1) {
  33. return true;
  34. }
  35. }
  36. }
  37. return false;
  38. };
  39. for (var i = 0, ci; ci = imgs[i++];) {
  40. if (ci.getAttribute("word_img")) {
  41. continue;
  42. }
  43. var src = ci.getAttribute("_src") || ci.src || "";
  44. if (/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {
  45. remoteImages.push(src);
  46. }
  47. }
  48. if (remoteImages.length) {
  49. catchremoteimage(remoteImages, {
  50. //成功抓取
  51. success: function (r) {
  52. try {
  53. var info = r.state !== undefined ? r:eval("(" + r.responseText + ")");
  54. } catch (e) {
  55. return;
  56. }
  57. /* 获取源路径和新路径 */
  58. var i, j, ci, cj, oldSrc, newSrc, list = info.list;
  59. for (i = 0; ci = imgs[i++];) {
  60. oldSrc = ci.getAttribute("_src") || ci.src || "";
  61. for (j = 0; cj = list[j++];) {
  62. if (oldSrc == cj.source && cj.state == "SUCCESS") { //抓取失败时不做替换处理
  63. newSrc = catcherUrlPrefix + cj.url;
  64. domUtils.setAttributes(ci, {
  65. "src": newSrc,
  66. "_src": newSrc
  67. });
  68. break;
  69. }
  70. }
  71. }
  72. me.fireEvent('catchremotesuccess')
  73. },
  74. //回调失败,本次请求超时
  75. error: function () {
  76. me.fireEvent("catchremoteerror");
  77. }
  78. });
  79. }
  80. function catchremoteimage(imgs, callbacks) {
  81. var params = utils.serializeParam(me.queryCommandValue('serverparam')) || '',
  82. url = utils.formatUrl(catcherActionUrl + (catcherActionUrl.indexOf('?') == -1 ? '?':'&') + params),
  83. isJsonp = utils.isCrossDomainUrl(url),
  84. opt = {
  85. 'method': 'POST',
  86. 'dataType': isJsonp ? 'jsonp':'',
  87. 'timeout': 60000, //单位:毫秒,回调请求超时设置。目标用户如果网速不是很快的话此处建议设置一个较大的数值
  88. 'onsuccess': callbacks["success"],
  89. 'onerror': callbacks["error"]
  90. };
  91. opt[catcherFieldName] = imgs;
  92. ajax.request(url, opt);
  93. }
  94. });
  95. };