This repository has been archived by the owner on Sep 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
quick_add_client.js
65 lines (60 loc) · 2.29 KB
/
quick_add_client.js
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
/**
* A module to run in a content window to enable QuickAdd from that window.
* That is, pressing Tab+Q anywhere in the window will open the same popup
* as is available from the chrome menu.
*
* This is not a perfect solution (though it might be refined further with
* a little more effort). For one thing, we don't swallow the TAB key because
* we don't want to interfere with the underlying page (especially if the user
* wasn't going to press TAB+Q). So using the hotkey may cause you to focus
* a new input on the page before opening the popup.
*
* Also, there's no way to trigger the chrome extension popup itself, so this
* opens a new window with the popup content in it. This looks slightly
* different than the real popup (and appears in a different place), but it
* does the job.
*/
Asana.QuickAddClient = {
_tab_down_time: 0,
keyDown: function(e) {
var self = Asana.QuickAddClient;
if (e.keyCode === 9) {
// Mark tab key as pressed at the current time.
// If the Q key gets pressed soon enough afterward, we've hit our
// hotkey combo.
self._tab_down_time = new Date().getTime();
} else if (e.keyCode === 81) {
if (new Date().getTime() < self._tab_down_time + 5000) { // 5s timeout
self._tab_down_time = 0;
// Tab-Q!
// We cannot open the popup programmatically.
// http://code.google.com/chrome/extensions/faq.html#faq-open-popups
// So we do this roundabout thing.
chrome.extension.sendRequest({
type: "quick_add",
url: window.location.href,
title: document.title,
selected_text: "" + window.getSelection()
});
e.preventDefault();
return false;
}
}
},
keyUp: function(e) {
var self = Asana.QuickAddClient;
if (e.keyCode === 9) {
// Mark tab key as released.
self._tab_down_time = 0;
}
},
listen: function() {
// Don't run against Asana, which already has a QuickAdd feature. ;)
if (!/^https?:\/\/app[.]asana[.]com/.test(window.location.href) &&
!/^https?:\/\/localhost/.test(window.location.href)) {
window.addEventListener("keydown", Asana.QuickAddClient.keyDown, true);
window.addEventListener("keyup", Asana.QuickAddClient.keyUp, true);
}
}
};
Asana.QuickAddClient.listen();