Worldcosplay download

Download photo(s) on worldcosplay.net

当前为 2016-04-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @id worldcosplay-download@devs.forumvi.com
  3. // @name Worldcosplay download
  4. // @namespace http://devs.forumvi.com/
  5. // @description Download photo(s) on worldcosplay.net
  6. // @version 1.1.4
  7. // @icon http://i.imgur.com/gJLjIzb.png
  8. // @author Zzbaivong
  9. // @license MIT
  10. // @match http://worldcosplay.net/photo/*
  11. // @match http://worldcosplay.net/member/*/photos*
  12. // @match http://worldcosplay.net/member/*/favorites*
  13. // @match http://worldcosplay.net/tag/*
  14. // @match http://worldcosplay.net/search/photos?*
  15. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js
  16. // @require https://greasyfork.org/scripts/18532-filesaver/code/FileSaver.js?version=117790
  17. // @require https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
  18. // @noframes
  19. // @connect worldcosplay.net
  20. // @supportURL https://github.com/baivong/Userscript/issues
  21. // @run-at document-idle
  22. // @grant GM_xmlhttpRequest
  23. // ==/UserScript==
  24.  
  25. (function($, window, document, undefined) {
  26. 'use strict';
  27.  
  28. window.URL = window.URL || window.webkitURL;
  29.  
  30. function downloadPhoto(el, url) {
  31. var photoName = url.replace(/.*\//g, '');
  32. GM_xmlhttpRequest({
  33. method: 'GET',
  34. url: url,
  35. responseType: 'blob',
  36. onload: function(response) {
  37. var blob = response.response;
  38. $(el).attr({
  39. href: window.URL.createObjectURL(blob),
  40. download: photoName
  41. }).removeAttr('onclick');
  42. saveAs(blob, photoName);
  43. },
  44. onerror: function(err) {
  45. console.error(err);
  46. }
  47. });
  48. }
  49.  
  50. if (!location.pathname.indexOf('/photo/')) {
  51.  
  52. var $btn = $('<a>', {
  53. href: '#download',
  54. 'class': 'download-this-photo',
  55. html: '<div class="side_buttons" style="right: 220px;"><div class="like-this-photo button fave fa fa-download"><div class="effect-ripple"></div></div></div>'
  56. });
  57. $btn.on('click', function(e) {
  58. e.preventDefault();
  59. e.stopPropagation();
  60. downloadPhoto(this, $('#photoContainer').find('.img').attr('src'));
  61. });
  62. $btn.insertAfter('.side_buttons');
  63.  
  64. } else {
  65.  
  66. var addBtn = function() {
  67. $('.preview').not('.added-download-btn').each(function() {
  68. var $this = $(this),
  69. $btn = $('<a>', {
  70. href: '#download',
  71. 'class': 'download-this-photo',
  72. 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>'
  73. });
  74. $btn.on('click', function(e) {
  75. e.preventDefault();
  76. e.stopPropagation();
  77. downloadPhoto(this, $this.find('.photo_img').css('backgroundImage').slice(5, -2).replace('sq300/', ''));
  78. });
  79. $this.find('.options').append($btn);
  80. $this.addClass('added-download-btn');
  81. });
  82. };
  83. addBtn();
  84.  
  85. waitForKeyElements('.preview', addBtn);
  86.  
  87. }
  88.  
  89. })(jQuery, window, document);