forked from alyssaxuu/omni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
455 lines (427 loc) · 17.1 KB
/
content.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Workaround to capture Esc key on certain sites
var isOpen = false;
document.onkeyup = (e) => {
if (e.key == "Escape" && isOpen) {
chrome.runtime.sendMessage({request:"close-omni"})
}
}
$(document).ready(() => {
var actions = [];
var isFiltered = false;
// Append the omni into the current page
$.get(chrome.runtime.getURL('/content.html'), (data) => {
$(data).appendTo('body');
// Get checkmark image for toast
$("#omni-extension-toast img").attr("src", chrome.runtime.getURL("assets/check.svg"));
// Request actions from the background
chrome.runtime.sendMessage({request:"get-actions"}, (response) => {
actions = response.actions;
});
// New tab page workaround
if (window.location.href == "chrome-extension://mpanekjjajcabgnlbabmopeenljeoggm/newtab.html") {
isOpen = true;
$("#omni-extension").removeClass("omni-closing");
window.setTimeout(() => {
$("#omni-extension input").focus();
}, 100);
}
});
function renderAction(action, index, keys, img) {
var skip = "";
if (action.action == "search" || action.action == "goto") {
skip = "style='display:none'";
}
if (index != 0) {
$("#omni-extension #omni-list").append("<div class='omni-item' " skip " data-index='" index "' data-type='" action.type "'>" img "<div class='omni-item-details'><div class='omni-item-name'>" action.title "</div><div class='omni-item-desc'>" action.desc "</div></div>" keys "<div class='omni-select'>Select <span class='omni-shortcut'>⏎</span></div></div>");
} else {
$("#omni-extension #omni-list").append("<div class='omni-item omni-item-active' " skip " data-index='" index "' data-type='" action.type "'>" img "<div class='omni-item-details'><div class='omni-item-name'>" action.title "</div><div class='omni-item-desc'>" action.desc "</div></div>" keys "<div class='omni-select'>Select <span class='omni-shortcut'>⏎</span></div></div>");
}
if (!action.emoji) {
var loadimg = new Image();
loadimg.src = action.favIconUrl;
// Favicon doesn't load, use a fallback
loadimg.onerror = () => {
$(".omni-item[data-index='" index "'] img").attr("src", chrome.runtime.getURL("/assets/globe.svg"));
}
}
}
// Add actions to the omni
function populateOmni() {
$("#omni-extension #omni-list").html("");
actions.forEach((action, index) => {
var keys = "";
if (action.keycheck) {
keys = "<div class='omni-keys'>";
action.keys.forEach(function(key){
keys = "<span class='omni-shortcut'>" key "</span>";
});
keys = "</div>";
}
// Check if the action has an emoji or a favicon
if (!action.emoji) {
var onload = 'if ("naturalHeight" in this) {if (this.naturalHeight this.naturalWidth === 0) {this.onerror();return;}} else if (this.width this.height == 0) {this.onerror();return;}';
var img = "<img src='" action.favIconUrl "' alt='favicon' onload='" onload "' onerror='this.src="" chrome.runtime.getURL("/assets/globe.svg") ""' class='omni-icon'>";
renderAction(action, index, keys, img);
} else {
var img = "<span class='omni-emoji-action'>" action.emojiChar "</span>";
renderAction(action, index, keys, img);
}
})
$(".omni-extension #omni-results").html(actions.length " results");
}
// Add filtered actions to the omni
function populateOmniFilter(actions) {
isFiltered = true;
$("#omni-extension #omni-list").html("");
const renderRow = (index) => {
const action = actions[index]
var keys = "";
if (action.keycheck) {
keys = "<div class='omni-keys'>";
action.keys.forEach(function(key){
keys = "<span class='omni-shortcut'>" key "</span>";
});
keys = "</div>";
}
var img = "<img src='" action.favIconUrl "' alt='favicon' onerror='this.src="" chrome.runtime.getURL("/assets/globe.svg") ""' class='omni-icon'>";
if (action.emoji) {
img = "<span class='omni-emoji-action'>" action.emojiChar "</span>"
}
if (index != 0) {
return $("<div class='omni-item' data-index='" index "' data-type='" action.type "' data-url='" action.url "'>" img "<div class='omni-item-details'><div class='omni-item-name'>" action.title "</div><div class='omni-item-desc'>" action.url "</div></div>" keys "<div class='omni-select'>Select <span class='omni-shortcut'>⏎</span></div></div>")[0]
} else {
return $("<div class='omni-item omni-item-active' data-index='" index "' data-type='" action.type "' data-url='" action.url "'>" img "<div class='omni-item-details'><div class='omni-item-name'>" action.title "</div><div class='omni-item-desc'>" action.url "</div></div>" keys "<div class='omni-select'>Select <span class='omni-shortcut'>⏎</span></div></div>")[0]
}
}
actions.length && new VirtualizedList.default($("#omni-extension #omni-list")[0], {
height: 400,
rowHeight: 60,
rowCount: actions.length,
renderRow,
onMount: () => $(".omni-extension #omni-results").html(actions.length " results"),
});
}
// Open the omni
function openOmni() {
chrome.runtime.sendMessage({request:"get-actions"}, (response) => {
isOpen = true;
actions = response.actions;
$("#omni-extension input").val("");
populateOmni();
$("html, body").stop();
$("#omni-extension").removeClass("omni-closing");
window.setTimeout(() => {
$("#omni-extension input").focus();
focusLock.on($("#omni-extension input").get(0));
$("#omni-extension input").focus();
}, 100);
});
}
// Close the omni
function closeOmni() {
if (window.location.href == "chrome-extension://mpanekjjajcabgnlbabmopeenljeoggm/newtab.html") {
chrome.runtime.sendMessage({request:"restore-new-tab"});
} else {
isOpen = false;
$("#omni-extension").addClass("omni-closing");
}
}
// Hover over an action in the omni
function hoverItem() {
$(".omni-item-active").removeClass("omni-item-active");
$(this).addClass("omni-item-active");
}
// Show a toast when an action has been performed
function showToast(action) {
$("#omni-extension-toast span").html('"' action.title '" has been successfully performed');
$("#omni-extension-toast").addClass("omni-show-toast");
setTimeout(() => {
$(".omni-show-toast").removeClass("omni-show-toast");
}, 3000)
}
// Autocomplete commands. Since they all start with different letters, it can be the default behavior
function checkShortHand(e, value) {
var el = $(".omni-extension input");
if (e.keyCode != 8) {
if (value == "/t") {
el.val("/tabs ")
} else if (value == "/b") {
el.val("/bookmarks ")
} else if (value == "/h") {
el.val("/history ");
} else if (value == "/r") {
el.val("/remove ");
} else if (value == "/a") {
el.val("/actions ");
}
} else {
if (value == "/tabs" || value == "/bookmarks" || value == "/actions" || value == "/remove" || value == "/history") {
el.val("");
}
}
}
// Add protocol
function addhttp(url) {
if (!/^(?:f|ht)tps?\:\/\//.test(url)) {
url = "http://" url;
}
return url;
}
// Check if valid url
function validURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?' // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.) [a-z]{2,}|' // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' // OR ip (v4) address
'(\\:\\d )?(\\/[-a-z\\d%_.~ ]*)*' // port and path
'(\\?[;&a-z\\d%_.~ =-]*)?' // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
return !!pattern.test(str);
}
// Search for an action in the omni
function search(e) {
if (e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40 || e.keyCode == 13 || e.keyCode == 37) {
return;
}
var value = $(this).val().toLowerCase();
checkShortHand(e, value);
value = $(this).val().toLowerCase();
if (value.startsWith("/history")) {
$(".omni-item[data-index='" actions.findIndex(x => x.action == "search") "']").hide();
$(".omni-item[data-index='" actions.findIndex(x => x.action == "goto") "']").hide();
var tempvalue = value.replace("/history ", "");
var query = "";
if (tempvalue != "/history") {
query = value.replace("/history ", "");
}
chrome.runtime.sendMessage({request:"search-history", query:query}, (response) => {
populateOmniFilter(response.history);
});
} else if (value.startsWith("/bookmarks")) {
$(".omni-item[data-index='" actions.findIndex(x => x.action == "search") "']").hide();
$(".omni-item[data-index='" actions.findIndex(x => x.action == "goto") "']").hide();
var tempvalue = value.replace("/bookmarks ", "");
if (tempvalue != "/bookmarks" && tempvalue != "") {
var query = value.replace("/bookmarks ", "");
chrome.runtime.sendMessage({request:"search-bookmarks", query:query}, (response) => {
populateOmniFilter(response.bookmarks);
});
} else {
populateOmniFilter(actions.filter(x => x.type == "bookmark"));
}
} else {
if (isFiltered) {
populateOmni();
isFiltered = false;
}
$(".omni-extension #omni-list .omni-item").filter(function(){
if (value.startsWith("/tabs")) {
$(".omni-item[data-index='" actions.findIndex(x => x.action == "search") "']").hide();
$(".omni-item[data-index='" actions.findIndex(x => x.action == "goto") "']").hide();
var tempvalue = value.replace("/tabs ", "");
if (tempvalue == "/tabs") {
$(this).toggle($(this).attr("data-type") == "tab");
} else {
tempvalue = value.replace("/tabs ", "");
$(this).toggle(($(this).find(".omni-item-name").text().toLowerCase().indexOf(tempvalue) > -1 || $(this).find(".omni-item-desc").text().toLowerCase().indexOf(tempvalue) > -1) && $(this).attr("data-type") == "tab");
}
} else if (value.startsWith("/remove")) {
$(".omni-item[data-index='" actions.findIndex(x => x.action == "search") "']").hide();
$(".omni-item[data-index='" actions.findIndex(x => x.action == "goto") "']").hide();
var tempvalue = value.replace("/remove ", "")
if (tempvalue == "/remove") {
$(this).toggle($(this).attr("data-type") == "bookmark" || $(this).attr("data-type") == "tab");
} else {
tempvalue = value.replace("/remove ", "");
$(this).toggle(($(this).find(".omni-item-name").text().toLowerCase().indexOf(tempvalue) > -1 || $(this).find(".omni-item-desc").text().toLowerCase().indexOf(tempvalue) > -1) && ($(this).attr("data-type") == "bookmark" || $(this).attr("data-type") == "tab"));
}
} else if (value.startsWith("/actions")) {
$(".omni-item[data-index='" actions.findIndex(x => x.action == "search") "']").hide();
$(".omni-item[data-index='" actions.findIndex(x => x.action == "goto") "']").hide();
var tempvalue = value.replace("/actions ", "")
if (tempvalue == "/actions") {
$(this).toggle($(this).attr("data-type") == "action");
} else {
tempvalue = value.replace("/actions ", "");
$(this).toggle(($(this).find(".omni-item-name").text().toLowerCase().indexOf(tempvalue) > -1 || $(this).find(".omni-item-desc").text().toLowerCase().indexOf(tempvalue) > -1) && $(this).attr("data-type") == "action");
}
} else {
$(this).toggle($(this).find(".omni-item-name").text().toLowerCase().indexOf(value) > -1 || $(this).find(".omni-item-desc").text().toLowerCase().indexOf(value) > -1);
if (value == "") {
$(".omni-item[data-index='" actions.findIndex(x => x.action == "search") "']").hide();
$(".omni-item[data-index='" actions.findIndex(x => x.action == "goto") "']").hide();
} else if (!validURL(value)) {
$(".omni-item[data-index='" actions.findIndex(x => x.action == "search") "']").show();
$(".omni-item[data-index='" actions.findIndex(x => x.action == "goto") "']").hide();
$(".omni-item[data-index='" actions.findIndex(x => x.action == "search") "'] .omni-item-name").html('\"' value '\"');
} else {
$(".omni-item[data-index='" actions.findIndex(x => x.action == "search") "']").hide();
$(".omni-item[data-index='" actions.findIndex(x => x.action == "goto") "']").show();
$(".omni-item[data-index='" actions.findIndex(x => x.action == "goto") "'] .omni-item-name").html(value);
}
}
});
}
$(".omni-extension #omni-results").html($("#omni-extension #omni-list .omni-item:visible").length " results");
$(".omni-item-active").removeClass("omni-item-active");
$(".omni-extension #omni-list .omni-item:visible").first().addClass("omni-item-active");
}
// Handle actions from the omni
function handleAction(e) {
var action = actions[$(".omni-item-active").attr("data-index")];
closeOmni();
if ($(".omni-extension input").val().toLowerCase().startsWith("/remove")) {
chrome.runtime.sendMessage({request:"remove", type:action.type, action:action});
} else if ($(".omni-extension input").val().toLowerCase().startsWith("/history")) {
if (e.ctrlKey || e.metaKey) {
window.open($(".omni-item-active").attr("data-url"));
} else {
window.open($(".omni-item-active").attr("data-url"), "_self");
}
} else if ($(".omni-extension input").val().toLowerCase().startsWith("/bookmarks")) {
if (e.ctrlKey || e.metaKey) {
window.open($(".omni-item-active").attr("data-url"));
} else {
window.open($(".omni-item-active").attr("data-url"), "_self");
}
} else {
chrome.runtime.sendMessage({request:action.action, tab:action, query:$(".omni-extension input").val()});
switch (action.action) {
case "bookmark":
if (e.ctrlKey || e.metaKey) {
window.open(action.url);
} else {
window.open(action.url, "_self");
}
break;
case "scroll-bottom":
window.scrollTo(0,document.body.scrollHeight);
showToast(action);
break;
case "scroll-top":
window.scrollTo(0,0);
break;
case "navigation":
if (e.ctrlKey || e.metaKey) {
window.open(action.url);
} else {
window.open(action.url, "_self");
}
break;
case "fullscreen":
var elem = document.documentElement;
elem.requestFullscreen();
break;
case "new-tab":
window.open("");
break;
case "email":
window.open("mailto:");
break;
case "url":
if (e.ctrlKey || e.metaKey) {
window.open(action.url);
} else {
window.open(action.url, "_self");
}
break;
case "goto":
if (e.ctrlKey || e.metaKey) {
window.open(addhttp($(".omni-extension input").val()));
} else {
window.open(addhttp($(".omni-extension input").val()), "_self");
}
break;
case "print":
window.print();
break;
case "remove-all":
case "remove-history":
case "remove-cookies":
case "remove-cache":
case "remove-local-storage":
case "remove-passwords":
showToast(action);
break;
}
}
// Fetch actions again
chrome.runtime.sendMessage({request:"get-actions"}, (response) => {
actions = response.actions;
populateOmni();
});
}
// Customize the shortcut to open the Omni box
function openShortcuts() {
chrome.runtime.sendMessage({request:"extensions/shortcuts"});
}
// Check which keys are down
var down = [];
$(document).keydown((e) => {
down[e.keyCode] = true;
if (down[38]) {
// Up key
if ($(".omni-item-active").prevAll("div").not(":hidden").first().length) {
var previous = $(".omni-item-active").prevAll("div").not(":hidden").first();
$(".omni-item-active").removeClass("omni-item-active");
previous.addClass("omni-item-active");
previous[0].scrollIntoView({block:"nearest", inline:"nearest"});
}
} else if (down[40]) {
// Down key
if ($(".omni-item-active").nextAll("div").not(":hidden").first().length) {
var next = $(".omni-item-active").nextAll("div").not(":hidden").first();
$(".omni-item-active").removeClass("omni-item-active");
next.addClass("omni-item-active");
next[0].scrollIntoView({block:"nearest", inline:"nearest"});
}
} else if (down[27] && isOpen) {
// Esc key
closeOmni();
} else if (down[13] && isOpen) {
// Enter key
handleAction(e);
}
}).keyup((e) => {
if (down[18] && down[16] && down[80]) {
if (actions.find(x => x.action == "pin") != undefined) {
chrome.runtime.sendMessage({request:"pin-tab"});
} else {
chrome.runtime.sendMessage({request:"unpin-tab"});
}
chrome.runtime.sendMessage({request:"get-actions"}, (response) => {
actions = response.actions;
populateOmni();
});
} else if (down[18] && down[16] && down[77]) {
if (actions.find(x => x.action == "mute") != undefined) {
chrome.runtime.sendMessage({request:"mute-tab"});
} else {
chrome.runtime.sendMessage({request:"unmute-tab"});
}
chrome.runtime.sendMessage({request:"get-actions"}, (response) => {
actions = response.actions;
populateOmni();
});
} else if (down[18] && down[16] && down[67]) {
window.open("mailto:");
}
down = [];
});
// Recieve messages from background
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.request == "open-omni") {
if (isOpen) {
closeOmni();
} else {
openOmni();
}
} else if (message.request == "close-omni") {
closeOmni();
}
});
$(document).on("click", "#open-page-omni-extension-thing", openShortcuts);
$(document).on("mouseover", ".omni-extension .omni-item:not(.omni-item-active)", hoverItem);
$(document).on("keyup", ".omni-extension input", search);
$(document).on("click", ".omni-item-active", handleAction);
$(document).on("click", ".omni-extension #omni-overlay", closeOmni);
});