-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathextension.js
More file actions
303 lines (275 loc) · 9.6 KB
/
Copy pathextension.js
File metadata and controls
303 lines (275 loc) · 9.6 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
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
import St from 'gi://St';
import Gio from 'gi://Gio';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
class ChromeSearchProvider {
constructor(extension) {
this._extension = extension;
this.providers = {
'open-link': {
name: 'Open Link',
description: 'Open link in browser',
icon: 'web-browser-symbolic',
getQuery: function (terms) {
return `https://${terms.join(" ")}`;
}
},
'google': {
name: 'Search Google',
description: 'Search online with Google',
icon: 'web-browser-symbolic',
getQuery: function (terms) {
const query = terms.join(" ");
return `https://www.google.com/search?q=${encodeURIComponent(query)}`;
}
},
'duckduckgo': {
name: 'Search DuckDuckGo',
description: 'Search online with DuckDuckGo',
icon: 'web-browser-symbolic',
getQuery: function (terms) {
const query = terms.slice(1).join(" ");
return `https://duckduckgo.com/?q=${encodeURIComponent(query)}`;
}
},
'bing': {
name: 'Search Bing',
description: 'Search online with Bing',
icon: 'web-browser-symbolic',
getQuery: function (terms) {
const query = terms.slice(1).join(" ");
return `https://www.bing.com/search?q=${encodeURIComponent(query)}`;
}
},
'youtube': {
name: 'Search YouTube',
description: 'Search online with YouTube',
icon: 'web-browser-symbolic',
getQuery: function (terms) {
const query = terms.slice(1).join(" ");
return `https://www.youtube.com/results?search_query=${encodeURIComponent(query)}`;
}
},
};
}
/**
* The application of the provider.
*
* Applications will return a `Gio.AppInfo` representing themselves.
* Extensions will usually return `null`.
*
* @type {Gio.AppInfo}
*/
get appInfo() {
return null;
}
/**
* Whether the provider offers detailed results.
*
* Applications will return `true` if they have a way to display more
* detailed or complete results. Extensions will usually return `false`.
*
* @type {boolean}
*/
get canLaunchSearch() {
return false;
}
/**
* The unique ID of the provider.
*
* Applications will return their application ID. Extensions will usually
* return their UUID.
*
* @type {string}
*/
get id() {
return this._extension.uuid;
}
/**
* Launch the search result.
*
* This method is called when a search provider result is activated.
*
* @param {string} result - The result identifier
* @param {string[]} terms - The search terms
*/
activateResult(result, terms) {
const query = this.providers[result].getQuery(terms);
Gio.AppInfo.launch_default_for_uri(query, null);
}
/**
* Create a result object.
*
* This method is called to create an actor to represent a search result.
*
* Implementations may return any `Clutter.Actor` to serve as the display
* result, or `null` for the default implementation.
*
* @param {ResultMeta} meta - A result metadata object
* @returns {Clutter.Actor|null} An actor for the result
*/
createResultObject(meta) {
console.debug(`createResultObject(${meta.id})`);
return null;
}
/**
* Get result metadata.
*
* This method is called to get a `ResultMeta` for each identifier.
*
* If @cancellable is triggered, this method should throw an error.
*
* @async
* @param {string[]} results - The result identifiers
* @param {Gio.Cancellable} cancellable - A cancellable for the operation
* @returns {Promise<ResultMeta[]>} A list of result metadata objects
*/
getResultMetas(results, cancellable) {
const { scaleFactor } = St.ThemeContext.get_for_stage(global.stage);
return new Promise((resolve, reject) => {
const cancelledId = cancellable.connect(
() => reject(Error('Operation Cancelled')));
const resultMetas = [];
for (const identifier of results) {
const provider = this.providers[identifier];
const meta = {
id: identifier,
name: provider.name,
description: provider.description,
// clipboardText: 'Content for the clipboard',
createIcon: size => {
return new St.Icon({
icon_name: provider.icon,
width: size * scaleFactor,
height: size * scaleFactor,
});
},
};
resultMetas.push(meta);
}
cancellable.disconnect(cancelledId);
if (!cancellable.is_cancelled())
resolve(resultMetas);
});
}
/**
* Checks if a string is a valid
*
* @param {string} string
* @returns
*/
isValidUrl(string) {
const urlPattern = new RegExp(
'^' +
// Protocol (optional)
'(https?:\\/\\/)?' +
// Domain or IP (optional)
'((([a-zA-Z\\d]([a-zA-Z\\d-]*[a-zA-Z\\d])*)\\.)+[a-zA-Z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3})?)' + // OR ip (v4) address
// Port (optional)
'(\\:\\d+)?' +
// Path (optional)
'(\\/[-a-zA-Z\\d%_.~+]*)*' +
// Query string (optional)
'(\\?[;&a-zA-Z\\d%_.~+=-]*)?' +
// Fragment (optional)
'(\\#[-a-zA-Z\\d_]*)?$',
'i'
);
return !!urlPattern.test(string);
}
/**
* Initiate a new search.
*
* This method is called to start a new search and should return a list of
* unique identifiers for the results.
*
* If @cancellable is triggered, this method should throw an error.
*
* @async
* @param {string[]} terms - The search terms
* @param {Gio.Cancellable} cancellable - A cancellable for the operation
* @returns {Promise<string[]>} A list of result identifiers
*/
getInitialResultSet(terms, cancellable) {
const identifiers = [];
if (this.isValidUrl(terms[0])) {
identifiers.push('open-link')
}
switch (terms[0]) {
case 'd':
identifiers.push('duckduckgo');
break;
case 'b':
identifiers.push('bing');
break;
case 'y':
identifiers.push('youtube');
break;
case 'g':
identifiers.push('google');
break;
default:
identifiers.push('google')
break;
}
return new Promise((resolve, reject) => {
const cancelledId = cancellable.connect(
() => reject(Error('Search Cancelled')));
cancellable.disconnect(cancelledId);
if (!cancellable.is_cancelled())
resolve(identifiers);
});
}
/**
* Refine the current search.
*
* This method is called to refine the current search results with
* expanded terms and should return a subset of the original result set.
*
* Implementations may use this method to refine the search results more
* efficiently than running a new search, or simply pass the terms to the
* implementation of `getInitialResultSet()`.
*
* If @cancellable is triggered, this method should throw an error.
*
* @async
* @param {string[]} results - The original result set
* @param {string[]} terms - The search terms
* @param {Gio.Cancellable} cancellable - A cancellable for the operation
* @returns {Promise<string[]>}
*/
getSubsearchResultSet(results, terms, cancellable) {
if (cancellable.is_cancelled())
throw Error('Search Cancelled');
return this.getInitialResultSet(terms, cancellable);
}
/**
* Filter the current search.
*
* This method is called to truncate the number of search results.
*
* Implementations may use their own criteria for discarding results, or
* simply return the first n-items.
*
* @param {string[]} results - The original result set
* @param {number} maxResults - The maximum amount of results
* @returns {string[]} The filtered results
*/
filterResults(results, maxResults) {
console.debug(`filterResults([${results}], ${maxResults})`);
if (results.length <= maxResults)
return results;
return results.slice(0, maxResults);
}
}
export default class ChromeSearchProviderExtension extends Extension {
enable() {
this._provider = new ChromeSearchProvider(this);
Main.overview.searchController.addProvider(this._provider);
}
disable() {
Main.overview.searchController.removeProvider(this._provider);
this._provider = null;
}
}