1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
<html>
<head>
<script type="text/javascript" charset="utf-8">
var tabQueue = []; // Queue of tabs to restore with scroll info
var keyQueue = ""; // Queue of keys typed
// Port handler mapping
var portHandlers = { "keyDown": handleKeyDown,
"returnScrollPosition": handleReturnScrollPosition };
// Event handlers
var selectionChangedHandlers = [];
var getScrollPositionHandlers = {}; // tabId -> function (tab, scrollX, scrollY);
var tabLoadedHandlers = {}; // tabId -> function ()
chrome.extension.onConnect.addListener(function(port, name) {
var senderTabId = port.sender.tab ? port.sender.tab.id : null;
// If this is a tab we've been waiting to open, execute any "tab loaded" handlers, e.g. to restore
// the tab's scroll position.
if (senderTabId && tabLoadedHandlers[senderTabId]) {
var toCall = tabLoadedHandlers[senderTabId];
// Delete first to be sure there's no circular events.
delete tabLoadedHandlers[senderTabId];
toCall.call();
}
if (portHandlers[port.name])
port.onMessage.addListener(portHandlers[port.name]);
});
function handleReturnScrollPosition(args) {
if (getScrollPositionHandlers[args.currentTab.id]) {
// Delete first to be sure there's no circular events.
var toCall = getScrollPositionHandlers[args.currentTab.id];
delete getScrollPositionHandlers[args.currentTab.id];
toCall(args.currentTab, args.scrollX, args.scrollY);
}
}
chrome.tabs.onSelectionChanged.addListener(function (tabId, selectionInfo) {
if (selectionChangedHandlers.length > 0) { selectionChangedHandlers.pop().call(); }
});
function repeatFunction(func, totalCount, currentCount) {
if (currentCount < totalCount)
func(function () { repeatFunction(func, totalCount, currentCount + 1); });
}
// Returns the currently selected tab along with scroll coordinates. Pass in a callback of the form:
// function (tab, scrollX, scrollY) { .. }
function getCurrentTabWithScrollPosition(callback) {
chrome.tabs.getSelected(null, function(tab) {
getScrollPositionHandlers[tab.id] = callback;
var scrollPort = chrome.tabs.connect(tab.id, { name: "getScrollPosition" });
scrollPort.postMessage({currentTab: tab});
});
}
// Start action functions
function createTab(callback) {
chrome.tabs.create({}, function(tab) { callback(); });
}
function removeTab(callback) {
getCurrentTabWithScrollPosition(function(tab, scrollX, scrollY) {
tabQueue.push({ tabUrl: tab.url, scrollX: scrollX, scrollY: scrollY });
chrome.tabs.remove(tab.id);
// We can't just call the callback here because we actually need to wait
// for the selection to change to consider this action done.
selectionChangedHandlers.push(callback);
});
}
function restoreTab(callback) {
if (tabQueue.length > 0) {
var tabQueueEntry = tabQueue.pop();
// We have to chain a few callbacks to set the appropriate scroll position. We can't just wait until the
// tab is created because the content script is not available during the "loading" state. We need to
// wait until that's over before we can call setScrollPosition.
chrome.tabs.create({ url: tabQueueEntry.tabUrl }, function(tab) {
tabLoadedHandlers[tab.id] = function() {
var scrollPort = chrome.tabs.connect(tab.id, {name: "setScrollPosition"});
scrollPort.postMessage({ scrollX: tabQueueEntry.scrollX, scrollY: tabQueueEntry.scrollY });
};
callback();
});
}
}
// End action functions
var keyToCommandRegistry = {};
keyToCommandRegistry['j'] = 'scrollDown';
keyToCommandRegistry['k'] = 'scrollUp';
keyToCommandRegistry['h'] = 'scrollLeft';
keyToCommandRegistry['l'] = 'scrollRight';
keyToCommandRegistry['gg'] = 'scrollToTop';
keyToCommandRegistry['G'] = 'scrollToBottom';
keyToCommandRegistry['r'] = 'reload';
keyToCommandRegistry['ba'] = 'goBack';
keyToCommandRegistry['H'] = 'goBack';
keyToCommandRegistry['fw'] = 'goForward';
keyToCommandRegistry['fo'] = 'goForward';
keyToCommandRegistry['L'] = 'goForward';
keyToCommandRegistry['t'] = createTab;
keyToCommandRegistry['d'] = removeTab;
keyToCommandRegistry['u'] = restoreTab;
function handleKeyDown(key) {
keyQueue = keyQueue + key;
console.log("current keyQueue: [", keyQueue, "]");
checkKeyQueue();
}
function checkKeyQueue() {
var match = /([0-9]*)(.*)/.exec(keyQueue);
var count = parseInt(match[1]);
var command = match[2];
if (command.length == 0) { return; }
if (isNaN(count)) { count = 1; }
if (keyToCommandRegistry[command]) {
registryEntry = keyToCommandRegistry[command];
console.log("command found for [", keyQueue, "],", registryEntry);
if (typeof(registryEntry) == "string") {
chrome.tabs.getSelected(null, function(tab) {
var port = chrome.tabs.connect(tab.id, { name: "executePageCommand" });
port.postMessage({ command: registryEntry, count: count });
});
} else {
repeatFunction(registryEntry, count, 0);
}
keyQueue = "";
} else if (command.length > 1) {
keyQueue = "";
}
}
</script>
</head>
<body>
howdy
</body>
</html>
|