AO3 Text Replacer

Replace certain words on AO3 fanfics with new ones of your choosing

当前为 2022-04-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name AO3 Text Replacer
  3. // @namespace https://greasyfork.org/en/users/899130
  4. // @version 1.1
  5. // @description Replace certain words on AO3 fanfics with new ones of your choosing
  6. // @author Cor_Caroli
  7. // @include /https?://archiveofourown\.org/works/\d+/
  8. // @grant none
  9. // @directions See the CONFIGURATION SECTION. Edit the words in the word replacement dictionary.
  10. // ==/UserScript==
  11.  
  12. /***************************/
  13. /** CONFIGURATION SECTION **/
  14. /***************************/
  15.  
  16. /* * Master list of words to replace. Add as many words to replace as you'd like.
  17. // After making changes to the word replacement dictionary, save (Ctrl+S or whatever hotkey) and then refresh AO3 to see the word replacement changes.
  18. // !! Note that WORDS ARE CASE SENSITIVE !!
  19. // The format is:
  20. // "old word" : "new word",
  21. //
  22. // Example: You want to change all instances of "pancakes" to "waffles" in the fic
  23. // Insert a line of code:
  24. "pancakes" : "waffles",
  25. // (Remember to keep the comma at the end.)
  26. //
  27. // Alternatively, you can just edit the placeholder code lines with the words you want. E.g. reader insert fics, triggers, etc.
  28. * */
  29. const replaceDictionary = {
  30. "old word 1" : "new word 1",
  31. "old word 2" : "new word 2",
  32. "old word 3" : "new word 3",
  33. "old word 4" : "new word 4",
  34. "old word 5" : "new word 5",
  35. "old word 6" : "new word 6",
  36. "old word 7" : "new word 7",
  37. };
  38.  
  39.  
  40.  
  41. /********************/
  42. /** SCRIPT SECTION **/
  43. /********************/
  44.  
  45. // Finds the target div element of the fic and drills down to its innerHTML
  46. var origFic = document.querySelector('#main').innerHTML;
  47.  
  48. // Function that processes the text replacements in the fic
  49. function ficProcessing(){
  50. // Initialize variable
  51. var changedFic = origFic;
  52.  
  53. for (let w in replaceDictionary){
  54.  
  55. // Needs a regex in order to replace words not in hyperlinks (replacing words in hyperlinks can lead to broken links when navigating to other pages)
  56. // Checks for hyperlinks and replaces the anchor text, but not the URL
  57. var regex = new RegExp("(?<!\<[^>]*)\\b" + w + "\\b", 'g');
  58.  
  59. changedFic = changedFic.replace(regex, replaceDictionary[w]);
  60. }
  61.  
  62. // Replaces the fic with the new words
  63. document.querySelector('#main').innerHTML = changedFic;
  64. };
  65.  
  66. // Runs the function
  67. ficProcessing();