Skip to content

Commit df2ab71

Browse files
committed
Refactoring and fixing lint warnings
1 parent 45e35d8 commit df2ab71

File tree

5 files changed

+91
-81
lines changed

5 files changed

+91
-81
lines changed

src/Utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
/// <reference path="d.ts/DefinitelyTyped/chrome/chrome.d.ts"/>
22

33
module Utils {
4+
var logging = true;
5+
export function log(message: string) {
6+
if (logging) {
7+
console.log(message);
8+
}
9+
}
10+
411
export interface ActiveTabCallback {
512
(tab: chrome.tabs.Tab) : void;
613
}

src/background/BackgroundScript.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module BackgroundScript {
3333
// Reset the tab state whenever it is loaded/reloaded
3434
if (request.event == "loaded") {
3535
if (tabStates.exists(id)) {
36-
tabStates.set(id, { query: "", searching: false, caseInsensitive: false });
36+
tabStates.resetState(id);
3737
} else {
3838
// Don't change the query, only reset searching
3939
tabStates.set(id, "searching", false);
@@ -48,4 +48,4 @@ module BackgroundScript {
4848
}
4949

5050
init();
51-
}
51+
}

src/background/TabStateManager.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ class TabStateManager {
1111
return typeof (this.tabStates[tabId]) !== "undefined";
1212
}
1313

14+
public resetState(tabId: number) {
15+
this.set(tabId, { query: "", searching: false, caseInsensitive: false });
16+
}
17+
18+
public isSearching(tabId: number): boolean {
19+
return this.get(tabId, "searching");
20+
}
21+
1422
public set(tabId: number, tabState: TabState);
1523
public set(tabId: number, propName: string, propVal: any);
1624
public set(tabId: number, stateOrPropName: any, propVal?: any) {
@@ -38,4 +46,4 @@ class TabState {
3846
public query: string;
3947
public searching: boolean;
4048
public caseInsensitive: boolean;
41-
}
49+
}

src/content/content.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var marks = new Array();
1+
var marks = [];
22
var cur = 0;
33
var logging = false;
44

@@ -42,11 +42,11 @@ chrome.runtime.onMessage.addListener(
4242
function(request, sender, sendResponse) {
4343
log("Received command " + request.command);
4444
if (request.command == "search") {
45-
log("Searching for regex: " + request.regexp)
45+
log("Searching for regex: " + request.regexp);
4646
var flags = "g";
47-
if (request.caseInsensitive == true) {
48-
log("Case insensitive enabled")
49-
var flags = "gi";
47+
if (request.caseInsensitive === true) {
48+
log("Case insensitive enabled");
49+
flags = "gi";
5050
}
5151
clear();
5252
displayInfoSpan();
@@ -114,7 +114,7 @@ function recurse(element, regexp) {
114114
}
115115
}
116116

117-
if (element.nodeType == Node.TEXT_NODE && element.nodeValue.trim() != '') {
117+
if (element.nodeType == Node.TEXT_NODE && element.nodeValue.trim() !== '') {
118118
/*
119119
* 1. Find all regex matches
120120
* 2. Loop through matches, finding their starting positions
@@ -126,7 +126,7 @@ function recurse(element, regexp) {
126126
var matches = str.match(regexp);
127127
var parent = element.parentNode;
128128

129-
if (matches != null) {
129+
if (matches !== null) {
130130
var pos = 0;
131131
var mark;
132132
for (var i = 0; i < matches.length; i++) {
@@ -141,7 +141,7 @@ function recurse(element, regexp) {
141141
if (element.parentNode == parent) {
142142
parent.replaceChild(before, element);
143143
} else {
144-
parent.insertBefore(before, mark.nextSibling)
144+
parent.insertBefore(before, mark.nextSibling);
145145
}
146146

147147
mark = document.createElement('mark');
@@ -172,10 +172,11 @@ function clear() {
172172

173173
// Set the infoSpan text to the number of matches
174174
function displayCount() {
175+
var num;
175176
if (marks.length > 0) {
176-
var num = cur + 1;
177+
num = cur + 1;
177178
} else {
178-
var num = 0;
179+
num = 0;
179180
}
180181
setInfoSpanText(num + " of " + marks.length + " matches.");
181182
displayInfoSpan();

src/popup/popup.ts

Lines changed: 62 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,75 @@
11
/// <reference path="../d.ts/DefinitelyTyped/chrome/chrome.d.ts"/>
22
/// <reference path="../bg-interface.ts"/>
33
/// <reference path="../Utils.ts"/>
4-
var logging = false;
54

6-
function log(message: string) {
7-
if (logging) {
8-
console.log(message);
9-
}
10-
}
5+
module Popup {
6+
Utils.withActiveTab(function (tab: chrome.tabs.Tab) {
7+
var id = tab.id;
8+
var tabStates = BackgroundInterface.getTabStateManager();
119

12-
Utils.withActiveTab(function (tab: chrome.tabs.Tab) {
13-
var id = tab.id;
14-
var tabStates = BackgroundInterface.getTabStateManager();
15-
16-
// In most cases the map entry will already be initialized. However, there
17-
// may be a few edge cases where we need to initialize it ourselves.
18-
if (!tabStates.exists(id)) {
19-
log("ID doesn't exist. Initializing entry.")
20-
tabStates.set(id, { query: "", searching: false, caseInsensitive: false });
21-
}
22-
var tabState = tabStates.get(id);
10+
// In most cases the map entry will already be initialized. However, there
11+
// may be a few edge cases where we need to initialize it ourselves.
12+
if (!tabStates.exists(id)) {
13+
Utils.log("ID doesn't exist. Initializing entry.")
14+
tabStates.resetState(id);
15+
}
2316

24-
var prevButton = document.getElementById("prev");
25-
var nextButton = document.getElementById("next");
26-
var queryInput = <HTMLInputElement> document.getElementById("query");
27-
var caseInsensitiveCheckbox = <HTMLInputElement> document.getElementById("case-insensitive");
17+
var prevButton = document.getElementById("prev");
18+
var nextButton = document.getElementById("next");
19+
var queryInput = <HTMLInputElement> document.getElementById("query");
20+
var caseInsensitiveCheckbox = <HTMLInputElement> document.getElementById("case-insensitive");
2821

29-
function setEnabled(id, val) {
30-
document.getElementById(id).disabled = !val;
31-
}
22+
prevButton.addEventListener("click", function(event) {
23+
Utils.sendCommand("prev");
24+
});
25+
nextButton.addEventListener("click", function(event) {
26+
if (tabStates.isSearching(id)) {
27+
Utils.sendCommand("next");
28+
} else {
29+
search(id, tabStates);
30+
}
31+
});
3232

33-
prevButton.addEventListener("click", function(event) {
34-
Utils.sendCommand("prev");
35-
});
36-
nextButton.addEventListener("click", function(event) {
37-
if (tabState.searching) {
38-
Utils.sendCommand("next");
39-
} else {
40-
search();
41-
}
42-
});
33+
queryInput.addEventListener("keydown", function(event) {
34+
if (event.keyCode == 13) {
35+
Utils.log("Enter pressed");
36+
search(id, tabStates);
37+
}
38+
});
39+
queryInput.addEventListener("input", () => {
40+
tabStates.set(id, "query", this.value);
4341

44-
queryInput.addEventListener("keydown", function(event) {
45-
if (event.keyCode == 13) {
46-
log("Enter pressed");
47-
search();
48-
}
49-
});
50-
queryInput.addEventListener("input", function(event) {
51-
tabState.query = queryInput.value;
42+
if (tabStates.isSearching(id)) {
43+
tabStates.set(id, "searching", false);
44+
Utils.sendCommand("clear");
45+
}
5246

53-
if (tabState.searching) {
54-
tabState.searching = false;
55-
Utils.sendCommand("clear");
56-
}
47+
// Remove the invalid class if it's there
48+
this.className = '';
5749

58-
// Remove the invalid class if it's there
59-
queryInput.className = '';
50+
if (this.value == "") {
51+
setEnabled("next", false);
52+
} else {
53+
setEnabled("next", true);
54+
}
55+
});
6056

61-
if (queryInput.value == "") {
57+
this.value = tabStates.get(id, "query");
58+
if (this.value == "") {
6259
setEnabled("next", false);
6360
} else {
6461
setEnabled("next", true);
6562
}
66-
});
67-
68-
queryInput.value = tabState.query;
69-
if (queryInput.value == "") {
70-
setEnabled("next", false);
71-
} else {
72-
setEnabled("next", true);
73-
}
7463

75-
caseInsensitiveCheckbox.onclick = function() {
76-
log("Set checkbox state to " + this.checked);
77-
tabState.caseInsensitive = this.checked;
78-
}
64+
caseInsensitiveCheckbox.onclick = function() {
65+
Utils.log("Set checkbox state to " + this.checked);
66+
tabStates.set(id, "caseInsensitive", this.checked);
67+
}
7968

80-
caseInsensitiveCheckbox.checked = tabState.caseInsensitive;
69+
caseInsensitiveCheckbox.checked = tabStates.get(id, "caseInsensitive");
70+
});
8171

82-
function search() {
72+
function search(tabId: number, tabStates: TabStateManager) {
8373
var el = <HTMLInputElement> document.getElementById("query");
8474
if (validate(el.value)) {
8575
el.className = '';
@@ -89,20 +79,20 @@ Utils.withActiveTab(function (tab: chrome.tabs.Tab) {
8979
} else {
9080
var insensitive = false;
9181
}
92-
chrome.tabs.sendMessage(id,
82+
chrome.tabs.sendMessage(tabId,
9383
{
9484
command: "search",
9585
caseInsensitive: insensitive,
9686
regexp: el.value
9787
});
98-
tabState.searching = true;
88+
tabStates.set(tabId, "searching", true);
9989
} else {
100-
log("Invalid regex");
90+
Utils.log("Invalid regex");
10191
el.className = 'invalid';
10292
}
10393
}
10494

105-
function validate(regexp) {
95+
function validate(regexp: string): boolean {
10696
if (regexp != "") {
10797
try {
10898
"".match(regexp);
@@ -112,4 +102,8 @@ Utils.withActiveTab(function (tab: chrome.tabs.Tab) {
112102
}
113103
return false;
114104
}
115-
});
105+
106+
function setEnabled(id: string, val: boolean) {
107+
document.getElementById(id).disabled = !val;
108+
}
109+
}

0 commit comments

Comments
 (0)