forked from thunderbird/extension-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
211 lines (172 loc) · 5.01 KB
/
script.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
/* global lunr */
// If we consider to use google sheets as data source, these might be useful:
// const slugMatch = /\/addon\/([^\/]+)\//;
// const sheetId = '1ZzheVRDnEpAwdQ3eHDVI6Hu5om5zhp2YtSCeB0mmLUQ';
// const url = `https://spreadsheets.google.com/feeds/list/${sheetId}/1/public/full?alt=json`;
// const U_NAME_FIELD = "u_name"; //.gsx$legacycontent.$t,
// const R_NAME_FIELD = "r_name"; //.gsx$webextensionreplacement.$t,
// const R_LINK_FIELD = "r_link"; //.gsx$url.$t
async function dataToJSON(data) {
let entries = [];
let lines = data.split(/\r\n|\n/);
let i = 0;
do
{
let entry = {};
while (i < lines.length) {
i++;
let line = lines[i-1].trim();
// End of Block
if (line.startsWith("---")) {
break;
}
// Skip comments.
if (line.startsWith("#")) {
continue;
}
let parts = line.split(":");
let key = parts.shift().trim();
if (key) {
let value = parts.join(":").trim();
entry[key] = value;
}
}
// Add found entry.
if (Object.keys(entry).length > 0) {
entries.push(entry);
}
} while (i < lines.length);
return entries;
}
const templates = {
results: {
addon: $('#search-result-addon'),
general: $('#search-result-general'),
empty: $('#search-result-empty')
}
}
function stamp(template, cb) {
let el = document.importNode(template.content, true);
cb(sel => el.querySelector(sel));
return el;
}
function $(selector, parent=document) {
return parent.querySelector(selector);
}
async function loadData() {
let url = "https://raw.githubusercontent.com/thundernest/extension-finder/master/data.yaml"
return fetch(url).then(r => r.text()).then(dataToJSON);
}
function buildIndex(data) {
let b = new lunr.Builder();
b.field('name'); //search field
b.ref('idx'); // unique index reference
let addons = {};
data.forEach(e => { // google sheets will need in data.feed.entry
let record = process(e);
b.add(record);
addons[record.idx] = record;
});
let idx = b.build();
console.log({idx, addons})
return { idx, addons };
}
function process(entry) {
let obj = {
idx: entry["r_name"],
name: entry["u_name"],
suggested: {
name: entry["r_name"],
url: entry["r_link"],
id: entry["r_id"],
desc: entry["r_desc"],
}
};
return obj;
}
function init({ idx, addons }) {
let input = $('input');
let outEl = $('.out');
let allAddons = Object.values(addons).sort((a, b) => (a.name.toLowerCase() > b.name.toLowerCase()) ? 1 : -1);
function search(query) {
let results, out;
if (query) {
results = idx.search('*' + query + '*');
out = results.map(r => addons[r.ref]);
} else {
out = allAddons;
}
outEl.innerHTML = '';
if (out.length) {
out.forEach(r => outEl.appendChild(resultRow(r)));
} else {
outEl.appendChild(emptyResult(query));
}
}
input.addEventListener('input', function (e) {
let query = input.value.trim();
search(query);
}, { passive: true });
input.setAttribute('placeholder', 'name of unmaintained extension');
input.disabled = false;
let loc = new URL(window.location);
let query = loc.searchParams.get('q') || input.value;
if (query) {
input.value = query;
search(query);
} else {
search();
}
input.focus();
}
function resultRow(result) {
if (result.suggested.id) {
return addonResult(result);
}
return generalResult(result);
}
let cachedAddons = {};
function getAddonData(id) {
return new Promise((resolve, reject) => {
if (id in cachedAddons) {
resolve(cachedAddons[id]);
} else {
let p = fetch(`https://addons.thunderbird.net/api/v4/addons/addon/${id}/`).then(r => r.json())
p.then(data => cachedAddons[id] = p);
resolve(p);
}
});
}
function addonResult(result) {
return stamp(templates.results.addon, $ => {
$('.legacy-name').textContent = result.name;
$('.alt-name').textContent = result.suggested.name;
$('.cta .button').setAttribute('href', result.suggested.url);
let authorEl = $('.alt-author');
let iconEl = $('.icon');
getAddonData(result.suggested.id)
.then(data => {
authorEl.textContent = data.authors.map(a => a.name).join(', ');
iconEl.src = data.icon_url;
}).catch(console.error);
});
}
function generalResult(result) {
return stamp(templates.results.general, $ => {
$('.legacy-name').textContent = result.name;
$('.alt-name').textContent = result.suggested.name;
$('.cta .button').setAttribute('href', result.suggested.url);
if (result.suggested.desc) {
$('.alt-desc').insertAdjacentHTML('afterbegin', result.suggested.desc);
}
});
}
function emptyResult(query) {
return stamp(templates.results.empty, $=> {
$('.query').textContent = query;
$('.button').href = `https://addons.thunderbird.net/search/?q=${query}&appver=78.0`;
});
}
window.addEventListener('load', function (e) {
loadData().then(buildIndex).then(init);
});