Fixing script erroring when variables are not set, fixes #19

This commit is contained in:
Jean Viscogliosi-Pate 2025-01-26 15:26:57 -08:00
parent d695bb5476
commit 86060a24fc
1 changed files with 71 additions and 75 deletions

View File

@ -1,53 +1,55 @@
const verbsHelper = require('english-verbs-helper');
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if ('input_word' in message){
escapeAndReplace(message.input_word, message.replace_value, message.case_sensitive);
} else {
replaceAll();
}
});
function replaceAll() {
browser.storage.local.get(null, replaceAllInStorage); // TODO this is bad
}
browser.storage.local.get(null, (options) => {
const hostname = window.location.hostname;
const isEnabled = !(options.domains === undefined) && options.domains.includes(hostname);
if (!isEnabled) { return; }
function replaceAllInStorage(options) {
const hostname = window.location.hostname;
if (!options.domains.includes(hostname)) {
return;
}
const isNameSet = !(options.name === undefined || options.name == "");
const isPronounsSet = !(options.preset === undefined || options.preset == "");
const isPovSet = !(options.pov === undefined || options.pov == "");
const isPovLegal = isPovSet && (options.pov != "third" || (isNameSet && isPronounsSet));
if (true) { // change paused later to only care about hostname
replaceVerbs(options);
replaceName(options);
replacePronouns(options);
replacePov(options);
replacePlv(options);
replaceAlso(options);
}
if (isPovLegal) {
replaceVerbs(options);
replacePov(options);
replacePlv(options);
}
if (isNameSet) {
replaceName(options);
}
if (isPronounsSet) {
replacePronouns(options);
}
const isAlsoSet = !options.also === undefined;
if (isAlsoSet) {
replaceAlso(options);
}
});
}
function replaceName(options) {
const regexp = /\by\/n\b|\(y\/n\)|\[y\/n\]/ig;
walk(document.body, regexp, options["name"], directMethod);
const search_term = /\by\/n\b|\(y\/n\)|\[y\/n\]/ig;
walk(document.body, search_term, options["name"], directMethod);
}
function replaceVerbs(options) {
const isPlural = getPronouns(options.preset, options.other).plurality == "plural";
const isPluralThird = isPlural && options.pov == "third";
const isPluralThird = options.pov == "third" && getPronouns(options.preset, options.other).plurality == "plural";
if (isPluralThird) {
const regexp = /([Pp])(?:rn|ov)\/S [Vv]rb\/([\w\s]+)\/([\w\s]+)\/|[Vv]rb\/([\w\s]+)\/([\w\s]+)\/ ([Pp])(?:rn|ov)\/S/;
walk(document.body, regexp, options, pluralThirdVerbMethod);
const search_term = /([Pp])(?:rn|ov)\/S [Vv]rb\/([\w\s]+)\/([\w\s]+)\/|[Vv]rb\/([\w\s]+)\/([\w\s]+)\/ ([Pp])(?:rn|ov)\/S/;
walk(document.body, search_term, options, pluralThirdVerbMethod);
}
const regexp = /vrb\/([\w\s]+)\/([\w\s]+)\//;
walk(document.body, regexp, options, verbMethod);
const search_term = /vrb\/([\w\s]+)\/([\w\s]+)\//;
walk(document.body, search_term, options, verbMethod);
}
// TODO case sensitivity, premade regexp
/* This could be made faster if I give premade regexp expressions for each, but I don't want to do that! */
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"]);
@ -147,31 +149,26 @@ function getPronouns(key, 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"]);
if (options.pov == "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");
} else {
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"]);
@ -181,7 +178,6 @@ function replacePov(options) {
}
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"]);
@ -196,20 +192,20 @@ function replaceAlso(options) {
});
}
function escapeAndReplace(input_word, replace_value, case_sensitive) {
let input_word_escaped = input_word.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
function escapeAndReplace(search_term, replace_value, case_sensitive) {
let search_term_escaped = search_term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
const flags = case_sensitive ? "g" : "ig"
if (input_word_escaped[0].match(/[a-z]/i)) {
input_word_escaped = `\\b${input_word_escaped}`
if (search_term_escaped[0].match(/[a-z]/i)) {
search_term_escaped = `\\b${search_term_escaped}`
}
if (input_word_escaped[input_word_escaped.length - 1].match((/[a-z]/i))) {
input_word_escaped = `${input_word_escaped}\\b`
if (search_term_escaped[search_term_escaped.length - 1].match((/[a-z]/i))) {
search_term_escaped = `${search_term_escaped}\\b`
}
const regexp_input_word = new RegExp(input_word_escaped, flags)
walk(document.body, regexp_input_word, replace_value, directMethod);
const search_term = new RegExp(search_term_escaped, flags)
walk(document.body, search_term, replace_value, directMethod);
}
function walk(node, input_word, replace_value, replaceMethod) {
function walk(node, search_term, replace_value, replaceMethod) {
if (node.contentEditable == "true" || node.type == "textarea" || node.type == "input") { return; }
let child, next;
switch (node.nodeType) {
@ -219,32 +215,32 @@ function walk(node, input_word, replace_value, replaceMethod) {
child = node.firstChild;
while (child) {
next = child.nextSibling;
walk(child, input_word, replace_value, replaceMethod);
walk(child, search_term, replace_value, replaceMethod);
child = next;
}
break;
case 3: /* TEXT_NODE */
replaceMethod(node, input_word, replace_value);
replaceMethod(node, search_term, replace_value);
}
}
function directMethod(node, input_word, replace_value) {
node.nodeValue = node.nodeValue.replace(input_word, replace_value); // TODO might replaceall work better here?;
function directMethod(node, search_term, replace_value) {
node.nodeValue = node.nodeValue.replaceAll(search_term, replace_value);
}
// TODO do something about options being not input_word
function verbMethod(node, regexp, options) {
let match = node.nodeValue.match(regexp);
//TODO are "options" here named comprehensibly
function verbMethod(node, search_term, options) {
let match = node.nodeValue.match(search_term);
if (match == null) { return; }
const verb = match[1];
const tense = match[2].toUpperCase().replaceAll(" ", "_");
const replace_value = verbsHelper.getConjugation(null, verb, tense, getPovIndex(options));
node.nodeValue = node.nodeValue.replace(regexp, replace_value);
verbMethod(node, regexp, options);
node.nodeValue = node.nodeValue.replace(search_term, replace_value);
verbMethod(node, search_term, options);
}
function pluralThirdVerbMethod(node, regexp, options) {
let match = node.nodeValue.match(regexp);
function pluralThirdVerbMethod(node, search_term, options) {
let match = node.nodeValue.match(search_term);
if (match == null) { return; }
const before = match[1];
const after = match[4];
@ -265,8 +261,8 @@ function pluralThirdVerbMethod(node, regexp, options) {
} else {
replace_value = replace_verb + " " + options["name"];
}
node.nodeValue = node.nodeValue.replace(regexp, replace_value);
pluralThirdVerbMethod(node, regexp, options);
node.nodeValue = node.nodeValue.replace(search_term, replace_value);
pluralThirdVerbMethod(node, search_term, options);
}
function capitalize(word) {