metamorpov/src/replace-words.js

263 lines
8.1 KiB
JavaScript
Raw Normal View History

//PAUSED_KEY = 'pause-this-domain-pls-interactive-fics-yalla-bina';
const verbsHelper = require('english-verbs-helper');
2024-12-23 05:58:48 -05:00
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if ('input_word' in message){
escapeAndReplace(message.input_word, message.replace_value, message.case_sensitive);
2024-12-23 05:58:48 -05:00
} else {
replaceAll();
2024-12-23 05:58:48 -05:00
}
2019-10-19 05:27:27 -04:00
});
function replaceAll() {
browser.storage.local.get(null, replaceAllInStorage); // TODO this is bad
2024-12-23 05:58:48 -05:00
}
function replaceAllInStorage(options) {
//const hostname = window.location.hostname
//const is_paused = options[PAUSED_KEY] && options[PAUSED_KEY].indexOf(hostname) !== -1
2024-12-23 05:58:48 -05:00
if (true) { // change paused later to only care about hostname
replaceName(options);
replacePronouns(options);
replacePov(options);
replacePlv(options);
replaceAlso(options);
replaceVerbs(options);
2024-12-23 05:58:48 -05:00
}
}
function replaceName(options) {
const regexp = /\by\/n\b|\(y\/n\)|\[y\/n\]/ig;
walk(document.body, regexp, options["name"], directMethod);
}
function replaceVerbs(options) {
const regexp = /vrb\/([\w\s]+)\/([\w\s]+)\//;
walk(document.body, regexp, options, verbMethod);
2024-12-23 05:58:48 -05:00
}
// TODO case sensitivity, premade regexp
function replacePronouns(options) {
if (options.preset == "") { return; }
let pronouns = getPronouns(options.preset, options.other);
replacePronounSet("Prn/s", "prn/s", pronouns["subjective"]);
replacePronounSet("Prn/o", "prn/o", pronouns["objective"]);
replacePronounSet("Prn/p", "prn/p", pronouns["possessive"]);
replacePronounSet("Prn/a", "prn/a", pronouns["adjective"]);
replacePronounSet("Prn/r", "prn/r", pronouns["reflexive"]);
replacePronounSet("Prn/H", "prn/H", pronouns["honorific-abbr"]);
replacePronounSet("Prn/h", "prn/h", pronouns["honorific"]);
replacePronounSet("Prn/N", "prn/N", pronouns["noun-adult"]);
replacePronounSet("Prn/n", "prn/n", pronouns["noun-child"]);
}
function getPronouns(key, other) {
switch (key) {
case "she":
return {
"subjective": "she",
"objective": "her",
"possessive": "her",
"adjective": "hers",
"reflexive": "herself",
"honorific-abbr": "Ms.",
"honorific": "miss",
"noun-adult": "woman",
"noun-child": "girl",
"plurality": "singular"
};
case "he":
return {
"subjective": "he",
"objective": "him",
"possessive": "his",
"adjective": "his",
"reflexive": "himself",
"honorific-abbr": "Mr.",
"honorific": "mister",
"noun-adult": "man",
"noun-child": "boy",
"plurality": "singular"
};
case "they":
return {
"subjective": "they",
"objective": "them",
"possessive": "their",
"adjective": "theirs",
"reflexive": "themself",
"honorific-abbr": "Mx.",
"honorific": "mix",
"noun-adult": "person",
"noun-child": "kid",
"plurality": "plural"
};
case "first":
return {
"subjective": "I",
"objective": "me",
"possessive": "my",
"adjective": "mine",
"reflexive": "myself"
};
case "second":
return {
"subjective": "you",
"objective": "you",
"possessive": "your",
"adjective": "yours",
"reflexive": "yourself"
};
case "first-plural":
return {
"subjective": "we",
"objective": "us",
"possessive": "our",
"adjective": "ours",
"reflexive": "ourselves"
};
case "second-plural":
return {
"subjective": "you",
"objective": "you",
"possessive": "your",
"adjective": "yours",
"reflexive": "yourselves"
};
case "third-plural":
return {
"subjective": "they",
"objective": "them",
"possessive": "their",
"adjective": "theirs",
"reflexive": "themselves"
};
case "other":
return options.other;
}
}
function replacePronounSet(keyCapital, key, pronoun) {
if (pronoun === undefined) { return; }
escapeAndReplace(keyCapital, capitalize(pronoun), true);
escapeAndReplace(key, pronoun, true);
}
function replacePov(options) {
let pronouns;
switch (options.pov) {
case "":
return;
case "third":
pronouns = getPronouns(options.preset, options.other);
replacePronounSet("Pov/S", "pov/S", options.name);
replacePronounSet("Pov/O", "pov/O", options.name);
replacePronounSet("Pov/P", "pov/P", options.name + "'s");
replacePronounSet("Pov/A", "pov/A", options.name + "'s");
replacePronounSet("Pov/R", "pov/R", options.name + "'s self");
break;
default:
pronouns = getPronouns(options.pov, null);
replacePronounSet("Pov/S", "pov/S", pronouns["subjective"]);
replacePronounSet("Pov/O", "pov/O", pronouns["objective"]);
replacePronounSet("Pov/P", "pov/P", pronouns["possessive"]);
replacePronounSet("Pov/A", "pov/A", pronouns["adjective"]);
replacePronounSet("Pov/R", "pov/R", pronouns["reflexive"]);
}
replacePronounSet("Pov/s", "pov/s", pronouns["subjective"]);
replacePronounSet("Pov/o", "pov/o", pronouns["objective"]);
replacePronounSet("Pov/p", "pov/p", pronouns["possessive"]);
replacePronounSet("Pov/a", "pov/a", pronouns["adjective"]);
replacePronounSet("Pov/r", "pov/r", pronouns["reflexive"]);
2019-10-20 07:41:12 -04:00
}
2024-12-23 05:58:48 -05:00
function replacePlv(options) {
if (options.pov == "") { return; }
let pronouns = getPronouns(options.pov + "-plural", null);
replacePronounSet("Plv/s", "plv/s", pronouns["subjective"]);
replacePronounSet("Plv/o", "plv/o", pronouns["objective"]);
replacePronounSet("Plv/p", "plv/p", pronouns["possessive"]);
replacePronounSet("Plv/a", "plv/a", pronouns["adjective"]);
replacePronounSet("Plv/r", "plv/r", pronouns["reflexive"]);
}
2024-12-23 05:58:48 -05:00
function replaceAlso(options) {
Object.entries(options.also).forEach(([key, value]) => {
escapeAndReplace(key, value, true);
});
2016-07-06 01:12:36 -04:00
}
2016-06-18 23:04:38 -04:00
2024-12-23 05:58:48 -05:00
function escapeAndReplace(input_word, replace_value, case_sensitive) {
let input_word_escaped = input_word.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
2024-12-23 05:58:48 -05:00
const flags = case_sensitive ? "g" : "ig"
if (input_word_escaped[0].match(/[a-z]/i)) {
input_word_escaped = `\\b${input_word_escaped}`
}
if (input_word_escaped[input_word_escaped.length - 1].match((/[a-z]/i))) {
input_word_escaped = `${input_word_escaped}\\b`
}
const regexp_input_word = new RegExp(input_word_escaped, flags)
walk(document.body, regexp_input_word, replace_value, directMethod);
}
function walk(node, input_word, replace_value, replaceMethod) {
if (node.contentEditable == "true" || node.type == "textarea" || node.type == "input") { return; }
var child, next;
switch (node.nodeType) {
case 1: /* ELEMENT_NODE */
case 9: /* DOCUMENT_NODE */
case 11: /* DOCUMENT_FRAGMENT_NODE */
child = node.firstChild;
while (child) {
next = child.nextSibling;
walk(child, input_word, replace_value, replaceMethod);
child = next;
}
break;
case 3: /* TEXT_NODE */
replaceMethod(node, input_word, replace_value);
2024-12-23 05:58:48 -05:00
}
}
function directMethod(node, input_word, replace_value) {
node.nodeValue = node.nodeValue.replace(input_word, replace_value); // TODO might replaceall work better here?;
}
// TODO do something about options being not input_word
function verbMethod(node, regexp, options) {
var match = node.nodeValue.match(regexp);
if (match == null) { return; }
var verb = match[1];
var tense = match[2];
const replace_value = verbsHelper.getConjugation(null, verb, tense, getPovIndex(options));
node.nodeValue = node.nodeValue.replace(regexp, replace_value);
}
2024-12-23 05:58:48 -05:00
function capitalize(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
2015-02-21 22:00:28 -05:00
}
// TODO figure out plv for this
function getPovIndex(options) {
switch (options.pov) {
case "first":
return 0;
case "second":
return 1;
case "third":
if (options.preset == "he" || options.preset == "she") {
return 2;
} else if (options.preset == "they") {
return 5;
} else if (options.other.plurality == "singular") {
return 2;
} else {
return 5;
}
}
}
replaceAll();