-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
196 lines (169 loc) · 5.35 KB
/
background.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
// Contains the suggestions returned by IPFS Search
var storedSuggestions = [];
// IPFS Gateway/API, will be updated after reading from storage
var gateway = "https://gateway.ipfs.io/ipfs/";
// Default action
var defaultAction = "search";
// Cache size
var cacheSize = 100;
// Latest omnibox text
var omniboxText = "";
// Read from the options storage
function updateOptions() {
chrome.storage.sync.get({
defaultAction: "search",
gateway: "https://gateway.ipfs.io/ipfs/",
cacheSize: 100
}, function (items) {
defaultAction = items.defaultAction;
gateway = items.gateway;
cacheSize = items.cacheSize;
});
}
// Jump to suggested URL
function goToPage(hash) {
let jumpGateway = gateway;
if (jumpGateway[jumpGateway.length - 1] != "/") {
jumpGateway += "/";
}
let jumpURL = jumpGateway + hash;
chrome.tabs.update(null, {
"url": jumpURL
}, function () {
console.log("Executing jump to " + jumpURL);
});
}
// Search on IPFS (default)
function standardSearch(text) {
let searchURL = "https://ipfs-search.com/#/search?search=" + text;
chrome.tabs.update({
"url": searchURL
}, function () {
console.log("Executing search: " + searchURL);
});
}
// Query the IPFS search API
function querySearch(text) {
let cachedSuggestion = getCachedSuggestion(text);
if (cachedSuggestion == null) {
fetch("https://api.ipfs-search.com/v1/search?q=" + encodeURI(text) + "&type=any&page=1", {
mode: 'cors'
})
.then(function (response) {
return response.json();
})
.then(function (myJson) {
parseSuggestions(myJson, text);
})
.catch(function (error) {
console.log("Error: " + error);
});
} else {
// If the user hasn't already inserted another query, provide
// the suggestion
if (omniboxText == text) {
setDefaultSuggestion(cachedSuggestion.title, text);
}
}
}
function getCachedSuggestion(text) {
text = text.trim();
let result = storedSuggestions.find((el) => el.key == text);
if (result == undefined) {
return null;
}
return result.value;
}
// Sets the default suggestion for the Omnibox
function setDefaultSuggestion(title, originalText) {
let suggestionText = "";
let useDefault = isDefault(originalText);
let cachedSuggestion = getCachedSuggestion(originalText);
let foundResult = (cachedSuggestion != null) && (title != null);
if (foundResult) {
suggestionText += title;
}
suggestionText += "<dim>";
if (foundResult) {
suggestionText += " | "
}
if (defaultAction === "go") {
if (!useDefault || !foundResult) {
suggestionText += "Search on IPFS";
} else {
suggestionText += "Jump to Page (Space + Enter to search)";
}
} else {
// Includes both "search" and any unrecognized value
if (useDefault || !foundResult) {
suggestionText += "Search on IPFS (Space + Enter to jump to page)";
} else {
suggestionText += "Jump to Page";
}
if (defaultAction !== "search") {
console.log(`Unrecognized action ${defaultAction}.`);
}
}
suggestionText += "</dim>";
// Chrome only allows one suggestion from an extension
chrome.omnibox.setDefaultSuggestion({
description: (suggestionText)
});
}
// An action is treated as non-default if its query ends with a space
function isDefault(text) {
return text.charAt(text.length - 1) !== " ";
}
// Handle the suggestion received from the IPFS API
function parseSuggestions(jsonResponse, originalText) {
// Set first suggestion as the default suggestion
console.log("Received suggestion response: " + jsonResponse);
let hit = jsonResponse.hits[0];
if (hit != null) {
if (storedSuggestions.length > cacheSize) {
storedSuggestions.shift();
}
storedSuggestions.push({
key: originalText,
value: {
title: hit.title,
hash: hit.hash
}
});
}
// If the user hasn't already inserted another query, provide
// the suggestion
if (omniboxText == originalText) {
setDefaultSuggestion(hit == null ? null : hit.title, originalText);
}
}
// Fired when the user changes the content of the omnibox
chrome.omnibox.onInputChanged.addListener(
function (text, suggest) {
updateOptions();
omniboxText = text;
querySearch(text);
});
// Fired when the user accepts the suggestion
chrome.omnibox.onInputEntered.addListener(
function (text) {
let cachedSuggestion = getCachedSuggestion(text);
if (cachedSuggestion == null) {
// In case of error, fall back to search
standardSearch(text);
return;
}
if (isDefault(text)) {
if (defaultAction === "go") {
goToPage(cachedSuggestion.hash, text);
} else {
standardSearch(text);
}
} else {
if (defaultAction === "go") {
standardSearch(text);
} else {
goToPage(cachedSuggestion.hash, text);
}
}
});