-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
132 lines (115 loc) · 3.12 KB
/
main.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
window.addEventListener("load",init);
function _$(selector){
return document.querySelector(selector);
}
function _$$(selector){
return document.querySelectorAll(selector);
}
function init(){
searchText = "";
bookmarkResults = [];
historyResults = [];
topSites = [];
chrome.topSites.get((results)=>{
topSites = results;
renderResults();
});
//createContextMenuItem("warpzone","Add to WarpZone");
_$(".search-screen input").addEventListener("input",onSearchInput);
_$(".search-screen input").addEventListener("keydown",onSearchBarKeyDown);
_$(".search-screen form").addEventListener("submit",onSearchSubmit)
}
function createContextMenuItem(id,title,parentId,onclick){
var properties = {
contexts:["all"],
title: title,
id: id
}
if(parentId){
properties.parentId = parentId;
}
if(onclick){
properties.onclick = onclick;
}
chrome.contextMenus.create(properties, ()=>{
console.log("Context Item Created");
});
}
function onSearchBarKeyDown(e){
if(e.key=="ArrowDown"){
e.preventDefault();
var current = _$(".results .result.active");
if(current){
var next = _$(".results .result.active ~ .result");
if(next){
current.classList.remove("active");
next.classList.add("active");
next.scrollIntoViewIfNeeded();
}
}
else{
var next = _$(".results .result");
if(next) {
next.classList.add("active");
next.scrollIntoViewIfNeeded();
}
}
}
if(e.key=="ArrowUp"){
e.preventDefault();
var results = _$$(".result");
for (var i=1;i<results.length;i++){
if(results[i].classList.contains("active")){
results[i].classList.remove("active");
results[i-1].classList.add("active");
results[i-1].scrollIntoViewIfNeeded();
}
}
}
}
function onSearchInput(e){
renderResults();
searchText = e.target.value.trim();
chrome.bookmarks.search(searchText, (results) => {
bookmarkResults = [];
if(searchText) bookmarkResults = results;
chrome.history.search({maxResults:20,text: searchText}, (results) => {
historyResults = [];
if(searchText) historyResults = results;
renderResults();
});
});
}
function onSearchSubmit(e){
e.preventDefault();
var currentLink = _$(".result.active a");
if(currentLink) currentLink.click();
}
function renderResultLink(obj){
return `<div class="result"><a href="${obj.url}"><p>${obj.title}</p><p>${obj.url}</p></a></div>`;
}
function renderResults(){
var html = "";
if(bookmarkResults.length){
html += "<h3>Bookmarks</h3>";
bookmarkResults.slice(0,10).forEach((r) => {
html += renderResultLink(r);
});
}
if(historyResults.length){
html += "<h3>History</h3>";
historyResults.slice(0,10).forEach((r) => {
html += renderResultLink(r);
});
}
if(!(bookmarkResults.length + historyResults.length)){
if(topSites){
html += "<h3>Top Sites</h3>";
topSites.slice(0,10).forEach((r) => {
html += renderResultLink(r);
});
}
}
_$(".results").innerHTML = html;
_$(".results .result").classList.add("active");
}