Adding function descriptions to replace-words.js
This commit is contained in:
parent
1551f8880e
commit
2754ffafe6
|
@ -3,6 +3,7 @@ const verbsIrregular = require('english-verbs-irregular/dist/verbs.json');
|
||||||
const verbsGerunds = require('english-verbs-gerunds/dist/gerunds.json');
|
const verbsGerunds = require('english-verbs-gerunds/dist/gerunds.json');
|
||||||
const verbsData = verbsHelper.mergeVerbsData(verbsIrregular, verbsGerunds);
|
const verbsData = verbsHelper.mergeVerbsData(verbsIrregular, verbsGerunds);
|
||||||
|
|
||||||
|
/* Replaces verbs, point-of-view, name, pronouns, and "also" terms */
|
||||||
function replaceAll() {
|
function replaceAll() {
|
||||||
browser.storage.local.get(null, (options) => {
|
browser.storage.local.get(null, (options) => {
|
||||||
const hostname = window.location.hostname;
|
const hostname = window.location.hostname;
|
||||||
|
@ -35,11 +36,13 @@ function replaceAll() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Replaces all instances of "Y/n" with the user's name */
|
||||||
function replaceName(options) {
|
function replaceName(options) {
|
||||||
const search_term = /\by\/n\b|\(y\/n\)|\[y\/n\]/ig;
|
const search_term = /\by\/n\b|\(y\/n\)|\[y\/n\]/ig;
|
||||||
walk(document.body, search_term, options["name"], directMethod);
|
walk(document.body, search_term, options["name"], directMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Replaces all verb keys */
|
||||||
function replaceVerbs(options) {
|
function replaceVerbs(options) {
|
||||||
const isPluralThird = options.pov == "third" && getPronouns(options.preset, options.other).plurality == "plural";
|
const isPluralThird = options.pov == "third" && getPronouns(options.preset, options.other).plurality == "plural";
|
||||||
if (isPluralThird) {
|
if (isPluralThird) {
|
||||||
|
@ -51,6 +54,7 @@ function replaceVerbs(options) {
|
||||||
walk(document.body, search_term, options, verbMethod);
|
walk(document.body, search_term, options, verbMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Replaces all pronoun keys */
|
||||||
/* This could be made faster if I give premade regexp expressions for each, but I don't want to do that! */
|
/* 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) {
|
||||||
let pronouns = getPronouns(options.preset, options.other);
|
let pronouns = getPronouns(options.preset, options.other);
|
||||||
|
@ -65,6 +69,7 @@ function replacePronouns(options) {
|
||||||
replacePronounSet("Prn/n", "prn/n", pronouns["noun-child"]);
|
replacePronounSet("Prn/n", "prn/n", pronouns["noun-child"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Gets pronouns based on the provided key (or user input) */
|
||||||
function getPronouns(key, other) {
|
function getPronouns(key, other) {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case "she":
|
case "she":
|
||||||
|
@ -151,11 +156,13 @@ function getPronouns(key, other) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Replaces pronoun keys in capital and lowercase with their equivalent pronouns */
|
||||||
function replacePronounSet(keyCapital, key, pronoun) {
|
function replacePronounSet(keyCapital, key, pronoun) {
|
||||||
escapeAndReplace(keyCapital, capitalize(pronoun), true);
|
escapeAndReplace(keyCapital, capitalize(pronoun), true);
|
||||||
escapeAndReplace(key, pronoun, true);
|
escapeAndReplace(key, pronoun, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Replaces all point-of-view keys */
|
||||||
function replacePov(options) {
|
function replacePov(options) {
|
||||||
let pronouns;
|
let pronouns;
|
||||||
if (options.pov == "third") {
|
if (options.pov == "third") {
|
||||||
|
@ -180,6 +187,7 @@ function replacePov(options) {
|
||||||
replacePronounSet("Pov/r", "pov/r", pronouns["reflexive"]);
|
replacePronounSet("Pov/r", "pov/r", pronouns["reflexive"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Replaces plural point-of-view keys */
|
||||||
function replacePlv(options) {
|
function replacePlv(options) {
|
||||||
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"]);
|
||||||
|
@ -189,12 +197,14 @@ function replacePlv(options) {
|
||||||
replacePronounSet("Plv/r", "plv/r", pronouns["reflexive"]);
|
replacePronounSet("Plv/r", "plv/r", pronouns["reflexive"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Replaces additional terms specified by the user */
|
||||||
function replaceAlso(options) {
|
function replaceAlso(options) {
|
||||||
Object.entries(options.also).forEach(([key, value]) => {
|
Object.entries(options.also).forEach(([key, value]) => {
|
||||||
escapeAndReplace(key, value, true);
|
escapeAndReplace(key, value, true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Turns a term into regexp format and uses that to replace it */
|
||||||
function escapeAndReplace(search_term, replace_value, case_sensitive) {
|
function escapeAndReplace(search_term, replace_value, case_sensitive) {
|
||||||
let search_term_escaped = search_term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
let search_term_escaped = search_term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
||||||
const flags = case_sensitive ? "g" : "ig"
|
const flags = case_sensitive ? "g" : "ig"
|
||||||
|
@ -208,6 +218,7 @@ function escapeAndReplace(search_term, replace_value, case_sensitive) {
|
||||||
walk(document.body, search_term_regexp, replace_value, directMethod);
|
walk(document.body, search_term_regexp, replace_value, directMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Traverses a node and its children to run the provided replacement method */
|
||||||
function walk(node, search_term, 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;
|
||||||
|
@ -227,11 +238,13 @@ function walk(node, search_term, replace_value, replaceMethod) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Replaces all of the provided terms in a node */
|
||||||
function directMethod(node, search_term, replace_value) {
|
function directMethod(node, search_term, replace_value) {
|
||||||
node.nodeValue = node.nodeValue.replaceAll(search_term, replace_value);
|
node.nodeValue = node.nodeValue.replaceAll(search_term, replace_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO are "options" here named comprehensibly
|
/* Replaces all verbs, not used for third-person plural */
|
||||||
|
/* TODO Is the "options" variable here readable? It's not replace_value and that seems bad */
|
||||||
function verbMethod(node, search_term, options) {
|
function verbMethod(node, search_term, options) {
|
||||||
let match = node.nodeValue.match(search_term);
|
let match = node.nodeValue.match(search_term);
|
||||||
if (match == null) { return; }
|
if (match == null) { return; }
|
||||||
|
@ -242,6 +255,8 @@ function verbMethod(node, search_term, options) {
|
||||||
verbMethod(node, search_term, options);
|
verbMethod(node, search_term, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Replaces all verbs, used only for third-person plural */
|
||||||
|
/* TODO This is meant to be able to replace in both subject-object and object-subject order, but it's only the first for now. */
|
||||||
function pluralThirdVerbMethod(node, search_term, options) {
|
function pluralThirdVerbMethod(node, search_term, options) {
|
||||||
let match = node.nodeValue.match(search_term);
|
let match = node.nodeValue.match(search_term);
|
||||||
if (match == null) { return; }
|
if (match == null) { return; }
|
||||||
|
@ -268,11 +283,13 @@ function pluralThirdVerbMethod(node, search_term, options) {
|
||||||
pluralThirdVerbMethod(node, search_term, options);
|
pluralThirdVerbMethod(node, search_term, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Returns the input word, capitalized */
|
||||||
function capitalize(word) {
|
function capitalize(word) {
|
||||||
return word.charAt(0).toUpperCase() + word.slice(1);
|
return word.charAt(0).toUpperCase() + word.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO figure out plv for this
|
/* Determines the index for point-of-view to be used when conjugating verbs */
|
||||||
|
/* TODO Did I ever implement third-person plural? */
|
||||||
function getPovIndex(options) {
|
function getPovIndex(options) {
|
||||||
switch (options.pov) {
|
switch (options.pov) {
|
||||||
case "first":
|
case "first":
|
||||||
|
|
Loading…
Reference in New Issue