diff --git a/src/js/content.js b/src/js/content.js index 9779667..6b27892 100644 --- a/src/js/content.js +++ b/src/js/content.js @@ -15,13 +15,13 @@ var DEFAULT_CASE_INSENSITIVE = false; /*** VARIABLES ***/ var searchInfo; /*** VARIABLES ***/ - + /*** LIBRARY FUNCTIONS ***/ Element.prototype.documentOffsetTop = function () { return this.offsetTop + ( this.offsetParent ? this.offsetParent.documentOffsetTop() : 0 ); }; Element.prototype.visible = function() { - return (!window.getComputedStyle(this) || window.getComputedStyle(this).getPropertyValue('display') == '' || + return (!window.getComputedStyle(this) || window.getComputedStyle(this).getPropertyValue('display') == '' || window.getComputedStyle(this).getPropertyValue('display') != 'none') } /*** LIBRARY FUNCTIONS ***/ @@ -57,7 +57,7 @@ function isTextNode(node) { /* Check if the given node is an expandable node that will yield text nodes */ function isExpandable(node) { - return node && node.nodeType === ELEMENT_NODE_TYPE && node.childNodes && + return node && node.nodeType === ELEMENT_NODE_TYPE && node.childNodes && !UNEXPANDABLE.test(node.tagName) && node.visible(); } @@ -73,7 +73,7 @@ function highlight(regex, highlightColor, selectedColor, textColor, maxResults) var matchedText = node.data.match(regex)[0]; var matchedTextNode = node.splitText(index); matchedTextNode.splitText(matchedText.length); - var spanNode = document.createElement(HIGHLIGHT_TAG); + var spanNode = document.createElement(HIGHLIGHT_TAG); spanNode.className = HIGHLIGHT_CLASS; spanNode.style.backgroundColor = highlightColor; spanNode.style.color = textColor; @@ -107,7 +107,7 @@ function removeHighlight() { /* Scroll page to given element */ function scrollToElement(element) { - element.scrollIntoView(); + element.scrollIntoView(); var top = element.documentOffsetTop() - ( window.innerHeight / 2 ); window.scrollTo( 0, Math.max(top, window.pageYOffset - (window.innerHeight/2))) ; } @@ -136,13 +136,13 @@ function selectNode(highlightedColor, selectedColor, getNext) { searchInfo.highlightedNodes[searchInfo.selectedIndex].style.backgroundColor = highlightedColor; if(getNext) { if(searchInfo.selectedIndex === length - 1) { - searchInfo.selectedIndex = 0; + searchInfo.selectedIndex = 0; } else { searchInfo.selectedIndex += 1; } } else { if(searchInfo.selectedIndex === 0) { - searchInfo.selectedIndex = length - 1; + searchInfo.selectedIndex = length - 1; } else { searchInfo.selectedIndex -= 1; } @@ -161,7 +161,7 @@ function selectNode(highlightedColor, selectedColor, getNext) { } /* Forward cycle through regex matched elements */ function selectNextNode(highlightedColor, selectedColor) { - selectNode(highlightedColor, selectedColor, true); + selectNode(highlightedColor, selectedColor, true); } /* Backward cycle through regex matched elements */ @@ -189,7 +189,7 @@ function search(regexString, configurationChanged) { 'selectedColor' : DEFAULT_SELECTED_COLOR, 'textColor' : DEFAULT_TEXT_COLOR, 'maxResults' : DEFAULT_MAX_RESULTS, - 'caseInsensitive' : DEFAULT_CASE_INSENSITIVE}, + 'caseInsensitive' : DEFAULT_CASE_INSENSITIVE}, function(result) { initSearchInfo(regexString); if(result.caseInsensitive){ @@ -203,7 +203,7 @@ function search(regexString, configurationChanged) { } else if (regex && regexString != '' && regexString === searchInfo.regexString) { // elements are already highlighted chrome.storage.local.get({ 'highlightColor' : DEFAULT_HIGHLIGHT_COLOR, - 'selectedColor' : DEFAULT_SELECTED_COLOR}, + 'selectedColor' : DEFAULT_SELECTED_COLOR}, function(result) { selectNextNode(result.highlightColor, result.selectedColor); } @@ -227,7 +227,7 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { chrome.storage.local.get({ 'highlightColor' : DEFAULT_HIGHLIGHT_COLOR, 'selectedColor' : DEFAULT_SELECTED_COLOR - }, + }, function(result) { selectNextNode(result.highlightColor, result.selectedColor); } @@ -238,7 +238,7 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { chrome.storage.local.get({ 'highlightColor' : DEFAULT_HIGHLIGHT_COLOR, 'selectedColor' : DEFAULT_SELECTED_COLOR - }, + }, function(result) { selectPrevNode(result.highlightColor, result.selectedColor); } diff --git a/src/js/options.js b/src/js/options.js index 2bfc891..544fa5a 100644 --- a/src/js/options.js +++ b/src/js/options.js @@ -8,6 +8,7 @@ var WHITE_COLOR = '#ffffff'; var ERROR_COLOR = '#ff8989'; var GOOD_COLOR = '#89ff89'; var DEFAULT_INSTANT_RESULTS = true; +var DEFAULT_KEEP_LAST_SEARCH = true; /*** CONSTANTS ***/ /*** FUNCTIONS ***/ @@ -18,7 +19,7 @@ function markStatus(text, time){ status.textContent = text; setTimeout(function() { status.textContent = ''; - }, time); + }, time); } /* Validate input for max results */ @@ -55,9 +56,10 @@ function saveOptions() { 'textColor' : document.getElementById('textColor').value, 'maxResults' : maxResults, 'instantResults' : document.getElementById('instantResults').checked, + 'keepLastSearch' : document.getElementById('keepLastSearch').checked, 'maxHistoryLength' : document.getElementById('maxHistoryLength').value } - + chrome.storage.local.set(options, function() { markStatus('New settings saved'); }); @@ -72,7 +74,8 @@ function loadOptions() { 'textColor' : DEFAULT_TEXT_COLOR, 'maxResults' : DEFAULT_MAX_RESULTS, 'instantResults' : DEFAULT_INSTANT_RESULTS, - 'maxHistoryLength' : DEFAULT_MAX_HISTORY_LENGTH }, + 'keepLastSearch' : DEFAULT_KEEP_LAST_SEARCH, + 'maxHistoryLength' : DEFAULT_MAX_HISTORY_LENGTH }, function(result) { document.getElementById('highlightColor').value = result.highlightColor; document.getElementById('exampleHighlighted').style.backgroundColor = result.highlightColor; @@ -83,6 +86,7 @@ function loadOptions() { document.getElementById('exampleSelected').style.color = result.textColor; document.getElementById('maxResults').value = result.maxResults; document.getElementById('instantResults').checked = result.instantResults; + document.getElementById('keepLastSearch').checked = result.keepLastSearch; document.getElementById('maxHistoryLength').value = result.maxHistoryLength; } ); @@ -105,18 +109,18 @@ document.addEventListener('DOMContentLoaded', function() { document.getElementById('exampleHighlighted').style.backgroundColor = document.getElementById('highlightColor').value; saveOptions(); }); - + document.getElementById('selectedColor').addEventListener('change', function() { document.getElementById('exampleSelected').style.backgroundColor = document.getElementById('selectedColor').value; saveOptions(); }); - + document.getElementById('textColor').addEventListener('change', function() { document.getElementById('exampleHighlighted').style.color = document.getElementById('textColor').value; document.getElementById('exampleSelected').style.color = document.getElementById('textColor').value; saveOptions(); }); - + document.getElementById('maxResults').addEventListener('change', function() { saveOptions(); }); @@ -125,14 +129,18 @@ document.addEventListener('DOMContentLoaded', function() { saveOptions(); }); + document.getElementById('keepLastSearch').addEventListener('change', function() { + saveOptions(); + }); + document.getElementById('maxHistoryLength').addEventListener('change', function() { saveOptions(); }); - + document.getElementById('buttonSave').addEventListener('click', function() { saveOptions(); }); - + document.getElementById('buttonReset').addEventListener('click', function() { restoreDefaults(); }); diff --git a/src/js/popup.js b/src/js/popup.js index fea3f45..38757a2 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -1,5 +1,6 @@ /*** CONSTANTS ***/ var DEFAULT_INSTANT_RESULTS = true; +var DEFAULT_KEEP_LAST_SEARCH = true; var ERROR_COLOR = '#ff8989'; var WHITE_COLOR = '#ffffff'; var ERROR_TEXT = "Content script was not loaded. Are you currently in the Chrome Web Store or in a chrome:// page? If you are, content scripts won't work here. If not, please wait for the page to finish loading or refresh the page."; @@ -18,6 +19,9 @@ var sentInput = false; var processingKey = false; var searchHistory = null; var maxHistoryLength = MAX_HISTORY_LENGTH; +// Flag dictates if we want to store the last searched regex in memory, +// pre-populating the regex bar if it exists. +var keepLastSearch = DEFAULT_KEEP_LAST_SEARCH; /*** VARIABLES ***/ /*** FUNCTIONS ***/ @@ -87,6 +91,9 @@ function passInputToContentScript(configurationChanged){ }); sentInput = true; } + if (keepLastSearch) { + chrome.storage.local.set({lastSearch: regexString}); + } } ); } @@ -245,7 +252,7 @@ document.getElementById('copy-to-clipboard').addEventListener('click', function }); }); -/* Received returnSearchInfo message, populate popup UI */ +/* Received returnSearchInfo message, populate popup UI */ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { if ('returnSearchInfo' == request.message) { processingKey = false; @@ -254,9 +261,6 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { } else { document.getElementById('numResults').textContent = String(request.currentSelection) + ' of ' + String(request.numResults); } - if (!sentInput) { - document.getElementById('inputRegex').value = request.regexString; - } if (request.numResults > 0 && request.cause == 'selectNode') { addToHistory(request.regexString); } @@ -292,7 +296,9 @@ chrome.storage.local.get({ 'instantResults' : DEFAULT_INSTANT_RESULTS, 'maxHistoryLength' : MAX_HISTORY_LENGTH, 'searchHistory' : null, - 'isSearchHistoryVisible' : false}, + 'isSearchHistoryVisible' : false, + 'keepLastSearch' : DEFAULT_KEEP_LAST_SEARCH, + 'lastSearch' : ''}, function(result) { if(result.instantResults) { document.getElementById('inputRegex').addEventListener('input', function() { @@ -304,6 +310,11 @@ chrome.storage.local.get({ }); } console.log(result); + + keepLastSearch = result.keepLastSearch; + if (keepLastSearch) { + document.getElementById('inputRegex').value = result.lastSearch; + } if(result.maxHistoryLength) { maxHistoryLength = result.maxHistoryLength; } @@ -340,7 +351,7 @@ function(tabs) { /* Focus onto input form */ document.getElementById('inputRegex').focus(); -window.setTimeout( +window.setTimeout( function(){document.getElementById('inputRegex').select();}, 0); //Thanks to http://stackoverflow.com/questions/480735#comment40578284_14573552 @@ -350,4 +361,3 @@ chrome.storage.local.set({isSearchHistoryVisible: makeVisible}); setCaseInsensitiveElement(); /*** INIT ***/ - diff --git a/src/options.html b/src/options.html index 9addaf6..73443b7 100644 --- a/src/options.html +++ b/src/options.html @@ -71,6 +71,14 @@ When this is disabled, you must press 'Enter' to trigger a search for regex matches. +