Greasy Fork 增强

增进 Greasyfork 浏览体验。

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

  1. // ==UserScript==
  2. // @name Greasy Fork Enhance
  3. // @name:zh-CN Greasy Fork 增强
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.6.3
  6. // @description Enhance your experience at Greasyfork.
  7. // @description:zh-CN 增进 Greasyfork 浏览体验。
  8. // @author PRO
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @grant GM_deleteValue
  12. // @grant GM_registerMenuCommand
  13. // @grant GM_unregisterMenuCommand
  14. // @match https://greasyfork.org/*
  15. // @require https://greasyfork.org/scripts/470224-tampermonkey-config/code/Tampermonkey%20Config.js?version=1244657
  16. // @icon https://greasyfork.org/vite/assets/blacklogo16-bc64b9f7.png
  17. // @license gpl-3.0
  18. // ==/UserScript==
  19.  
  20. (function() {
  21. 'use strict';
  22. // Judge if the script should run
  23. let no_run = [".json", ".js"];
  24. let is_run = true;
  25. let idPrefix = "greasyfork-enhance-";
  26. no_run.forEach((suffix) => {
  27. if (window.location.pathname.endsWith(suffix)) {
  28. is_run = false;
  29. }
  30. });
  31. if (!is_run) return;
  32. // Config
  33. function _bollDest(name, title = undefined, default_value = true) {
  34. return {
  35. name: name,
  36. value: default_value,
  37. input: "current",
  38. processor: "not",
  39. formatter: "boolean",
  40. autoClose: false,
  41. title: title
  42. };
  43. }
  44. let config_desc = {
  45. "auto-hide-code": _bollDest("Auto hide code", "Hide long code blocks by default"),
  46. "auto-hide-rows": {
  47. name: "Min rows to hide",
  48. value: 10,
  49. processor: "int_range-1-",
  50. autoClose: false,
  51. title: "Minimum number of rows to hide"
  52. },
  53. "flat-layout": _bollDest("Flat layout", "Use flat layout for script list and descriptions", false),
  54. "animation": _bollDest("Animation", "Enable animation for toggling code blocks")
  55. };
  56. let config = GM_config(config_desc);
  57. // CSS
  58. const dynamicStyle = {
  59. "flat-layout": `
  60. .script-list li:not(.ad-entry) { padding-right: 0; } ol.script-list > li > article { display: flex; flex-direction: row; justify-content: space-between; align-items: center; }
  61. ol.script-list > li > article > h2 { width: 60%; overflow: hidden; text-overflow: ellipsis; margin-right: 0.5em; padding-right: 0.5em; border-right: 1px solid #DDDDDD; }
  62. .showing-all-languages .badge-js, .showing-all-languages .badge-css, .script-type { display: none; }
  63. ol.script-list > li > article > h2 > a.script-link { white-space: nowrap; }
  64. ol.script-list > li > article > h2 > span.script-description { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  65. ol.script-list > li > article > div.script-meta-block { width: 40%; column-gap: 0; }
  66. ol.script-list > li[data-script-type="library"] > article > h2 { width: 80%; }
  67. ol.script-list > li[data-script-type="library"] > article > div.script-meta-block { width: 20%; column-count: 1; }
  68. ol.script-list > li > article > div.script-meta-block > dl.inline-script-stats { margin: 0; }
  69. ol.script-list > li > article > div.script-meta-block > dl.inline-script-stats > dd { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  70. #script-info div.script-meta-block { float: right; column-count: 1; max-width: 300px; border-left: 1px solid #DDDDDD; margin-left: 1em; padding-left: 1em; }
  71. #additional-info { width: calc(100% - 2em - 2px); }`,
  72. "animation": `
  73. /* Toggle code animation */
  74. pre > code { transition: height 0.5s ease-in-out 0s; }
  75. /* Adapted from animate.css - https://animate.style/ */
  76. :root { --animate-duration: 1s; --animate-delay: 1s; --animate-repeat: 1; }
  77. .animate__animated { animation-duration: var(--animate-duration); animation-fill-mode: both; }
  78. .animate__animated.animate__fastest { animation-duration: calc(var(--animate-duration) / 3); }
  79. @keyframes tada {
  80. from { transform: scale3d(1, 1, 1); }
  81. 10%, 20% { transform: scale3d(0.9, 0.9, 0.9) rotate3d(0, 0, 1, -3deg); }
  82. 30%, 50%, 70%, 90% { transform: scale3d(1.1, 1.1, 1.1) rotate3d(0, 0, 1, 3deg); }
  83. 40%, 60%, 80% { transform: scale3d(1.1, 1.1, 1.1) rotate3d(0, 0, 1, -3deg); }
  84. to { transform: scale3d(1, 1, 1); }
  85. }
  86. .animate__tada { animation-name: tada; }
  87. @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
  88. .animate__fadeIn { animation-name: fadeIn; }
  89. @keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } }
  90. .animate__fadeOut { -webkit-animation-name: fadeOut; animation-name: fadeOut; }`
  91. };
  92. // Functions
  93. let body = document.querySelector("body");
  94. function sanitify(s) {
  95. // Remove emojis (such a headache)
  96. s = s.replaceAll(/([\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2580-\u27BF]|\uD83E[\uDD10-\uDEFF]|\uFE0F)/g, "");
  97. // Trim spaces and newlines
  98. s = s.trim();
  99. // Replace spaces
  100. s = s.replaceAll(" ", "-");
  101. s = s.replaceAll("%20", "-");
  102. // No more multiple "-"
  103. s = s.replaceAll(/-+/g, "-");
  104. return s;
  105. }
  106. function process(node) { // Add anchor and assign id to given node; Add to outline. Return true if node is actually processed.
  107. if (node.childElementCount > 1 || node.classList.length > 0) return false; // Ignore complex nodes
  108. let text = node.textContent;
  109. if (!node.id) { // If the node has no id
  110. node.id = sanitify(text); // Then assign id
  111. }
  112. // Add anchors
  113. let node_ = document.createElement('a');
  114. node_.className = 'anchor';
  115. node_.href = '#' + node.id;
  116. node.appendChild(node_);
  117. let list_item = document.createElement("li");
  118. outline.appendChild(list_item);
  119. let link = document.createElement("a");
  120. link.href = "#" + node.id;
  121. link.text = text;
  122. list_item.appendChild(link);
  123. return true;
  124. }
  125. async function animate(node, animation) {
  126. return new Promise((resolve, reject) => {
  127. node.classList.add("animate__animated", "animate__" + animation);
  128. if (node.getAnimations().length == 0) {
  129. node.classList.remove("animate__animated", "animate__" + animation);
  130. reject("No animation available");
  131. }
  132. node.addEventListener('animationend', e => {
  133. e.stopPropagation();
  134. node.classList.remove("animate__animated", "animate__" + animation);
  135. resolve("Animation ended");
  136. }, { once: true });
  137. });
  138. }
  139. async function transition(node, height) {
  140. return new Promise((resolve, reject) => {
  141. node.style.height = height;
  142. if (node.getAnimations().length == 0) {
  143. resolve("No transition available");
  144. }
  145. node.addEventListener('transitionend', e => {
  146. e.stopPropagation();
  147. resolve("Transition ended");
  148. }, { once: true });
  149. });
  150. }
  151. function copyCode() {
  152. let code = this.parentNode.nextElementSibling;
  153. let text = code.textContent;
  154. navigator.clipboard.writeText(text).then(() => {
  155. this.textContent = "Copied!";
  156. animate(this, "tada").then(() => {
  157. this.textContent = "Copy code";
  158. }, () => {
  159. window.setTimeout(() => {
  160. this.textContent = "Copy code";
  161. }, 1000);
  162. });
  163. });
  164. }
  165. function toggleCode() {
  166. let code = this.parentNode.nextElementSibling;
  167. if (code.style.height == "0px") {
  168. code.style.willChange = "height";
  169. transition(code, code.getAttribute("data-height")).then(() => {
  170. code.style.willChange = "";
  171. });
  172. animate(this, "fadeOut").then(() => {
  173. this.textContent = "Hide code";
  174. animate(this, "fadeIn");
  175. }, () => {
  176. this.textContent = "Hide code";
  177. });
  178. } else {
  179. code.style.willChange = "height";
  180. transition(code, "0px").then(() => {
  181. code.style.willChange = "";
  182. });
  183. animate(this, "fadeOut").then(() => {
  184. this.textContent = "Show code";
  185. animate(this, "fadeIn");
  186. }, () => {
  187. this.textContent = "Show code";
  188. });
  189. }
  190. }
  191. function create_toolbar() {
  192. let toolbar = document.createElement("div");
  193. let copy = document.createElement("a");
  194. let toggle = document.createElement("a");
  195. toolbar.appendChild(copy);
  196. toolbar.appendChild(toggle);
  197. copy.textContent = "Copy code";
  198. copy.className = "code-operation";
  199. copy.title = "Copy code to clipboard";
  200. copy.addEventListener("click", copyCode);
  201. toggle.textContent = "Hide code";
  202. toggle.classList.add("code-operation", "animate__fastest");
  203. toggle.title = "Toggle code display";
  204. toggle.addEventListener("click", toggleCode);
  205. // Css
  206. toolbar.className = "code-toolbar";
  207. return toolbar;
  208. }
  209. function injectCSS(id, css) {
  210. let style = document.createElement("style");
  211. style.id = idPrefix + id;
  212. style.textContent = css;
  213. document.head.appendChild(style);
  214. }
  215. function cssHelper(id, enable) {
  216. let current = document.getElementById(idPrefix + id);
  217. if (current) {
  218. current.disabled = !enable;
  219. } else if (enable) {
  220. injectCSS(id, dynamicStyle[id]);
  221. }
  222. }
  223. // Basic css
  224. injectCSS("basic", `
  225. html { scroll-behavior: smooth; }
  226. a.anchor::before { content: "#"; }
  227. a.anchor { opacity: 0; text-decoration: none; padding: 0px 0.5em; transition: all 0.25s ease-in-out; }
  228. h1:hover>a.anchor, h2:hover>a.anchor, h3:hover>a.anchor,
  229. h4:hover>a.anchor, h5:hover>a.anchor, h6:hover>a.anchor { opacity: 1; transition: all 0.25s ease-in-out; }
  230. a.button { margin: 0.5em 0 0 0; display: flex; align-items: center; justify-content: center; text-decoration: none; color: black; background-color: #a42121ab; border-radius: 50%; width: 2em; height: 2em; font-size: 1.8em; font-weight: bold; }
  231. div.code-toolbar { display: flex; gap: 1em; }
  232. a.code-operation { cursor: pointer; font-style: italic; }
  233. div.lum-lightbox { z-index: 2; }
  234. div#float-buttons { position: fixed; bottom: 1em; right: 1em; display: flex; flex-direction: column; user-select: none; z-index: 1; }
  235. aside.panel { display: none; }
  236. .dynamic-opacity { transition: opacity 0.2s ease-in-out; opacity: 0.2; }
  237. .dynamic-opacity:hover { opacity: 0.8; }
  238. input[type=file] { border-style: dashed; border-radius: 0.5em; padding: 0.5em; background: rgba(169, 169, 169, 0.4); }
  239. table { border: 1px solid #8d8d8d; border-collapse: collapse; width: auto; }
  240. table td, table th { padding: 0.5em 0.75em; vertical-align: middle; border: 1px solid #8d8d8d; }
  241. @media (any-hover: none) { .dynamic-opacity { opacity: 0.8; } .dynamic-opacity:hover { opacity: 0.8; } }
  242. @media screen and (min-width: 767px) {
  243. aside.panel { display: contents; line-height: 1.5; }
  244. ul.outline { position: sticky; float: right; padding: 0 0 0 0.5em; margin: 0 0.5em -99vh; max-height: 80vh; border: 1px solid #BBBBBB; border-left: 2px solid #F2E5E5; box-shadow: 0 0 5px #ddd; background: linear-gradient(to right, #fcf1f1, #FFF 1em); list-style: none; width: 10.5%; color: gray; border-radius: 5px; overflow-y: scroll; z-index: 1; }
  245. ul.outline > li { overflow: hidden; text-overflow: ellipsis; }
  246. ul.outline > li > a { color: gray; white-space: nowrap; text-decoration: none; }
  247. }
  248. pre > code { overflow: hidden; display: block; }`);
  249. // Aside panel & Anchors
  250. let outline;
  251. let is_script = /^\/[^\/]+\/scripts/;
  252. let is_specific_script = /^\/[^\/]+\/scripts\/\d+/;
  253. let is_disccussion = /^\/[^\/]+\/discussions/;
  254. let path = window.location.pathname;
  255. if ((!is_script.test(path) && !is_disccussion.test(path)) || is_specific_script.test(path)) {
  256. let panel = document.createElement("aside");
  257. panel.className = "panel";
  258. body.insertBefore(panel, document.querySelector("body > div.width-constraint"));
  259. let reference_node = document.querySelector("body > div.width-constraint > section");
  260. outline = document.createElement("ul");
  261. outline.classList.add("outline");
  262. outline.classList.add("dynamic-opacity");
  263. outline.style.top = reference_node ? getComputedStyle(reference_node).marginTop : "1em";
  264. outline.style.marginTop = outline.style.top;
  265. panel.appendChild(outline);
  266. let flag = false;
  267. document.querySelectorAll("body > div.width-constraint h1, h2, h3, h4, h5, h6").forEach((node) => {
  268. flag = process(node) || flag; // Not `flag || process(node)`!
  269. });
  270. if (!flag) {
  271. panel.remove();
  272. }
  273. }
  274. // Navigate to hash
  275. let hash = window.location.hash.slice(1);
  276. if (hash) {
  277. let ele = document.getElementById(decodeURIComponent(hash));
  278. if (ele) {
  279. ele.scrollIntoView();
  280. }
  281. }
  282. // Buttons
  283. let buttons = document.createElement("div");
  284. buttons.id = "float-buttons";
  285. let to_top = document.createElement("a");
  286. to_top.classList.add("button");
  287. to_top.classList.add("dynamic-opacity");
  288. to_top.href = "#top";
  289. to_top.text = "↑";
  290. buttons.appendChild(to_top);
  291. body.appendChild(buttons);
  292. // Double click to get to top
  293. body.addEventListener("dblclick", (e) => {
  294. if (e.target === body) {
  295. to_top.click();
  296. }
  297. });
  298. // Fix current tab link
  299. let tab = document.querySelector("ul#script-links > li.current");
  300. if (tab) {
  301. let link = document.createElement("a");
  302. link.href = window.location.pathname;
  303. let orig_child = tab.firstChild;
  304. link.appendChild(orig_child);
  305. tab.appendChild(link);
  306. }
  307. let parts = window.location.pathname.split("/");
  308. if (parts.length <= 2 || (parts.length == 3 && parts[2] === '')) {
  309. let banner = document.querySelector("header#main-header div#site-name");
  310. let img = banner.querySelector("img");
  311. let text = banner.querySelector("#site-name-text > h1");
  312. let link1 = document.createElement("a");
  313. link1.href = window.location.pathname;
  314. img.parentNode.replaceChild(link1, img);
  315. link1.appendChild(img);
  316. let link2 = document.createElement("a");
  317. link2.href = window.location.pathname;
  318. link2.textContent = text.textContent;
  319. text.textContent = "";
  320. text.appendChild(link2);
  321. }
  322. // Toolbar for code blocks
  323. let code_blocks = document.getElementsByTagName("pre");
  324. let auto_hide = config["auto-hide-code"];
  325. let auto_hide_rows = config["auto-hide-rows"];
  326. for (let code_block of code_blocks) {
  327. if (code_block.firstChild.tagName === "CODE") {
  328. let height = getComputedStyle(code_block.firstChild).getPropertyValue("height");
  329. code_block.firstChild.style.height = height;
  330. code_block.firstChild.setAttribute("data-height", height);
  331. code_block.insertAdjacentElement("afterbegin", create_toolbar());
  332. }
  333. }
  334. // Auto hide code blocks
  335. function autoHide() {
  336. if (!auto_hide) {
  337. for (let code_block of code_blocks) {
  338. let toggle = code_block.firstChild.lastChild;
  339. if (toggle.textContent === "Show code") {
  340. toggle.click(); // Click the toggle button
  341. }
  342. }
  343. } else {
  344. for (let code_block of code_blocks) {
  345. let m = code_block.lastChild.textContent.match(/\n/g);
  346. let rows = m ? m.length : 0;
  347. let toggle = code_block.firstChild.lastChild;
  348. let hidden = toggle.textContent === "Show code";
  349. if (rows >= auto_hide_rows && !hidden || rows < auto_hide_rows && hidden) {
  350. code_block.firstChild.lastChild.click(); // Click the toggle button
  351. }
  352. }
  353. }
  354. }
  355. document.addEventListener("readystatechange", (e) => {
  356. if (e.target.readyState === "complete") {
  357. autoHide();
  358. }
  359. }, {once: true});
  360. // Initialize css
  361. for (let prop in dynamicStyle) {
  362. cssHelper(prop, config[prop]);
  363. }
  364. // Dynamically respond to config changes
  365. let callbacks = {
  366. "auto-hide-code": (after) => {
  367. auto_hide = after;
  368. autoHide();
  369. },
  370. "auto-hide-rows": (after) => {
  371. auto_hide_rows = after;
  372. autoHide();
  373. },
  374. "flat-layout": (after) => {
  375. const meta_orig = document.querySelector("#script-info > #script-content > .script-meta-block");
  376. const meta_mod = document.querySelector("#script-info > .script-meta-block");
  377. if (after && meta_orig) {
  378. const links = document.querySelector("#script-info > #script-links");
  379. links.after(meta_orig);
  380. } else if (!after && meta_mod) {
  381. const additional = document.querySelector("#script-info > #script-content > #additional-info");
  382. additional.before(meta_mod);
  383. }
  384. },
  385. };
  386. callbacks["flat-layout"](config["flat-layout"]);
  387. window.addEventListener(GM_config_event, e => {
  388. if (e.detail.type === "set") {
  389. let callback = callbacks[e.detail.prop];
  390. if (callback && (e.detail.before !== e.detail.after)) {
  391. callback(e.detail.after);
  392. }
  393. if (e.detail.prop in dynamicStyle) {
  394. cssHelper(e.detail.prop, e.detail.after);
  395. }
  396. }
  397. });
  398. })();