GreasyFork: download script button

If you have a script manager and you want to download some script without installing it, this script will help

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

  1. // ==UserScript==
  2. // @name GreasyFork: download script button
  3. // @description If you have a script manager and you want to download some script without installing it, this script will help
  4. // @author Konf
  5. // @version 2.0.0
  6. // @namespace https://greasyfork.org/users/424058
  7. // @icon https://greasyfork.org/vite/assets/blacklogo96-e0c2c761.png
  8. // @match https://greasyfork.org/*/scripts/*
  9. // @match https://sleazyfork.org/*/scripts/*
  10. // @compatible Chrome
  11. // @compatible Opera
  12. // @compatible Firefox
  13. // @run-at document-end
  14. // @grant GM_addStyle
  15. // @noframes
  16. // ==/UserScript==
  17.  
  18. /* jshint esversion: 8 */
  19.  
  20. (function() {
  21. 'use strict';
  22.  
  23. const i18n = {
  24. download: 'download',
  25. downloadWithoutInstalling: 'downloadWithoutInstalling',
  26. failedToDownload: 'failedToDownload',
  27. };
  28.  
  29. const translate = (function() {
  30. const userLang = location.pathname.split('/')[1];
  31. const strings = {
  32. 'en': {
  33. [i18n.download]: 'Download ⇩',
  34. [i18n.downloadWithoutInstalling]: 'Download without installing',
  35. [i18n.failedToDownload]:
  36. 'Failed to download the script. There is might be more info in the browser console'
  37. },
  38. 'ru': {
  39. [i18n.download]: 'Скачать ⇩',
  40. [i18n.downloadWithoutInstalling]: 'Скачать не устанавливая',
  41. [i18n.failedToDownload]:
  42. 'Не удалось скачать скрипт. Больше информации может быть в консоли браузера'
  43. },
  44. 'zh-CN': {
  45. [i18n.download]: '下载 ⇩',
  46. [i18n.downloadWithoutInstalling]: '下载此脚本',
  47. [i18n.failedToDownload]: '无法下载此脚本'
  48. },
  49. };
  50.  
  51. return id => (strings[userLang] || strings.en)[id] || strings.en[id];
  52. }());
  53.  
  54. const installBtn = document.querySelector('a.install-link');
  55. const installArea = document.querySelector('div#install-area');
  56. const installHelpLink = document.querySelector('a.install-help-link');
  57. const suggestions = document.querySelector('div#script-feedback-suggestion');
  58. const libraryRequire = document.querySelector('div#script-content > p > code');
  59.  
  60. // if a script is detected
  61. if (installBtn && installArea && installHelpLink) {
  62. mountScriptDownloadButton(installBtn, installArea, installHelpLink);
  63. } else if (suggestions && libraryRequire) { // or maybe a library
  64. mountLibraryDownloadButton(suggestions, libraryRequire);
  65. }
  66.  
  67. function mountScriptDownloadButton(
  68. installBtn,
  69. installArea,
  70. installHelpLink,
  71. ) {
  72. if (!installBtn.href) throw new Error('script href is not found');
  73.  
  74. // https://img.icons8.com/pastel-glyph/64/ffffff/download.png
  75. // array to fold the string in a code editor
  76. const downloadIconBase64 = [
  77. 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaX',
  78. 'HeAAAABmJLR0QA/wD/AP+gvaeTAAABgUlEQVR4nO3ZTU6DUAAE4HnEk+jWG3TrHV',
  79. 'wY3XoEt23cGleamtRtTbyPS3sCV0bXjptHRAIEsM/hZ76kCZRHGaZAGwDMzMzMbJ',
  80. '6CasMkMwBncXYbQvhSZZEgecEf56ocmWrDAA4L00eqEMoCBsEFqAOouQB1ADUXoA',
  81. '6g5gLUAdRcgDqAmgtQB1BzAeoAakkLIHlN8pPkDcnWd59IBpK3cd1VyoxJkfwo3P',
  82. 'V5KJZAcllYtiy8H+LY3HvKjKlPgU1h+hLAuulIiMvWcWzVZ4xL/Dbv+Nsjyax8BM',
  83. 'Sx96Wxm3jzdLwaSliVCpjezucqzmuSfKuZJkvXi0moORKqTOebL2tRwnR3PtdQwv',
  84. 'R3PldRgmznlc8GA4DTOPscQqAqy6x1+X8+6Ke5yfNxIE9z6/TN1+XCM4inuQ165Z',
  85. 'vHz04DF6AOoOYC1AHUXIA6gNpBz/UWJK/2muTvFn1W6lvASXyNXpdTYJcsxf69th',
  86. '3Y5QjYAiCA485x/tcLgCd1CDMzMzMbum8+xtkWw6QCvwAAAABJRU5ErkJggg==',
  87. ].join('');
  88.  
  89. GM_addStyle([`
  90. .GF-DSB__script-download-button {
  91. position: relative;
  92. padding: 0.5em 1.38em;
  93. cursor: pointer;
  94. border: none;
  95. background: #0F750F;
  96. transition: box-shadow 0.2s;
  97. }
  98.  
  99. .GF-DSB__script-download-button:hover,
  100. .GF-DSB__script-download-button:focus {
  101. box-shadow: 0 8px 16px 0 rgb(0 0 0 / 20%), 0 6px 20px 0 rgb(0 0 0 / 19%);
  102. }
  103.  
  104.  
  105. .GF-DSB__script-download-icon {
  106. position: absolute;
  107. }
  108.  
  109. .GF-DSB__script-download-icon--download {
  110. width: 30px;
  111. height: 30px;
  112. top: 4px;
  113. left: 7px;
  114. }
  115.  
  116. .GF-DSB__script-download-icon--loading,
  117. .GF-DSB__script-download-icon--loading:after {
  118. border-radius: 50%;
  119. width: 16px;
  120. height: 16px;
  121. }
  122.  
  123. .GF-DSB__script-download-icon--loading {
  124. top: 8px;
  125. left: 11px;
  126. border-top: 3px solid rgba(255, 255, 255, 0.2);
  127. border-right: 3px solid rgba(255, 255, 255, 0.2);
  128. border-bottom: 3px solid rgba(255, 255, 255, 0.2);
  129. border-left: 3px solid #ffffff;
  130. transform: translateZ(0);
  131. object-position: -99999px;
  132. animation: GF-DSB__script-download-loading-icon 1.1s infinite linear;
  133. }
  134.  
  135. @keyframes GF-DSB__script-download-loading-icon {
  136. 0% {
  137. transform: rotate(0deg);
  138. }
  139. 100% {
  140. transform: rotate(360deg);
  141. }
  142. }
  143. `][0]);
  144.  
  145. const b = document.createElement('a');
  146. const bIcon = document.createElement('img');
  147.  
  148. b.href = '#';
  149. b.title = translate(i18n.downloadWithoutInstalling);
  150. b.draggable = false;
  151. b.className = 'GF-DSB__script-download-button';
  152.  
  153. bIcon.src = downloadIconBase64;
  154. bIcon.draggable = false;
  155. bIcon.className =
  156. 'GF-DSB__script-download-icon GF-DSB__script-download-icon--download';
  157.  
  158. installHelpLink.style.position = 'relative'; // shadows bugfix
  159.  
  160. b.appendChild(bIcon);
  161. installArea.insertBefore(b, installHelpLink);
  162.  
  163. b.addEventListener('click', clicksHandler);
  164. b.addEventListener('auxclick', e => e.button === 1 && clicksHandler(e));
  165.  
  166. // against doubleclicks
  167. let isFetchingAllowed = true;
  168.  
  169. async function clicksHandler(ev) {
  170. ev.preventDefault();
  171.  
  172. setTimeout(() => b === document.activeElement && b.blur(), 250);
  173.  
  174. if (isFetchingAllowed === false) return;
  175.  
  176. isFetchingAllowed = false;
  177. bIcon.className =
  178. 'GF-DSB__script-download-icon GF-DSB__script-download-icon--loading';
  179.  
  180. try {
  181. await downloadScript({
  182. href: installBtn.href,
  183. name: installBtn.dataset.scriptName,
  184. });
  185. } catch (e) {
  186. console.error(e);
  187. alert(`${translate(i18n.failedToDownload)}: \n${e}`);
  188. } finally {
  189. setTimeout(() => {
  190. isFetchingAllowed = true;
  191. bIcon.className =
  192. 'GF-DSB__script-download-icon GF-DSB__script-download-icon--download';
  193. }, 300);
  194. }
  195. }
  196. }
  197.  
  198. function mountLibraryDownloadButton(suggestions, libraryRequire) {
  199. const [
  200. libraryHref,
  201. libraryName,
  202. ] = libraryRequire.innerText.match(
  203. /\/\/ @require (https:\/\/.+\/scripts\/\d+.+\/code\/(.*)\.js\?version=\d+)/
  204. ).slice(1);
  205.  
  206. // this probably is completely useless but whatever
  207. if (!libraryHref) throw new Error('library href is not found');
  208.  
  209. GM_addStyle([`
  210. .GF-DSB__library-download-button {
  211. transition: box-shadow 0.2s;
  212. }
  213.  
  214. .GF-DSB__library-download-button--loading {
  215. animation: GF-DSB__loading-text 1s infinite linear;
  216. }
  217.  
  218. @keyframes GF-DSB__loading-text {
  219. 50% {
  220. opacity: 0.4;
  221. }
  222. }
  223. `][0]);
  224.  
  225. const b = document.createElement('a');
  226.  
  227. b.href = '#';
  228. b.draggable = false;
  229. b.innerText = translate(i18n.download);
  230. b.className = 'GF-DSB__library-download-button';
  231.  
  232. suggestions.appendChild(b);
  233.  
  234. b.addEventListener('click', clicksHandler);
  235. b.addEventListener('auxclick', e => e.button === 1 && clicksHandler(e));
  236.  
  237. // against doubleclicks
  238. let isFetchingAllowed = true;
  239.  
  240. async function clicksHandler(ev) {
  241. ev.preventDefault();
  242.  
  243. setTimeout(() => b === document.activeElement && b.blur(), 250);
  244.  
  245. if (isFetchingAllowed === false) return;
  246.  
  247. isFetchingAllowed = false;
  248. b.className =
  249. 'GF-DSB__library-download-button GF-DSB__library-download-button--loading';
  250.  
  251. try {
  252. await downloadScript({
  253. href: libraryHref,
  254. name: libraryName,
  255. isLibrary: true,
  256. });
  257. } catch (e) {
  258. console.error(e);
  259. alert(`${translate(i18n.failedToDownload)}: \n${e}`);
  260. } finally {
  261. setTimeout(() => {
  262. isFetchingAllowed = true;
  263. b.className = 'GF-DSB__library-download-button';
  264. }, 300);
  265. }
  266. }
  267. }
  268.  
  269. // utils --------------------------------------------------------------------
  270.  
  271. async function downloadScript({
  272. href,
  273. name,
  274. isLibrary = false,
  275. } = {}) {
  276. if (!href) throw new Error('href is missing');
  277.  
  278. const blob = await (await fetch(href)).blob();
  279. const url = window.URL.createObjectURL(blob);
  280. const a = document.createElement('a');
  281.  
  282. a.href = url;
  283. a.download = `${name || Date.now()}${!isLibrary ? '.user' : ''}.js`;
  284. document.body.appendChild(a); // is needed due to firefox bug
  285. a.click();
  286. a.remove();
  287.  
  288. window.URL.revokeObjectURL(url);
  289. }
  290. }());