MeFi replace quote label

MetaFilter: nicer MefiQuote buttons

目前为 2025-03-29 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name MeFi replace quote label
  3. // @namespace https://github.com/klipspringr/mefi-userscripts
  4. // @version 2025-03-28-i
  5. // @description MetaFilter: nicer MefiQuote buttons
  6. // @author Klipspringer
  7. // @supportURL https://github.com/klipspringr/mefi-userscripts
  8. // @license MIT
  9. // @match *://*.metafilter.com/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. const getSetting = (key, defaultValue) => {
  14. try {
  15. return localStorage.getItem(key) || defaultValue;
  16. } catch {
  17. return defaultValue;
  18. }
  19. };
  20.  
  21. const modifyQuoteButton = (quoteButtonNode, to) => {
  22. // replace quote button content
  23. quoteButtonNode.textContent = to;
  24.  
  25. // move quote button to before flag button
  26. const quoteNode = quoteButtonNode.parentNode;
  27. const bylineNode = quoteNode.parentNode;
  28. const flagNode = bylineNode.querySelector("span[id^='flag']");
  29. if (flagNode) bylineNode.insertBefore(quoteNode, flagNode);
  30.  
  31. // mark as done so we don't pick this up again
  32. quoteButtonNode.setAttribute("data-mrql-done", "");
  33. };
  34.  
  35. const modifyQuoteButtons = (to) => {
  36. const start = performance.now();
  37.  
  38. const quoteButtonNodes = document.querySelectorAll(
  39. 'a[class="quotebutton"]:not([data-mrql-done])'
  40. );
  41.  
  42. for (const quoteButtonNode of quoteButtonNodes)
  43. modifyQuoteButton(quoteButtonNode, to);
  44.  
  45. console.log(
  46. "mefi-replace-quote-label",
  47. quoteButtonNodes.length,
  48. `"${to}"`,
  49. performance.now() - start + "ms"
  50. );
  51. };
  52.  
  53. (() => {
  54. if (!/^\/(\d|comments\.mefi)/.test(window.location.pathname)) return;
  55.  
  56. const DEFAULT_TO = "↩ "; // note space, for aesthetics
  57.  
  58. const to = getSetting("mefi-replace-quote-label", DEFAULT_TO);
  59.  
  60. const runner = () => modifyQuoteButtons(to);
  61.  
  62. // MefiQuote listens for the "mefi-comments" event, but:
  63. // (a) my event listener wasn't picking that up, for some reason; and
  64. // (b) there could be timing issues as MefiQuote needs to complete its work first
  65. // hence using MutationObserver instead.
  66. const newCommentsElement = document.getElementById("newcomments");
  67. if (newCommentsElement) {
  68. const observer = new MutationObserver(runner);
  69. observer.observe(newCommentsElement, { childList: true });
  70. }
  71.  
  72. runner();
  73. })();

QingJ © 2025

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