Greasy Fork 增强

增进 Greasyfork 浏览体验。

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

  1. // ==UserScript==
  2. // @name Greasy Fork Enhance
  3. // @name:zh-CN Greasy Fork 增强
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.1.2
  6. // @description Enhance your experience at Greasyfork.
  7. // @description:zh-CN 增进 Greasyfork 浏览体验。
  8. // @author PRO
  9. // @match https://greasyfork.org/*
  10. // @icon https://greasyfork.org/vite/assets/blacklogo16-bc64b9f7.png
  11. // @license gpl-3.0
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17. function sanitify(s) {
  18. // Trim spaces and newlines
  19. s = s.trim();
  20. // Replace spaces
  21. s = s.replaceAll(" ", "-");
  22. s = s.replaceAll("%20", "-");
  23. // Remove emojis (such a headache)
  24. s = s.replaceAll(/([\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2580-\u27BF]|\uD83E[\uDD10-\uDEFF]|\uFE0F| )+-/g, '');
  25. return s;
  26. }
  27. function process(node) { // Add anchor and assign id to given node
  28. if (node.childElementCount > 1) return; // Ignore complex nodes
  29. node.id = sanitify(node.textContent); // Assign id
  30. // Add anchors
  31. let node_ = document.createElement('a');
  32. node_.className = 'anchor';
  33. node_.href = '#' + node.id;
  34. node.appendChild(node_);
  35. }
  36. // Css for anchors
  37. let css = document.createElement("style");
  38. css.textContent = `
  39. html {
  40. scroll-behavior: smooth;
  41. }
  42. a.anchor::before {
  43. content: "#";
  44. }
  45. a.anchor {
  46. opacity: 0;
  47. text-decoration: none;
  48. padding: 0px 0.5em;
  49. -moz-transition: all 0.25s ease-in-out;
  50. -o-transition: all 0.25s ease-in-out;
  51. -webkit-transition: all 0.25s ease-in-out;
  52. transition: all 0.25s ease-in-out;
  53. }
  54. h1:hover>a.anchor,
  55. h2:hover>a.anchor,
  56. h3:hover>a.anchor,
  57. h4:hover>a.anchor,
  58. h5:hover>a.anchor,
  59. h6:hover>a.anchor {
  60. opacity: 1;
  61. -moz-transition: all 0.25s ease-in-out;
  62. -o-transition: all 0.25s ease-in-out;
  63. -webkit-transition: all 0.25s ease-in-out;
  64. transition: all 0.25s ease-in-out;
  65. }
  66. @media (any-hover: none) {
  67. a.anchor {
  68. opacity: 1;
  69. }
  70. }`;
  71. document.querySelector("head").appendChild(css); // Inject css
  72. document.querySelectorAll("body > div.width-constraint h1, h2, h3, h4, h5, h6").forEach(process); // Main
  73. })();