Worldcosplay download

Download photo(s) on worldcosplay.net

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

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