站酷网下载按钮

提供站酷网原图下载功能

  1. // ==UserScript==
  2. // @name 站酷网下载按钮
  3. // @namespace zhanku-download
  4. // @version 1.0.2
  5. // @icon https://static.zcool.cn/git_z/z/site/favicon.ico?version=1618914637608
  6. // @description 提供站酷网原图下载功能
  7. // @author sertraline
  8. // @match http*://www.zcool.com.cn/work/*
  9. // @require https://cdn.staticfile.org/jquery/3.6.2/jquery.min.js
  10. // @grant GM_openInTab
  11. // @grant GM_download
  12. // ==/UserScript==
  13. const IMG_SELECTOR = '.photoImage'
  14. const IMG_BOX_SELECTOR = '.photoInformationContent'
  15. const DOWNLOAD_BOX_SELECTOR = '.imageCollectIcons'
  16. const DOWNLOAD_ALL_BOX_SELECTOR = '.detailNavBox'
  17. const INDEX_MODAL_SELECTOR = '.ReactModalPortal'
  18. const NAMESPACE = GM_info.script.namespace
  19. const DOWNLOAD_ICON = `<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M229.4 452.7l258.5 195.1a39.9 39.9 0 0 0 48.2 0l258.5-195.1c12.2-9.2 5.7-28.7-9.6-28.7H612.9V168a40 40 0 0 0-40-40H451.1a40 40 0 0 0-40 40v256H239c-15.3 0-21.8 19.5-9.6 28.7zM856 656H619.6a4.4 4.4 0 0 0-2.5.8l-47.3 35.7a95.8 95.8 0 0 1-115.6 0l-47.3-35.7a4.4 4.4 0 0 0-2.5-.8H168a40 40 0 0 0-40 40v160a40 40 0 0 0 40 40h688a40 40 0 0 0 40-40V696a40 40 0 0 0-40-40zM688 808a32 32 0 1 1 32-32 32 32 0 0 1-32 32zm120 0a32 32 0 1 1 32-32 32 32 0 0 1-32 32z"/></svg>`
  20. function uuid() {
  21. return Math.random().toString(36).substr(2)
  22. }
  23. function namespace(str) {
  24. return `${NAMESPACE}-${str}`
  25. }
  26. let toasts = [];
  27. let $toastContainer;
  28. const TOAST_CONTAINER_SELECTOR = `#${namespace("toast")}`;
  29. function initToast() {
  30. if (!$toastContainer) {
  31. $toastContainer = $(TOAST_CONTAINER_SELECTOR);
  32. if (!$toastContainer.length) {
  33. const toast = `
  34. <div id="${namespace("toast")}" class="${namespace("toast")}">
  35. </div>
  36. `;
  37. $toastContainer = $(toast);
  38. $("body").append($toastContainer);
  39. }
  40. }
  41. }
  42.  
  43. function createBtn({ id, text, callback, parent = "body", cls = "btns-item" }) {
  44. const _id = namespace(`${uuid()}-${id}`)
  45. if (document.getElementById(_id)) return
  46. const btn = `<div id="${_id}" class="${namespace(cls)}">${text}</div>`
  47. $(parent).append(btn)
  48. $(`#${_id}`).click(callback)
  49. }
  50.  
  51. function openToast(text, duration = 2000) {
  52. if (!text) return;
  53.  
  54. initToast();
  55. const id = namespace(uuid + "toast")
  56. const toast = {
  57. id,
  58. element: $.parseHTML(`
  59. <div id="${id}" class="${namespace("toast__item")}">
  60. ${text}
  61. </div>
  62. `),
  63. };
  64. toasts.push(toast);
  65.  
  66. $toastContainer.append(toast.element);
  67. setTimeout(() => {
  68. $(toast.element).remove();
  69. toasts = toasts.filter((t) => t.id !== toast.id);
  70. }, duration);
  71. }
  72.  
  73. function getNameFromUrl(url) {
  74. const SPLIT_REGEXP = /\/|\?/
  75. return new URL(url).pathname.split(SPLIT_REGEXP).pop()
  76. }
  77. function getFileTypeFromUrl(url) {
  78. const name = getNameFromUrl(url)
  79. const SPLIT_REGEXP = /\./
  80. const res = name.split(SPLIT_REGEXP)[1]
  81. return res ? res.toUpperCase() : null
  82. }
  83. function downloadFile(url) {
  84. const name = getNameFromUrl(url)
  85. GM_download({
  86. url,
  87. name,
  88. saveAs: true,
  89. onload: () => {
  90. openToast('下载成功')
  91. },
  92. onerror: () => {
  93. openToast('下载失败')
  94. }
  95. })
  96. }
  97. function main() {
  98. const sourceImgEls = document.querySelectorAll(IMG_BOX_SELECTOR)
  99. sourceImgEls.forEach(el => {
  100. const src = el.querySelector(IMG_SELECTOR).src
  101. const _src = src.split('?')[0]
  102. createBtn({
  103. id: 'download',
  104. text: `${DOWNLOAD_ICON}下载原图`,
  105. callback() {
  106. downloadFile(_src)
  107. },
  108. parent: el.querySelector(DOWNLOAD_BOX_SELECTOR)
  109.  
  110. })
  111. })
  112. createBtn({
  113. id: 'download-all',
  114. text: DOWNLOAD_ICON,
  115. callback() {
  116. const imgs = Array.from(sourceImgEls).map(el => el.querySelector(IMG_SELECTOR).src.split('?')[0])
  117. openToast(`开始下载${imgs.length}张图片`)
  118. imgs.forEach(img => {
  119. downloadFile(img)
  120. })
  121. },
  122. parent: document.querySelector(DOWNLOAD_ALL_BOX_SELECTOR),
  123. cls: 'download-all-btns-item'
  124. })
  125. }
  126. //
  127. ; (function ($) {
  128. initStyles()
  129. main()
  130. })(jQuery)
  131. function initStyles() {
  132. const styles = `
  133. <style>
  134. .imageCollectIcons{
  135. width:auto !important;
  136. }
  137. .${namespace('toast')}{
  138. position: fixed;
  139. top: 50px;
  140. left: 50%;
  141. transform: translateX(-50%);
  142. display: flex;
  143. flex-direction: column;
  144. align-items: center;
  145. z-index: 99999999;
  146. }
  147. .${namespace('toast__item')}{
  148. padding: 10px 20px;
  149. background: rgba(0,0,0,0.5);
  150. color: #fff;
  151. border-radius: 5px;
  152. margin-bottom: 10px;
  153. }
  154. .${namespace('btns-item')} {
  155. margin-left: 4px;
  156. padding: 0 10px;
  157. line-height: 36px;
  158. font-size: 14px;
  159. height: 36px;
  160. border-radius: 4px;
  161. background-color: rgba(0, 0, 0, 0.6);
  162. display: flex;
  163. align-items: center;
  164. color: #fff;
  165. }
  166. .${namespace('btns-item')}:hover {
  167. color: rgb(255, 242, 0);
  168. }
  169. .${namespace('btns-item')} svg {
  170. margin-right: 4px;
  171. height: 20px;
  172. fill: #fff;
  173. }
  174. .${namespace('btns-item')}:hover svg{
  175. fill: rgb(255, 242, 0);
  176. }
  177. .${namespace('download-all-btns-item')} {
  178. width: 50px;
  179. height: 50px;
  180. display: inline-flex;
  181. align-items: center;
  182. justify-content: center;
  183. border-radius: 50%;
  184. box-sizing: border-box;
  185. position: relative;
  186. cursor: pointer;
  187. margin-top: 16px;
  188. border: 1px solid rgb(237, 237, 237);
  189. background-color: rgba(255, 255, 255, 0.9);
  190. }
  191. .${namespace('download-all-btns-item')} svg{
  192. height: 24px;
  193. }
  194. </style>
  195. `
  196. $('head').append(styles)
  197. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址