Worldcosplay download

Download photo(s) on worldcosplay.net

当前为 2018-05-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Worldcosplay download
  3. // @namespace http://devs.forumvi.com/
  4. // @description Download photo(s) on worldcosplay.net
  5. // @version 3.1.1
  6. // @icon http://i.imgur.com/gJLjIzb.png
  7. // @author Zzbaivong
  8. // @oujs:author baivong
  9. // @license MIT; https://baivong.mit-license.org/license.txt
  10. // @match https://worldcosplay.net/*/photos*
  11. // @match https://worldcosplay.net/*/favorites*
  12. // @match https://worldcosplay.net/photo/*
  13. // @match https://worldcosplay.net/tag/*
  14. // @match https://worldcosplay.net/search/photos?*
  15. // @match https://worldcosplay.net/collections/*
  16. // @match https://worldcosplay.net/character/*
  17. // @match https://worldcosplay.net/title/*
  18. // @match https://worldcosplay.net/photos
  19. // @match https://worldcosplay.net/popular
  20. // @match https://worldcosplay.net/ranking/good*
  21. // @match https://worldcosplay.net/*/photo/*
  22. // @match https://worldcosplay.net/*/tag/*
  23. // @match https://worldcosplay.net/*/search/photos?*
  24. // @match https://worldcosplay.net/*/collections/*
  25. // @match https://worldcosplay.net/*/character/*
  26. // @match https://worldcosplay.net/*/title/*
  27. // @match https://worldcosplay.net/*/photos
  28. // @match https://worldcosplay.net/*/popular
  29. // @match https://worldcosplay.net/*/ranking/good*
  30. // @require https://code.jquery.com/jquery-3.3.1.slim.min.js
  31. // @require https://unpkg.com/file-saver@1.3.8/FileSaver.min.js
  32. // @require https://greasyfork.org/scripts/6250-waitforkeyelements/code/waitForKeyElements.js?version=23756
  33. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?v=a834d46
  34. // @noframes
  35. // @connect self
  36. // @connect sakurastorage.jp
  37. // @supportURL https://github.com/lelinhtinh/Userscript/issues
  38. // @run-at document-idle
  39. // @grant GM.xmlHttpRequest
  40. // @grant GM.openInTab
  41. // ==/UserScript==
  42.  
  43. /* global waitForKeyElements */
  44. (function ($, window) {
  45. 'use strict';
  46.  
  47. window.URL = window.URL || window.webkitURL;
  48.  
  49. function downloadPhoto(el, url) {
  50. var photoName = url.replace(/.*\//g, ''),
  51. $icon = $(el).find('.fa');
  52.  
  53. $icon.addClass('fa-spinner fa-spin');
  54.  
  55. GM.xmlHttpRequest({
  56. method: 'GET',
  57. url: url,
  58. responseType: 'blob',
  59. onload: function (response) {
  60. var blob = response.response;
  61.  
  62. $(el).attr({
  63. href: window.URL.createObjectURL(blob),
  64. download: photoName
  65. }).off('click');
  66. $icon.removeClass('fa-spinner fa-spin').addClass('fa-download');
  67.  
  68. saveAs(blob, photoName);
  69. },
  70. onerror: function (err) {
  71. $icon.removeClass('fa-spinner fa-spin').addClass('fa-times');
  72. console.error(err);
  73. }
  74. });
  75. }
  76.  
  77. function getImage3000(url) {
  78. var hasMax = url.match(/\/max-(\d+)\//);
  79. if (hasMax) return url.replace(/-[\dx]+\./, '-' + hasMax[1] + '.');
  80.  
  81. return url.replace(/-[\dx]+\./, '-3000.');
  82. }
  83.  
  84. if (/^(\/[a-z-]+)?\/photo\/\d+$/.test(location.pathname)) {
  85.  
  86. var $btn = $('<a>', {
  87. href: '#download',
  88. class: 'download-this-photo',
  89. title: 'Click to download this image\nRight Click to open in new tab',
  90. html: '<div class="side_buttons" style="right: 250px;"><div class="like-this-photo button fave fa fa-download"><div class="effect-ripple"></div></div></div>'
  91. }),
  92. img = $('#photoContainer').find('.img').attr('src');
  93. $btn.on('click', function (e) {
  94. e.preventDefault();
  95. e.stopPropagation();
  96. downloadPhoto(this, getImage3000(img));
  97. }).on('contextmenu', function (e) {
  98. e.preventDefault();
  99. e.stopPropagation();
  100. GM.openInTab(getImage3000(img));
  101. });
  102. $btn.insertAfter('.side_buttons');
  103.  
  104. } else {
  105.  
  106. var addBtn = function () {
  107. $('.preview').not('.added-download-btn').each(function () {
  108. var $this = $(this),
  109. $btn = $('<a>', {
  110. href: '#download',
  111. class: 'download-this-photo',
  112. title: 'Click to download this image\nRight Click to open in new tab',
  113. html: '<div class="item likes" style="top: 50px;"><span class="like-this-photo"><i class="fa fa-download"></i><span class="effect-ripple"></span></span></div>'
  114. });
  115. $btn.on('click', function (e) {
  116. e.preventDefault();
  117. e.stopPropagation();
  118. downloadPhoto(this, getImage3000($this.find('.photo_img').css('backgroundImage').slice(5, -2)));
  119. }).on('contextmenu', function (e) {
  120. e.preventDefault();
  121. e.stopPropagation();
  122. GM.openInTab(getImage3000($this.find('.photo_img').css('backgroundImage').slice(5, -2)));
  123. });
  124. $this.find('.options').append($btn);
  125. $this.addClass('added-download-btn');
  126. });
  127. };
  128. addBtn();
  129.  
  130. waitForKeyElements('.preview', addBtn);
  131.  
  132. }
  133.  
  134. })(jQuery, window);