-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent-script.js
More file actions
206 lines (180 loc) · 5.53 KB
/
Copy pathcontent-script.js
File metadata and controls
206 lines (180 loc) · 5.53 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Dictionary of the form: {string, bool} indicating if a key is pressed
const pressed_keys = {};
const tracked_keys = new Set();
// TODO: const leader = " ";
/*
* Wraps the harpoon iframe and handles all commands related to the iframe
* to initialize it. Commands to cycle and delete items are not supposed to be here.
*/
const Harpoon = {
harpoonUI: null,
init() {
if (!this.harpoonUI) {
this.harpoonUI = new UIComponent();
// Need the classname to be able to manipulate the iframe from the parent (like hiding)
this.harpoonUI.load("pages/harpoon.html", "harpoon-frame");
}
},
// Entry point to creating the Harpoon Iframe
activate() {
this.init();
this.harpoonUI.show(
{ name: "activate" },
{ focus: true },
);
},
hide() {
this.init();
this.harpoonUI.postMessage({ name: "hide" });
}
}
const Help = {
helpUI: null,
init() {
if (!this.helpUI) {
this.helpUI = new UIComponent();
console.log("hello");
this.helpUI.load("pages/help.html", "help-frame");
}
},
activate() {
this.init();
this.helpUI.show(
{ name: "activate" },
{ focus: true },
);
},
hide() {
this.init();
this.helpUI.postMessage({ name: "hide" });
}
}
var harpoon = null;
var help = null;
document.addEventListener("click", event => {
if (harpoon) {
harpoon.hide();
}
if (help) {
help.hide();
}
});
/**
* Release key in pressed_keys
*/
document.addEventListener("keyup", event => {
pressed_keys[event.key] = false;
});
/**
* Wrapper around postMessage to send specific messages at a port.
*/
const sendRequest = (messageParam, port) => {
try {
port.postMessage({ message: messageParam });
} catch (err) {
}
}
/**
* Add key to pressed_keys.
*
* Outside of adding the pressed key, this listener checks if any combination of keys
* are pressed that correspond to an input for the extension.
* If a valid key combination is pressed, signal the port which handles the event accordingly.
* This
*
* Possible key combinations:
* <Alt> +
* "a": Mark current tab
* "e": View all marked tabs
* 1-9: Swap current tab with corresponding marked tab
* "y": go to next tab in jump list
* "z": go to previous tab in jump list
*/
document.addEventListener("keydown", event => {
const active = document.activeElement;
if (
active.tagName === "INPUT" ||
active.tagName === "TEXTAREA" ||
active.isContentEditable
) {
return;
}
pressed_keys[event.key] = true;
if (event.altKey) {
try {
var port = chrome.runtime.connect({ name: "tabs" });
} catch (err) {
return;
}
if (pressed_keys["a"]) {
sendRequest("add", port);
} else if (pressed_keys["z"]) {
sendRequest("back", port);
// The pressed keys for y, z, and numbers is set to false,
// because this will change tabs, making the event listener
// miss the key up event.
pressed_keys['z'] = false;
event.altKey = false; // FIXME: Don't think this is supposed to be here
return;
} else if (pressed_keys["y"]) {
sendRequest("front", port);
pressed_keys['y'] = false;
return;
} else if (pressed_keys["w"]) {
if (!harpoon) {
harpoon = Harpoon;
harpoon.activate();
} else {
harpoon.activate();
}
pressed_keys["w"] = false;
} else if (pressed_keys["/"]) {
if (!help) {
help = Help;
}
help.activate();
pressed_keys["/"] = false;
}
else {
for (let i = 0; i < 10; i++) {
if (pressed_keys[i.toString()]) {
sendRequest(i, port);
pressed_keys[i] = false;
if (tracked_keys.has("s")) {
console.log("did it");
port.postMessage({ message: "addProfile", index: i });
}
break;
}
}
}
port.onMessage.addListener(function(message) {
if (message.status === "bad") {
console.log("Error occured while processing input")
}
});
} else if (event.shiftKey) {
try {
var port = chrome.runtime.connect({ name: "tabs" });
} catch (err) {
return;
}
for (let i = 0; i < 10; i++) {
if (event.code == `Digit${i}`) {
if (tracked_keys.has("s")) {
port.postMessage({ message: "addProfile", index: i });
tracked_keys.clear();
} else {
port.postMessage({ message: "loadProfile", index: i });
pressed_keys[event.key] = false;
}
break;
}
}
} else {
tracked_keys.clear();
}
if (event.key == "s") {
tracked_keys.add("s");
}
});