2020-10-18 09:26:41 -04:00
|
|
|
PAUSED_KEY = 'pause-this-domain-pls-interactive-fics-yalla-bina';
|
2020-09-26 12:03:23 -04:00
|
|
|
|
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)
|
|
|
|
|
} else {
|
|
|
|
|
replaceAll()
|
|
|
|
|
}
|
2019-10-19 05:27:27 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const replaceAll = () => {
|
2024-12-23 05:58:48 -05:00
|
|
|
browser.storage.local.get(null, replaceAllInStorage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const replaceAllInStorage = (options) => {
|
|
|
|
|
const hostname = window.location.hostname
|
|
|
|
|
const is_paused = options[PAUSED_KEY] && options[PAUSED_KEY].indexOf(hostname) !== -1
|
|
|
|
|
if (true) { // change paused later to only care about hostname
|
|
|
|
|
replaceName(options);
|
|
|
|
|
replacePronouns(options);
|
|
|
|
|
replacePov(options);
|
|
|
|
|
replacePlv(options);
|
|
|
|
|
replaceAlso(options);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function replaceName(options) {
|
|
|
|
|
const regexp = /\by\/n\b|\(y\/n\)|\[y\/n\]/ig;
|
|
|
|
|
replace(regexp, options["name"]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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"]);
|
2020-09-26 12:03:23 -04:00
|
|
|
}
|
|
|
|
|
|
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 = escapeRegExp(input_word.trim())
|
|
|
|
|
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)
|
|
|
|
|
replace(regexp_input_word, replace_value)
|
2016-06-18 23:04:38 -04:00
|
|
|
}
|
2015-07-31 06:58:38 -04:00
|
|
|
|
2019-10-19 05:27:27 -04:00
|
|
|
const escapeRegExp = (str) => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")
|
2015-02-21 22:00:28 -05:00
|
|
|
|
2019-10-19 05:27:27 -04:00
|
|
|
const replace = (input_word, replace_value) => {
|
2024-12-23 05:58:48 -05:00
|
|
|
browser.storage.local.get(obj => {
|
|
|
|
|
if (replace_value) {
|
|
|
|
|
walk(document.body, input_word, replace_value)
|
|
|
|
|
}
|
|
|
|
|
})
|
2019-10-19 05:27:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const replaceText = (textNode, input_word, replace_value) => {
|
2024-12-23 05:58:48 -05:00
|
|
|
let node_value = textNode.nodeValue
|
|
|
|
|
node_value = node_value.replace(input_word, replace_value)
|
|
|
|
|
textNode.nodeValue = node_value
|
2015-02-21 22:00:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-09-26 12:03:23 -04:00
|
|
|
|
2016-03-10 07:12:35 -05:00
|
|
|
function walk(node, v, p){
|
2024-12-23 05:58:48 -05:00
|
|
|
// I stole the base to this function from here:
|
|
|
|
|
// https://stackoverflow.com/questions/5904914/javascript-regex-to-replace-text-not-in-html-attributes/5904945
|
|
|
|
|
if (node.contentEditable != 'true' && node.type != 'textarea' && node.type != 'input') {
|
|
|
|
|
var child, next;
|
|
|
|
|
switch (node.nodeType){
|
|
|
|
|
case 1: // Element
|
|
|
|
|
case 9: // Document
|
|
|
|
|
case 11: // Document fragment
|
|
|
|
|
child = node.firstChild;
|
|
|
|
|
while (child){
|
|
|
|
|
next = child.nextSibling;
|
|
|
|
|
walk(child, v, p);
|
|
|
|
|
child = next;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 3: // Text node
|
|
|
|
|
replaceText(node, v, p);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function capitalize(word) {
|
|
|
|
|
return word.charAt(0).toUpperCase() + word.slice(1);
|
2015-02-21 22:00:28 -05:00
|
|
|
}
|
|
|
|
|
|
2019-10-19 05:27:27 -04:00
|
|
|
replaceAll()
|