Worldcosplay download

Download photo(s) on worldcosplay.net

当前为 2017-07-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Worldcosplay download
  3. // @namespace http://devs.forumvi.com/
  4. // @description Download photo(s) on worldcosplay.net
  5. // @version 2.2.1
  6. // @icon http://i.imgur.com/gJLjIzb.png
  7. // @author Zzbaivong
  8. // @license MIT
  9. // @match http://worldcosplay.net/*/photos*
  10. // @match http://worldcosplay.net/*/favorites*
  11. // @match http://worldcosplay.net/photo/*
  12. // @match http://worldcosplay.net/tag/*
  13. // @match http://worldcosplay.net/search/photos?*
  14. // @match http://worldcosplay.net/collections/*
  15. // @match http://worldcosplay.net/character/*
  16. // @match http://worldcosplay.net/title/*
  17. // @match http://worldcosplay.net/photos
  18. // @match http://worldcosplay.net/popular
  19. // @match http://worldcosplay.net/ranking/good*
  20. // @match http://worldcosplay.net/*/photo/*
  21. // @match http://worldcosplay.net/*/tag/*
  22. // @match http://worldcosplay.net/*/search/photos?*
  23. // @match http://worldcosplay.net/*/collections/*
  24. // @match http://worldcosplay.net/*/character/*
  25. // @match http://worldcosplay.net/*/title/*
  26. // @match http://worldcosplay.net/*/photos
  27. // @match http://worldcosplay.net/*/popular
  28. // @match http://worldcosplay.net/*/ranking/good*
  29. // @match https://worldcosplay.net/*/photos*
  30. // @match https://worldcosplay.net/*/favorites*
  31. // @match https://worldcosplay.net/photo/*
  32. // @match https://worldcosplay.net/tag/*
  33. // @match https://worldcosplay.net/search/photos?*
  34. // @match https://worldcosplay.net/collections/*
  35. // @match https://worldcosplay.net/character/*
  36. // @match https://worldcosplay.net/title/*
  37. // @match https://worldcosplay.net/photos
  38. // @match https://worldcosplay.net/popular
  39. // @match https://worldcosplay.net/ranking/good*
  40. // @match https://worldcosplay.net/*/photo/*
  41. // @match https://worldcosplay.net/*/tag/*
  42. // @match https://worldcosplay.net/*/search/photos?*
  43. // @match https://worldcosplay.net/*/collections/*
  44. // @match https://worldcosplay.net/*/character/*
  45. // @match https://worldcosplay.net/*/title/*
  46. // @match https://worldcosplay.net/*/photos
  47. // @match https://worldcosplay.net/*/popular
  48. // @match https://worldcosplay.net/*/ranking/good*
  49. // @require https://code.jquery.com/jquery-3.2.1.slim.min.js
  50. // @require https://greasyfork.org/scripts/18532-filesaver/code/FileSaver.js?version=164030
  51. // @require https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
  52. // @noframes
  53. // @connect self
  54. // @supportURL https://github.com/baivong/Userscript/issues
  55. // @run-at document-idle
  56. // @grant GM_xmlhttpRequest
  57. // @grant GM_download
  58. // ==/UserScript==
  59.  
  60. /* global GM_download, waitForKeyElements */
  61. (function ($, window) {
  62. 'use strict';
  63.  
  64. window.URL = window.URL || window.webkitURL;
  65.  
  66. function downloadPhoto(el, url) {
  67. var photoName = url.replace(/.*\//g, '');
  68. if (typeof GM_download !== 'undefined') {
  69. GM_download({
  70. url: url,
  71. name: photoName,
  72. saveAs: false,
  73. onerror: function (err) {
  74. console.error(err);
  75. }
  76. });
  77. } else {
  78. GM_xmlhttpRequest({
  79. method: 'GET',
  80. url: url,
  81. responseType: 'blob',
  82. onload: function (response) {
  83. var blob = response.response;
  84. $(el).attr({
  85. href: window.URL.createObjectURL(blob),
  86. download: photoName
  87. }).off('click');
  88. saveAs(blob, photoName);
  89. },
  90. onerror: function (err) {
  91. console.error(err);
  92. }
  93. });
  94. }
  95. }
  96.  
  97. function getImage3000(url) {
  98. var hasMax = url.match(/\/max\-(\d+)\//);
  99. if (hasMax) return url.replace(/\-[\dx]+\./, '-' + hasMax[1] + '.');
  100.  
  101. return url.replace(/\-[\dx]+\./, '-3000.');
  102. }
  103.  
  104. if (/^(\/[a-z\-]+)?\/photo\/\d+$/.test(location.pathname)) {
  105.  
  106. var $btn = $('<a>', {
  107. href: '#download',
  108. 'class': 'download-this-photo',
  109. 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>'
  110. });
  111. $btn.on('click', function (e) {
  112. e.preventDefault();
  113. e.stopPropagation();
  114. downloadPhoto(this, getImage3000($('#photoContainer').find('.img').attr('src')));
  115. });
  116. $btn.insertAfter('.side_buttons');
  117.  
  118. } else {
  119.  
  120. var addBtn = function () {
  121. $('.preview').not('.added-download-btn').each(function () {
  122. var $this = $(this),
  123. $btn = $('<a>', {
  124. href: '#download',
  125. 'class': 'download-this-photo',
  126. 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>'
  127. });
  128. $btn.on('click', function (e) {
  129. e.preventDefault();
  130. e.stopPropagation();
  131. downloadPhoto(this, getImage3000($this.find('.photo_img').css('backgroundImage').slice(5, -2)));
  132. });
  133. $this.find('.options').append($btn);
  134. $this.addClass('added-download-btn');
  135. });
  136. };
  137. addBtn();
  138.  
  139. waitForKeyElements('.preview', addBtn);
  140.  
  141. }
  142.  
  143. })(jQuery, window);