Skip to content

Commit 45e35d8

Browse files
committed
Added withActiveTab function
1 parent c01d8b3 commit 45e35d8

File tree

4 files changed

+39
-31
lines changed

4 files changed

+39
-31
lines changed

src/ContentScript.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Utils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path="d.ts/DefinitelyTyped/chrome/chrome.d.ts"/>
2+
3+
module Utils {
4+
export interface ActiveTabCallback {
5+
(tab: chrome.tabs.Tab) : void;
6+
}
7+
8+
export function withActiveTab(callback: ActiveTabCallback) {
9+
chrome.tabs.query({ active: true, lastFocusedWindow: true },
10+
function (tabs: chrome.tabs.Tab[]) {
11+
12+
console.assert(tabs.length == 1);
13+
callback(tabs[0]);
14+
});
15+
}
16+
17+
export function sendCommand(commandName: string, responseHandler?: any): void {
18+
(function (commandName, responseHandler) {
19+
withActiveTab(function (tab: chrome.tabs.Tab) {
20+
if (typeof responseHandler === "undefined") {
21+
responseHandler = null;
22+
}
23+
chrome.tabs.sendMessage(tab.id, { command: commandName },
24+
responseHandler);
25+
});
26+
})(commandName, responseHandler);
27+
}
28+
}

src/background/KeyboardHandler.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
/// <reference path="../d.ts/DefinitelyTyped/chrome/chrome.d.ts"/>
2-
/// <reference path="../ContentScript.ts"/>
2+
/// <reference path="../Utils.ts"/>
33
/// <reference path="TabStateManager.ts"/>
44

55
module KeyboardHandler {
66
var lastCalled: number;
77

88
export function init(tabStates: TabStateManager) {
99
chrome.commands.onCommand.addListener(function (command: string) {
10-
chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {
11-
console.assert(tabs.length == 1);
12-
var id = tabs[0].id;
10+
Utils.withActiveTab(function (tab: chrome.tabs.Tab) {
11+
var id = tab.id;
1312

1413
// The time hack is to get around this function being called twice when
1514
// the popup is open
1615
var d = new Date();
1716
if (tabStates.exists(id) && tabStates.get(id, "searching")
1817
&& d.getTime() - lastCalled > 50) {
1918
if (command == "next" || command == "prev") {
20-
ContentScript.sendCommand(command);
19+
Utils.sendCommand(command);
2120
}
2221
lastCalled = d.getTime();
2322
}
2423
});
2524
});
2625
}
27-
}
26+
}

src/popup/popup.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference path="../d.ts/DefinitelyTyped/chrome/chrome.d.ts"/>
22
/// <reference path="../bg-interface.ts"/>
3-
/// <reference path="../ContentScript.ts"/>
3+
/// <reference path="../Utils.ts"/>
44
var logging = false;
55

66
function log(message: string) {
@@ -9,10 +9,8 @@ function log(message: string) {
99
}
1010
}
1111

12-
chrome.tabs.query({active: true, lastFocusedWindow: true}, function (tabs) {
13-
console.assert(tabs.length == 1);
14-
15-
var id = tabs[0].id;
12+
Utils.withActiveTab(function (tab: chrome.tabs.Tab) {
13+
var id = tab.id;
1614
var tabStates = BackgroundInterface.getTabStateManager();
1715

1816
// In most cases the map entry will already be initialized. However, there
@@ -33,11 +31,11 @@ chrome.tabs.query({active: true, lastFocusedWindow: true}, function (tabs) {
3331
}
3432

3533
prevButton.addEventListener("click", function(event) {
36-
ContentScript.sendCommand("prev");
34+
Utils.sendCommand("prev");
3735
});
3836
nextButton.addEventListener("click", function(event) {
3937
if (tabState.searching) {
40-
ContentScript.sendCommand("next");
38+
Utils.sendCommand("next");
4139
} else {
4240
search();
4341
}
@@ -54,7 +52,7 @@ chrome.tabs.query({active: true, lastFocusedWindow: true}, function (tabs) {
5452

5553
if (tabState.searching) {
5654
tabState.searching = false;
57-
ContentScript.sendCommand("clear");
55+
Utils.sendCommand("clear");
5856
}
5957

6058
// Remove the invalid class if it's there

0 commit comments

Comments
 (0)