-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwNoahPreferred.js
74 lines (61 loc) · 2.27 KB
/
twNoahPreferred.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// ==UserScript==
// @name John/Noah Name Switcher for AO3 (site-wide; NOAH PREFERRED)
// @namespace github.com/forceofcalm
// @version 0.1
// @description Switches the name John to Noah on AO3 on works tagged as Teen Wolf.
// @author calm
// @match https://archiveofourown.org/works/*
// @icon [later]
// @grant none
// @license MIT
// ==/UserScript==
const names = {
john: {
name: 'John',
regex: new RegExp(String.raw`\bJohn\b`, 'gu'),
regexLower: new RegExp(String.raw`\bjohn\b`, 'gu'),
regextUpper: new RegExp(String.raw`\bJOHN\b`, 'gu'),
regexIgnoreCase: new RegExp(String.raw`\bJohn\b`, 'gui'),
},
noah: {
name: 'Noah',
regex: new RegExp(String.raw`\bNoah\b`, 'gu'),
regexLower: new RegExp(String.raw`\bnoah\b`, 'gu'),
regexUpper: new RegExp(String.raw`\bNOAH\b`, 'gu'),
regexIgnoreCase: new RegExp(String.raw`\bNoah\b`, 'gui'),
},
}
const preferredName = names.noah;
const otherName = names.john;
(() => {
'use strict';
if (!document.querySelector(".fandom.tags ul.commas").querySelectorAll("li").some(tag => tag.textContent === "Teen Wolf")) return;
const workText = document.querySelector('#workskin');
const preferredName = preferredName.regexIgnoreCase.test(workText.textContent);
const otherName = otherName.regexIgnoreCase.test(workText.textContent);
const hasBothNames = preferredName && otherName;
// check that work only has one of the names
if (hasBothNames) {
alert('John/Noah Name Switcher: Work contains both John and Noah. Please edit the work manually.')
return;
}
// work is already in preferred name
if (preferredName) {
return;
}
function replaceWord(node, oldWord, newWord) {
if (node.nodeType === Node.TEXT_NODE) {
node.textContent = node.textContent.replace(oldWord, newWord);
} else if (node.hasChildNodes()) {
node.childNodes.forEach(childNode => {
replaceWord(childNode, oldWord, newWord);
});
}
}
// normally cased names
replaceWord(workText, otherName.regex, preferredName.name);
// lower cased names
replaceWord(workText, otherName.regexLower, preferredName.name.toLowerCase());
// upper cased names
replaceWord(workText, otherName.regexUpper, preferredName.name.toUpperCase());
})();