Fixing script erroring when variables are not set, fixes #19
This commit is contained in:
parent
d695bb5476
commit
86060a24fc
|
@ -1,53 +1,55 @@
|
||||||
const verbsHelper = require('english-verbs-helper');
|
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() {
|
function replaceAll() {
|
||||||
browser.storage.local.get(null, replaceAllInStorage); // TODO this is bad
|
browser.storage.local.get(null, (options) => {
|
||||||
}
|
|
||||||
|
|
||||||
function replaceAllInStorage(options) {
|
|
||||||
const hostname = window.location.hostname;
|
const hostname = window.location.hostname;
|
||||||
if (!options.domains.includes(hostname)) {
|
const isEnabled = !(options.domains === undefined) && options.domains.includes(hostname);
|
||||||
return;
|
if (!isEnabled) { return; }
|
||||||
}
|
|
||||||
|
|
||||||
if (true) { // change paused later to only care about hostname
|
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 (isPovLegal) {
|
||||||
replaceVerbs(options);
|
replaceVerbs(options);
|
||||||
replaceName(options);
|
|
||||||
replacePronouns(options);
|
|
||||||
replacePov(options);
|
replacePov(options);
|
||||||
replacePlv(options);
|
replacePlv(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isNameSet) {
|
||||||
|
replaceName(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isPronounsSet) {
|
||||||
|
replacePronouns(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
const isAlsoSet = !options.also === undefined;
|
||||||
|
if (isAlsoSet) {
|
||||||
replaceAlso(options);
|
replaceAlso(options);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function replaceName(options) {
|
function replaceName(options) {
|
||||||
const regexp = /\by\/n\b|\(y\/n\)|\[y\/n\]/ig;
|
const search_term = /\by\/n\b|\(y\/n\)|\[y\/n\]/ig;
|
||||||
walk(document.body, regexp, options["name"], directMethod);
|
walk(document.body, search_term, options["name"], directMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
function replaceVerbs(options) {
|
function replaceVerbs(options) {
|
||||||
const isPlural = getPronouns(options.preset, options.other).plurality == "plural";
|
const isPluralThird = options.pov == "third" && getPronouns(options.preset, options.other).plurality == "plural";
|
||||||
const isPluralThird = isPlural && options.pov == "third";
|
|
||||||
if (isPluralThird) {
|
if (isPluralThird) {
|
||||||
const regexp = /([Pp])(?:rn|ov)\/S [Vv]rb\/([\w\s]+)\/([\w\s]+)\/|[Vv]rb\/([\w\s]+)\/([\w\s]+)\/ ([Pp])(?:rn|ov)\/S/;
|
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, regexp, options, pluralThirdVerbMethod);
|
walk(document.body, search_term, options, pluralThirdVerbMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
const regexp = /vrb\/([\w\s]+)\/([\w\s]+)\//;
|
const search_term = /vrb\/([\w\s]+)\/([\w\s]+)\//;
|
||||||
walk(document.body, regexp, options, verbMethod);
|
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) {
|
function replacePronouns(options) {
|
||||||
if (options.preset == "") { return; }
|
|
||||||
let pronouns = getPronouns(options.preset, options.other);
|
let pronouns = getPronouns(options.preset, options.other);
|
||||||
replacePronounSet("Prn/s", "prn/s", pronouns["subjective"]);
|
replacePronounSet("Prn/s", "prn/s", pronouns["subjective"]);
|
||||||
replacePronounSet("Prn/o", "prn/o", pronouns["objective"]);
|
replacePronounSet("Prn/o", "prn/o", pronouns["objective"]);
|
||||||
|
@ -147,25 +149,20 @@ function getPronouns(key, other) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function replacePronounSet(keyCapital, key, pronoun) {
|
function replacePronounSet(keyCapital, key, pronoun) {
|
||||||
if (pronoun === undefined) { return; }
|
|
||||||
escapeAndReplace(keyCapital, capitalize(pronoun), true);
|
escapeAndReplace(keyCapital, capitalize(pronoun), true);
|
||||||
escapeAndReplace(key, pronoun, true);
|
escapeAndReplace(key, pronoun, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function replacePov(options) {
|
function replacePov(options) {
|
||||||
let pronouns;
|
let pronouns;
|
||||||
switch (options.pov) {
|
if (options.pov == "third") {
|
||||||
case "":
|
|
||||||
return;
|
|
||||||
case "third":
|
|
||||||
pronouns = getPronouns(options.preset, options.other);
|
pronouns = getPronouns(options.preset, options.other);
|
||||||
replacePronounSet("Pov/S", "pov/S", options.name);
|
replacePronounSet("Pov/S", "pov/S", options.name);
|
||||||
replacePronounSet("Pov/O", "pov/O", options.name);
|
replacePronounSet("Pov/O", "pov/O", options.name);
|
||||||
replacePronounSet("Pov/P", "pov/P", options.name + "'s");
|
replacePronounSet("Pov/P", "pov/P", options.name + "'s");
|
||||||
replacePronounSet("Pov/A", "pov/A", options.name + "'s");
|
replacePronounSet("Pov/A", "pov/A", options.name + "'s");
|
||||||
replacePronounSet("Pov/R", "pov/R", options.name + "'s self");
|
replacePronounSet("Pov/R", "pov/R", options.name + "'s self");
|
||||||
break;
|
} else {
|
||||||
default:
|
|
||||||
pronouns = getPronouns(options.pov, null);
|
pronouns = getPronouns(options.pov, null);
|
||||||
replacePronounSet("Pov/S", "pov/S", pronouns["subjective"]);
|
replacePronounSet("Pov/S", "pov/S", pronouns["subjective"]);
|
||||||
replacePronounSet("Pov/O", "pov/O", pronouns["objective"]);
|
replacePronounSet("Pov/O", "pov/O", pronouns["objective"]);
|
||||||
|
@ -181,7 +178,6 @@ function replacePov(options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function replacePlv(options) {
|
function replacePlv(options) {
|
||||||
if (options.pov == "") { return; }
|
|
||||||
let pronouns = getPronouns(options.pov + "-plural", null);
|
let pronouns = getPronouns(options.pov + "-plural", null);
|
||||||
replacePronounSet("Plv/s", "plv/s", pronouns["subjective"]);
|
replacePronounSet("Plv/s", "plv/s", pronouns["subjective"]);
|
||||||
replacePronounSet("Plv/o", "plv/o", pronouns["objective"]);
|
replacePronounSet("Plv/o", "plv/o", pronouns["objective"]);
|
||||||
|
@ -196,20 +192,20 @@ function replaceAlso(options) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function escapeAndReplace(input_word, replace_value, case_sensitive) {
|
function escapeAndReplace(search_term, replace_value, case_sensitive) {
|
||||||
let input_word_escaped = input_word.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
let search_term_escaped = search_term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
||||||
const flags = case_sensitive ? "g" : "ig"
|
const flags = case_sensitive ? "g" : "ig"
|
||||||
if (input_word_escaped[0].match(/[a-z]/i)) {
|
if (search_term_escaped[0].match(/[a-z]/i)) {
|
||||||
input_word_escaped = `\\b${input_word_escaped}`
|
search_term_escaped = `\\b${search_term_escaped}`
|
||||||
}
|
}
|
||||||
if (input_word_escaped[input_word_escaped.length - 1].match((/[a-z]/i))) {
|
if (search_term_escaped[search_term_escaped.length - 1].match((/[a-z]/i))) {
|
||||||
input_word_escaped = `${input_word_escaped}\\b`
|
search_term_escaped = `${search_term_escaped}\\b`
|
||||||
}
|
}
|
||||||
const regexp_input_word = new RegExp(input_word_escaped, flags)
|
const search_term = new RegExp(search_term_escaped, flags)
|
||||||
walk(document.body, regexp_input_word, replace_value, directMethod);
|
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; }
|
if (node.contentEditable == "true" || node.type == "textarea" || node.type == "input") { return; }
|
||||||
let child, next;
|
let child, next;
|
||||||
switch (node.nodeType) {
|
switch (node.nodeType) {
|
||||||
|
@ -219,32 +215,32 @@ function walk(node, input_word, replace_value, replaceMethod) {
|
||||||
child = node.firstChild;
|
child = node.firstChild;
|
||||||
while (child) {
|
while (child) {
|
||||||
next = child.nextSibling;
|
next = child.nextSibling;
|
||||||
walk(child, input_word, replace_value, replaceMethod);
|
walk(child, search_term, replace_value, replaceMethod);
|
||||||
child = next;
|
child = next;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 3: /* TEXT_NODE */
|
case 3: /* TEXT_NODE */
|
||||||
replaceMethod(node, input_word, replace_value);
|
replaceMethod(node, search_term, replace_value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function directMethod(node, input_word, replace_value) {
|
function directMethod(node, search_term, replace_value) {
|
||||||
node.nodeValue = node.nodeValue.replace(input_word, replace_value); // TODO might replaceall work better here?;
|
node.nodeValue = node.nodeValue.replaceAll(search_term, replace_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO do something about options being not input_word
|
//TODO are "options" here named comprehensibly
|
||||||
function verbMethod(node, regexp, options) {
|
function verbMethod(node, search_term, options) {
|
||||||
let match = node.nodeValue.match(regexp);
|
let match = node.nodeValue.match(search_term);
|
||||||
if (match == null) { return; }
|
if (match == null) { return; }
|
||||||
const verb = match[1];
|
const verb = match[1];
|
||||||
const tense = match[2].toUpperCase().replaceAll(" ", "_");
|
const tense = match[2].toUpperCase().replaceAll(" ", "_");
|
||||||
const replace_value = verbsHelper.getConjugation(null, verb, tense, getPovIndex(options));
|
const replace_value = verbsHelper.getConjugation(null, verb, tense, getPovIndex(options));
|
||||||
node.nodeValue = node.nodeValue.replace(regexp, replace_value);
|
node.nodeValue = node.nodeValue.replace(search_term, replace_value);
|
||||||
verbMethod(node, regexp, options);
|
verbMethod(node, search_term, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
function pluralThirdVerbMethod(node, regexp, options) {
|
function pluralThirdVerbMethod(node, search_term, options) {
|
||||||
let match = node.nodeValue.match(regexp);
|
let match = node.nodeValue.match(search_term);
|
||||||
if (match == null) { return; }
|
if (match == null) { return; }
|
||||||
const before = match[1];
|
const before = match[1];
|
||||||
const after = match[4];
|
const after = match[4];
|
||||||
|
@ -265,8 +261,8 @@ function pluralThirdVerbMethod(node, regexp, options) {
|
||||||
} else {
|
} else {
|
||||||
replace_value = replace_verb + " " + options["name"];
|
replace_value = replace_verb + " " + options["name"];
|
||||||
}
|
}
|
||||||
node.nodeValue = node.nodeValue.replace(regexp, replace_value);
|
node.nodeValue = node.nodeValue.replace(search_term, replace_value);
|
||||||
pluralThirdVerbMethod(node, regexp, options);
|
pluralThirdVerbMethod(node, search_term, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
function capitalize(word) {
|
function capitalize(word) {
|
||||||
|
|
Loading…
Reference in New Issue