AO3 Text Replacer

Replace certain words on archiveofourown.org with new ones of your choosing

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name            AO3 Text Replacer
// @namespace       https://greasyfork.org/en/users/899130
// @version         1.0
// @description     Replace certain words on archiveofourown.org with new ones of your choosing
// @author          Cor_Caroli
// @include         /https?://archiveofourown\.org/works/\d+/
// @grant           none
// @directions      See the CONFIGURATION SECTION. Edit the words in the word replacement dictionary.
// ==/UserScript==

/***************************/
/** CONFIGURATION SECTION **/
/***************************/

/* * Master list of words to replace. Add as many words to replace as you'd like.
// After making changes to the word replacement dictionary, save (Ctrl+S or whatever hotkey) and then refresh AO3 to see the word replacement changes.
// !! Note that WORDS ARE CASE SENSITIVE !!
// The format is:
// "old word" : "new word",
//
// Example: You want to change all instances of "pancakes" to "waffles" in the fic
// Insert a line of code:
        "pancakes" : "waffles",
// (Remember to keep the comma at the end.)
//
// Alternatively, you can just edit the placeholder code lines with the words you want.
* */
const replaceDictionary = {
    "old word 1" : "new word 1",
    "new word 2" : "new word 2",
    "new word 3" : "new word 3",
    "old word 4" : "new word 4",
    "new word 5" : "new word 5",
    "new word 6" : "new word 6",
    "new word 7" : "new word 7",
};



/********************/
/** SCRIPT SECTION **/
/********************/

// Finds the target div element of the fic and drills down to its innerHTML
var origFic = document.querySelector('div.work').innerHTML;

// Function that processes the text replacements in the fic
function ficProcessing(){
    // Initialize variable
    var changedFic = origFic;

    for (let w in replaceDictionary){

        // 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)
        // Checks for hyperlinks and replaces the anchor text, but not the URL
        var regex = new RegExp("~<a .*?</a>|\\b" + w + "\\b", 'g');

        changedFic = changedFic.replace(regex, replaceDictionary[w]);
    }

    // Replaces the fic with the new words
    document.querySelector('div.work').innerHTML = changedFic;
};

// Runs the function
ficProcessing();