Skip to content

Feature 'keep last result' #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/js/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ***/
Expand Down Expand Up @@ -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();
}

Expand All @@ -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;
Expand Down Expand Up @@ -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))) ;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 */
Expand Down Expand Up @@ -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){
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
24 changes: 16 additions & 8 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ***/
Expand All @@ -18,7 +19,7 @@ function markStatus(text, time){
status.textContent = text;
setTimeout(function() {
status.textContent = '';
}, time);
}, time);
}

/* Validate input for max results */
Expand Down Expand Up @@ -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');
});
Expand All @@ -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;
Expand All @@ -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;
}
);
Expand All @@ -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();
});
Expand All @@ -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();
});
Expand Down
24 changes: 17 additions & 7 deletions src/js/popup.js
Original file line number Diff line number Diff line change
@@ -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.";
Expand All @@ -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 ***/
Expand Down Expand Up @@ -87,6 +91,9 @@ function passInputToContentScript(configurationChanged){
});
sentInput = true;
}
if (keepLastSearch) {
chrome.storage.local.set({lastSearch: regexString});
}
}
);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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

Expand All @@ -350,4 +361,3 @@ chrome.storage.local.set({isSearchHistoryVisible: makeVisible});

setCaseInsensitiveElement();
/*** INIT ***/

8 changes: 8 additions & 0 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
<small>When this is disabled, you must press 'Enter' to trigger a search for regex matches.</small>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Should the most recent result be stored (Google find stores this)?</div>
<div class="panel-body">
<input id="keepLastSearch" type="checkbox" /> <label for="keepLastSearch">Keep last search</label>
<br />
<small>When this is disabled, the regex field will be empty on every new page.</small>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">What's the maximum number of regex matches that should be highlighted on the web page?</div>
<div class="panel-body">
Expand Down
6 changes: 3 additions & 3 deletions src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<style type="text/css">
a:link, a:visited {
display: block;
width: 2em;
width: 2em;
padding: 0.2em;
line-height: 1.6;
background-color: #f2f2f2;
Expand Down Expand Up @@ -83,9 +83,9 @@
font-style: italic;
color: #555;
}

.clearHistoryButton {
color: #000;
color: #000;
text-decoration: none;
float: left !important;
width: 7em !important;
Expand Down