Imgur - Fix Quotation Marks in the Old Design

Imgur's "old design" sometimes includes """ and "'" in post descriptions instead of quotation marks. This replaces them with the appropriate characters.

目前为 2023-12-01 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Imgur - Fix Quotation Marks in the Old Design
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.0
  5. // @description Imgur's "old design" sometimes includes """ and "'" in post descriptions instead of quotation marks. This replaces them with the appropriate characters.
  6. // @author Corrodias
  7. // @match https://imgur.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=imgur.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. const mutationObserver = new MutationObserver(mutations => {
  17. for (const mutation of mutations) {
  18. processMutation(mutation);
  19. }
  20. });
  21.  
  22. function processMutation(mutation) {
  23. for (const node of mutation.addedNodes) {
  24. if (node.nodeType !== Node.ELEMENT_NODE) continue;
  25. // Only act if a new post has been loaded.
  26. if (!node.classList.contains('post-image-container')) return;
  27. replaceText(node);
  28. }
  29. }
  30.  
  31. function replaceText(post) {
  32. // This is empty if the current page is not a post.
  33. const description = post.getElementsByClassName('post-image-description');
  34. for (const a of description) {
  35. a.textContent = a.textContent.replaceAll('"', '"').replaceAll(''', "'");
  36. }
  37. }
  38.  
  39. // Monitor dynamically loaded content, for when the user nagivates to a new post. Observe as little as possible.
  40. const postContent = document.body.getElementsByClassName('post-images');
  41. for (const a of postContent) {
  42. mutationObserver.observe(a, { attributes: false, childList: true, subtree: true });
  43. }
  44.  
  45. // Also run the replacement now in case this is a post.
  46. replaceText(document.body);
  47. })();