From 4e16628ee8e73d4b7785ff72a7a2497aeabae9e0 Mon Sep 17 00:00:00 2001 From: Mariam Maarouf Date: Thu, 10 Mar 2016 14:12:35 +0200 Subject: [PATCH] ability to change more words, resolves #6 --- content_script.js | 56 ++++++++++++++++++++++------------------------- manifest.json | 2 +- popup.html | 39 ++++++++++++++++++++++++++++++++- popup.js | 32 ++++++++++++++++++--------- 4 files changed, 87 insertions(+), 42 deletions(-) diff --git a/content_script.js b/content_script.js index f6f2660..06be922 100644 --- a/content_script.js +++ b/content_script.js @@ -1,65 +1,61 @@ 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){ //This is where the stuff you want from the background page will be - valChange = new RegExp(message.stuff, "ig"); - loadReplace(); + var val = new RegExp(message.stuff, "ig"); + if(message.isYN) + loadReplace(val, person); + else + if(message.replaceVal){ + loadReplace(val, message.replaceVal); + } }); -var person; -loadReplace(); - -function loadReplace() -{ -chrome.storage.local.get("person", function(value) -{ -person = value.person; -if(person != null){walk(document.body);} - -} -) +function loadReplace(rep, p){ + if(p!=null) + walk(document.body, rep, p); } -function walk(node) -{ +function walk(node, v, p){ // I stole this function from here: // http://is.gd/mwZp7E - var child, next; - - switch ( node.nodeType ) - { + switch (node.nodeType){ case 1: // Element case 9: // Document case 11: // Document fragment child = node.firstChild; - while ( child ) - { + while (child){ next = child.nextSibling; - walk(child); + walk(child, v, p); child = next; } break; - case 3: // Text node - handleText(node); + handleText(node, v, p); break; } } -function handleText(textNode) -{ +function handleText(textNode, val, p){ 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; } MutationObserver = window.MutationObserver || window.WebKitMutationObserver; var observer = new MutationObserver(function(mutations, observer) { - loadReplace(); + loadReplace(valChange, person); }); // define what element should be observed by the observer diff --git a/manifest.json b/manifest.json index 566d836..8abb601 100644 --- a/manifest.json +++ b/manifest.json @@ -3,7 +3,7 @@ "manifest_version": 2, "name": "InteractiveFics", - "version": "3.0", + "version": "4.0", "description": "Replaces Y/N in Reader Insert/second person fics with a name of your choice.", "browser_action": { "default_icon": "icon.png", diff --git a/popup.html b/popup.html index 0b5a335..15c6824 100644 --- a/popup.html +++ b/popup.html @@ -20,6 +20,29 @@ a:hover{ 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; +} +