-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementation.js
More file actions
38 lines (34 loc) · 864 Bytes
/
implementation.js
File metadata and controls
38 lines (34 loc) · 864 Bytes
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
function get_web_search_results(params, userSettings) {
var keyword = params.keyword;
var cx = userSettings.searchEngineID;
var key = userSettings.searchEngineAPIKey;
if (!cx || !key) {
throw new Error(
'Please set the Search Engine ID and API Key in the plugin settings.'
);
}
return fetch(
`https://customsearch.googleapis.com/customsearch/v1?q=${encodeURIComponent(
keyword
)}&key=${key}&cx=${cx}`
)
.then((r) => r.json())
.then((response) => {
if (response.error) {
throw new Error('Error: ' + response.error.message);
}
const items = response.items;
if (!items) {
return 'No results found';
}
return items
.map(
(item) => `
Title: ${item.title}
Result: ${item.snippet}
URL: ${item.link}
`
)
.join('');
});
}