ability to change more words, resolves #6
This commit is contained in:
parent
703ea18bfd
commit
4e16628ee8
|
@ -1,65 +1,61 @@
|
||||||
var valChange = /\by\/n\b|\(y\/n\)|\[y\/n\]/ig;
|
var valChange = /\by\/n\b|\(y\/n\)|\[y\/n\]/ig;
|
||||||
|
var person;
|
||||||
|
chrome.storage.local.get("person", function(value){
|
||||||
|
person = value.person;
|
||||||
|
if(person){
|
||||||
|
loadReplace(valChange, person);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// loadReplace(valChange);
|
||||||
|
|
||||||
chrome.extension.onMessage.addListener(function(message,sender,sendResponse){
|
chrome.extension.onMessage.addListener(function(message,sender,sendResponse){
|
||||||
//This is where the stuff you want from the background page will be
|
//This is where the stuff you want from the background page will be
|
||||||
valChange = new RegExp(message.stuff, "ig");
|
var val = new RegExp(message.stuff, "ig");
|
||||||
loadReplace();
|
if(message.isYN)
|
||||||
|
loadReplace(val, person);
|
||||||
|
else
|
||||||
|
if(message.replaceVal){
|
||||||
|
loadReplace(val, message.replaceVal);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var person;
|
|
||||||
|
|
||||||
loadReplace();
|
function loadReplace(rep, p){
|
||||||
|
if(p!=null)
|
||||||
function loadReplace()
|
walk(document.body, rep, p);
|
||||||
{
|
|
||||||
chrome.storage.local.get("person", function(value)
|
|
||||||
{
|
|
||||||
person = value.person;
|
|
||||||
if(person != null){walk(document.body);}
|
|
||||||
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function walk(node)
|
function walk(node, v, p){
|
||||||
{
|
|
||||||
// I stole this function from here:
|
// I stole this function from here:
|
||||||
// http://is.gd/mwZp7E
|
// http://is.gd/mwZp7E
|
||||||
|
|
||||||
var child, next;
|
var child, next;
|
||||||
|
switch (node.nodeType){
|
||||||
switch ( node.nodeType )
|
|
||||||
{
|
|
||||||
case 1: // Element
|
case 1: // Element
|
||||||
case 9: // Document
|
case 9: // Document
|
||||||
case 11: // Document fragment
|
case 11: // Document fragment
|
||||||
child = node.firstChild;
|
child = node.firstChild;
|
||||||
while ( child )
|
while (child){
|
||||||
{
|
|
||||||
next = child.nextSibling;
|
next = child.nextSibling;
|
||||||
walk(child);
|
walk(child, v, p);
|
||||||
child = next;
|
child = next;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3: // Text node
|
case 3: // Text node
|
||||||
handleText(node);
|
handleText(node, v, p);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleText(textNode)
|
function handleText(textNode, val, p){
|
||||||
{
|
|
||||||
var v = textNode.nodeValue;
|
var v = textNode.nodeValue;
|
||||||
v = v.replace(valChange, person); //replaces Y/N or other value entered regardless of the case, whether it's in a bracket or not
|
v = v.replace(val, p); //replaces Y/N or other value entered regardless of the case, whether it's in a bracket or not
|
||||||
|
|
||||||
textNode.nodeValue = v;
|
textNode.nodeValue = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
|
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
|
||||||
|
|
||||||
var observer = new MutationObserver(function(mutations, observer) {
|
var observer = new MutationObserver(function(mutations, observer) {
|
||||||
loadReplace();
|
loadReplace(valChange, person);
|
||||||
});
|
});
|
||||||
|
|
||||||
// define what element should be observed by the observer
|
// define what element should be observed by the observer
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
"manifest_version": 2,
|
"manifest_version": 2,
|
||||||
"name": "InteractiveFics",
|
"name": "InteractiveFics",
|
||||||
"version": "3.0",
|
"version": "4.0",
|
||||||
"description": "Replaces Y/N in Reader Insert/second person fics with a name of your choice.",
|
"description": "Replaces Y/N in Reader Insert/second person fics with a name of your choice.",
|
||||||
"browser_action": {
|
"browser_action": {
|
||||||
"default_icon": "icon.png",
|
"default_icon": "icon.png",
|
||||||
|
|
39
popup.html
39
popup.html
|
@ -20,6 +20,29 @@ a:hover{
|
||||||
color: #16a085;
|
color: #16a085;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
label *{
|
||||||
|
display:block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.otherWords input{
|
||||||
|
margin-top:0.2em;
|
||||||
|
width:100%;
|
||||||
|
}
|
||||||
|
.otherWords .other, .otherWords .replaceBy{
|
||||||
|
width:98%;
|
||||||
|
}
|
||||||
|
.otherWords .change{
|
||||||
|
margin-right:0;
|
||||||
|
}
|
||||||
|
|
||||||
|
button{
|
||||||
|
border:none;
|
||||||
|
text-transform: uppercase;
|
||||||
|
font-size:0.8em;
|
||||||
|
background-color: #16a085;
|
||||||
|
color:white;
|
||||||
|
}
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
<script src="popup.js" type="text/javascript" >
|
<script src="popup.js" type="text/javascript" >
|
||||||
|
|
||||||
|
@ -32,7 +55,21 @@ color: #16a085;
|
||||||
<p><details><summary>Is the author not using Y/N?</summary>
|
<p><details><summary>Is the author not using Y/N?</summary>
|
||||||
<p>Enter the expression the author's using instead:</p>
|
<p>Enter the expression the author's using instead:</p>
|
||||||
<p><small>(Make sure you already entered a name above. This change will go away if you refresh, so you'll have to enter it again)</small></p>
|
<p><small>(Make sure you already entered a name above. This change will go away if you refresh, so you'll have to enter it again)</small></p>
|
||||||
<p><form><input type="text" id="other"><input type="submit" id="change" value="Enter"></form></p>
|
<p><form class="changeForm"><input type="text" name="replaceWord" class="other">
|
||||||
|
<input type="submit" class="change" value="Enter"></form></p>
|
||||||
|
</details></p>
|
||||||
|
<p><details id="moreWords"><summary>Need to replace something else?</summary>
|
||||||
|
<p><small>(This change will go away when you refresh/go to another page)</small></p>
|
||||||
|
|
||||||
|
<p><form class="otherWords changeForm">
|
||||||
|
<label><span>Value [e.g L/N, E/C, etc]:</span>
|
||||||
|
<input type="text" name="replaceWord" class="other"></label>
|
||||||
|
<br>
|
||||||
|
<label><span>Replace with:</span>
|
||||||
|
<input type="text" name="replaceWith" class="replaceBy"></label>
|
||||||
|
<input type="submit" class="change" value="Change">
|
||||||
|
</form>
|
||||||
|
</p>
|
||||||
</details></p>
|
</details></p>
|
||||||
<p>
|
<p>
|
||||||
<details><summary>About</summary>
|
<details><summary>About</summary>
|
||||||
|
|
32
popup.js
32
popup.js
|
@ -1,18 +1,29 @@
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', function () {
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
document.getElementById("submit").addEventListener('click', clickHandler);
|
document.getElementById('submit').addEventListener('click', clickHandler);
|
||||||
|
var others = document.getElementsByClassName('changeForm');
|
||||||
|
for(var i=0; i<others.length; i++)
|
||||||
|
others[i].addEventListener('submit', otherHandler);
|
||||||
}); //instead of onclick="clickHandler()" in popup.html
|
}); //instead of onclick="clickHandler()" in popup.html
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', function(){
|
|
||||||
document.getElementById("change").addEventListener('click', otherHandler);
|
|
||||||
}); //instead of onclick="otherHandler()" in popup.html
|
|
||||||
|
|
||||||
function otherHandler(){
|
function otherHandler(){
|
||||||
var myInput = document.getElementById("other").value;
|
var myInput = this.replaceWord.value;
|
||||||
|
if(myInput){
|
||||||
var valChange = escapeRegExp(myInput);
|
var valChange = escapeRegExp(myInput);
|
||||||
chrome.tabs.query({active:true,currentWindow:true}, function(tab) {
|
var isYN;
|
||||||
chrome.tabs.sendMessage(tab[0].id, {stuff:valChange});
|
var replace;
|
||||||
});
|
if(this.replaceWith){
|
||||||
|
isYN = false;
|
||||||
|
replace = this.replaceWith.value;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
isYN = true;
|
||||||
|
replace = 'NOPE'; // should/will never get accessed
|
||||||
|
}
|
||||||
|
chrome.tabs.query({active:true,currentWindow:true}, function(tab){
|
||||||
|
chrome.tabs.sendMessage(tab[0].id, {stuff:valChange, isYN: isYN, replaceVal:replace});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function escapeRegExp(str) {
|
function escapeRegExp(str) {
|
||||||
|
@ -22,7 +33,8 @@ function escapeRegExp(str) {
|
||||||
|
|
||||||
function clickHandler(){
|
function clickHandler(){
|
||||||
var person = document.getElementById("inputTxt").value;
|
var person = document.getElementById("inputTxt").value;
|
||||||
chrome.storage.local.set({"person": person}, chrome.tabs.reload());
|
if(person)
|
||||||
|
chrome.storage.local.set({"person": person}, chrome.tabs.reload());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue