Skip to content

Commit 9a876e5

Browse files
author
Roger Shen
committed
Add clear history button and make max history length configurable
1 parent 1e2550d commit 9a876e5

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

src/js/options.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var DEFAULT_HIGHLIGHT_COLOR = '#ffff00';
44
var DEFAULT_SELECTED_COLOR = '#ff9900';
55
var DEFAULT_TEXT_COLOR = '#000000';
66
var DEFAULT_CASE_INSENSITIVE = false;
7+
var DEFAULT_MAX_HISTORY_LENGTH = 30;
78
var WHITE_COLOR = '#ffffff';
89
var ERROR_COLOR = '#ff8989';
910
var GOOD_COLOR = '#89ff89';
@@ -55,7 +56,8 @@ function saveOptions() {
5556
'textColor' : document.getElementById('textColor').value,
5657
'maxResults' : maxResults,
5758
'instantResults' : document.getElementById('instantResults').checked,
58-
'caseInsensitive' : document.getElementById('caseInsensitive').checked
59+
'caseInsensitive' : document.getElementById('caseInsensitive').checked,
60+
'maxHistoryLength' : document.getElementById('maxHistoryLength').value
5961
}
6062

6163
chrome.storage.local.set(options, function() {
@@ -72,7 +74,8 @@ function loadOptions() {
7274
'textColor' : DEFAULT_TEXT_COLOR,
7375
'maxResults' : DEFAULT_MAX_RESULTS,
7476
'instantResults' : DEFAULT_INSTANT_RESULTS,
75-
'caseInsensitive' : DEFAULT_CASE_INSENSITIVE },
77+
'caseInsensitive' : DEFAULT_CASE_INSENSITIVE,
78+
'maxHistoryLength' : DEFAULT_MAX_HISTORY_LENGTH },
7679
function(result) {
7780
document.getElementById('highlightColor').value = result.highlightColor;
7881
document.getElementById('exampleHighlighted').style.backgroundColor = result.highlightColor;
@@ -84,6 +87,7 @@ function loadOptions() {
8487
document.getElementById('maxResults').value = result.maxResults;
8588
document.getElementById('instantResults').checked = result.instantResults;
8689
document.getElementById('caseInsensitive').checked = result.caseInsensitive;
90+
document.getElementById('maxHistoryLength').value = result.maxHistoryLength;
8791
}
8892
);
8993
}
@@ -129,6 +133,10 @@ document.addEventListener('DOMContentLoaded', function() {
129133
document.getElementById('caseInsensitive').addEventListener('change', function() {
130134
saveOptions();
131135
});
136+
137+
document.getElementById('maxHistoryLength').addEventListener('change', function() {
138+
saveOptions();
139+
});
132140

133141
document.getElementById('buttonSave').addEventListener('click', function() {
134142
saveOptions();

src/js/popup.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ var ERROR_TEXT = "Content script was not loaded. Are you currently in the Chrome
66
var SHOW_HISTORY_TITLE = "Show search history";
77
var HIDE_HISTORY_TITLE = "Hide search history";
88
var HISTORY_IS_EMPTY_TEXT = "Search history is empty.";
9+
var CLEAR_ALL_HISTORY_TEXT = "Clear History";
910
var MAX_HISTORY_LENGTH = 30;
1011
/*** CONSTANTS ***/
1112

1213
/*** VARIABLES ***/
1314
var sentInput = false;
1415
var processingKey = false;
1516
var searchHistory = null;
17+
var maxHistoryLength = MAX_HISTORY_LENGTH;
1618
/*** VARIABLES ***/
1719

1820
/*** FUNCTIONS ***/
@@ -102,6 +104,7 @@ function createHistoryLineElement(text) {
102104
if (document.getElementById('inputRegex').value !== text) {
103105
document.getElementById('inputRegex').value = text;
104106
passInputToContentScript();
107+
document.getElementById('inputRegex').focus();
105108
}
106109
});
107110
var lineDiv = document.createElement('div');
@@ -123,6 +126,13 @@ function updateHistoryDiv() {
123126
for (var i = searchHistory.length - 1; i >= 0; i--) {
124127
historyDiv.appendChild(createHistoryLineElement(searchHistory[i]));
125128
}
129+
var clearButton = document.createElement('a');
130+
clearButton.href = '#';
131+
clearButton.type = 'button';
132+
clearButton.textContent = CLEAR_ALL_HISTORY_TEXT;
133+
clearButton.className = 'clearHistoryButton';
134+
clearButton.addEventListener('click', clearSearchHistory);
135+
historyDiv.appendChild(clearButton);
126136
}
127137
}
128138
}
@@ -137,8 +147,8 @@ function addToHistory(regex) {
137147
searchHistory.splice(i, 1);
138148
}
139149
}
140-
if (searchHistory.length > MAX_HISTORY_LENGTH) {
141-
searchHistory.splice(0, searchHistory.length - MAX_HISTORY_LENGTH);
150+
if (searchHistory.length > maxHistoryLength) {
151+
searchHistory.splice(0, searchHistory.length - maxHistoryLength);
142152
}
143153
chrome.storage.local.set({searchHistory: searchHistory});
144154
updateHistoryDiv();
@@ -150,6 +160,13 @@ function setHistoryVisibility(makeVisible) {
150160
document.getElementById('show-history').title = makeVisible ? HIDE_HISTORY_TITLE : SHOW_HISTORY_TITLE;
151161
}
152162

163+
function clearSearchHistory() {
164+
searchHistory = [];
165+
chrome.storage.local.set({searchHistory: searchHistory});
166+
updateHistoryDiv();
167+
}
168+
169+
153170
/*** LISTENERS ***/
154171
document.getElementById('next').addEventListener('click', function() {
155172
selectNext();
@@ -217,6 +234,7 @@ onkeydown = onkeyup = function(e) {
217234
/* Retrieve from storage whether we should use instant results or not */
218235
chrome.storage.local.get({
219236
'instantResults' : DEFAULT_INSTANT_RESULTS,
237+
'maxHistoryLength' : MAX_HISTORY_LENGTH,
220238
'searchHistory' : null,
221239
'isSearchHistoryVisible' : false},
222240
function(result) {
@@ -229,6 +247,10 @@ chrome.storage.local.get({
229247
passInputToContentScript();
230248
});
231249
}
250+
console.log(result);
251+
if(result.maxHistoryLength) {
252+
maxHistoryLength = result.maxHistoryLength;
253+
}
232254
if(result.searchHistory) {
233255
searchHistory = result.searchHistory.slice(0);
234256
} else {

src/options.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,18 @@
8686
<small>The smaller this number is, the better your performance (especially for complex pages).</small>
8787
</div>
8888
</div>
89+
<div class="panel panel-default">
90+
<div class="panel-heading">What's the maximum number of search history records that be stored??</div>
91+
<div class="panel-body">
92+
<label for="maxHistoryLength">Max records</label> <input id="maxHistoryLength" type="text" name="maxHistoryLength" ></input>
93+
<br />
94+
<small>The default number of records is 30.</small>
95+
</div>
96+
</div>
8997
<button id="buttonSave" type="save">Save Settings</button>
9098
<button id="buttonReset" type="reset">Reset to Defaults</button>
9199
<p id="status" class="status-chrome-regex"></p>
92100
</div>
93101
</div>
94102
</body>
95-
</html>
103+
</html>

src/popup.html

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#numResults {
4949
position: absolute;
5050
top: 0.25em;
51-
left: 19.5em;
51+
left: 15.5em;
5252
min-width: 4em;
5353
max-width: 5em;
5454
overflow:auto;
@@ -74,6 +74,15 @@
7474
font-style: italic;
7575
color: #555;
7676
}
77+
78+
.clearHistoryButton {
79+
color: #000;
80+
text-decoration: none;
81+
float: left !important;
82+
width: 7em !important;
83+
-webkit-text-stroke: 0.5px rgba(0,0,0,1) !important;
84+
margin-bottom: 0.2em;
85+
}
7786

7887
body {
7988
background-color: #dddddd;
@@ -88,12 +97,12 @@
8897
<input id="inputRegex" type="text" name="inputRegex"></input><span id="numResults"></span>
8998
<a href="#" id="prev" type="button" title="Previous Match"><i class="fa fa-angle-up"></i></a>
9099
<a href="#" id="next" type="button" title="Next Match"><i class="fa fa-angle-down"></i></a>
91-
<a href="#" id="clear" type="button" title="clear"><i class="fa fa-trash-o"></i></a>
92-
<a href="#" id="show-history" type="button"><i class="fa fa-history"></i></a>
100+
<a href="#" id="clear" type="button" title="Clear"><i class="fa fa-trash-o"></i></a>
101+
<a href="#" id="show-history" type="button" title="History"><i class="fa fa-history"></i></a>
93102
</div>
94103
<div id="history">
95104
</div>
96105
<span id="error" class="error-chrome-regex"></span>
97106
</body>
98107
<script type="text/javascript" src="js/popup.js"></script>
99-
</html>
108+
</html>

0 commit comments

Comments
 (0)