Repository navigation
Expand file tree
/
Copy pathoptions.js
More file actions
317 lines (290 loc) · 13.4 KB
/
Copy pathoptions.js
File metadata and controls
317 lines (290 loc) · 13.4 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
'use strict';
const api = globalThis.NotandiaPublisherProfiles;
if (!api) throw new Error('Publisher profile runtime failed to load');
const $ = id => document.getElementById(id);
const profileList = $('profileList');
const status = $('status');
let watchlist = api.defaultSettings();
const writeStatus = globalThis.NotandiaStatusMessages.create(status, 5000);
function setStatus(message, error = false) {
writeStatus(message);
status.classList.toggle('error', error);
}
function usesFirefoxDataConsent() {
const optional = chrome.runtime.getManifest().browser_specific_settings?.gecko?.data_collection_permissions?.optional;
return Array.isArray(optional) && optional.includes('websiteContent') && Boolean(globalThis.browser?.permissions);
}
function selectOption(select, value) {
const option = Array.from(select?.options || []).find(item => item.value === value);
if (option) option.selected = true;
}
async function hasFirefoxDataConsent() {
if (!usesFirefoxDataConsent()) return true;
const permissions = await browser.permissions.getAll();
return Array.isArray(permissions.data_collection) && permissions.data_collection.includes('websiteContent');
}
async function setFirefoxDataConsent(enabled) {
if (!usesFirefoxDataConsent()) return true;
if (enabled) return browser.permissions.request({ data_collection: ['websiteContent'] });
await browser.permissions.remove({ data_collection: ['websiteContent'] });
return true;
}
function appendOptions(select, choices, selected) {
for (const [value, label] of choices) {
const option = document.createElement('option');
option.value = value;
option.textContent = label;
option.selected = value === selected;
select.appendChild(option);
}
}
function renderProfiles() {
profileList.replaceChildren();
watchlist = api.sanitizeSettings(watchlist);
$('communityWarningsEnabledOptions').checked = watchlist.communityWarningsEnabled;
for (const profile of watchlist.profiles) {
const row = document.createElement('div');
row.className = 'profile-row';
row.dataset.profileId = profile.id;
const details = document.createElement('div');
details.className = 'profile-main';
const name = document.createElement('strong');
name.textContent = profile.name;
if (profile.source === 'preset') {
const chip = document.createElement('span');
chip.className = 'profile-chip';
chip.textContent = api.profileContext(profile.id) ? 'Publishing concerns' : 'Identification preset';
name.appendChild(chip);
}
const evidence = document.createElement('small');
evidence.textContent = [profile.domains.join(', '), profile.doiPrefixes.join(', ')].filter(Boolean).join(' · ');
details.append(name, evidence);
if (['builtin', 'preset'].includes(profile.source)) {
const context = api.profileContext(profile.id);
if (context) {
const rationale = document.createElement('details');
rationale.className = 'profile-rationale';
const summary = document.createElement('summary');
summary.textContent = `Why ${profile.name} is included`;
const explanation = document.createElement('p');
explanation.textContent = `${context.explanation} ${context.evidence} Sources reviewed ${context.reviewed}.`;
rationale.append(summary, explanation);
for (const source of context.sources) {
const link = document.createElement('a');
link.href = source.url;
link.textContent = source.label;
link.target = '_blank';
link.rel = 'noopener noreferrer';
rationale.appendChild(link);
}
details.appendChild(rationale);
if (context.testUrl) {
const testLink = document.createElement('a');
testLink.className = 'profile-test-link';
testLink.href = context.testUrl;
testLink.textContent = 'Open test page';
testLink.setAttribute('aria-label', `Open test page for ${profile.name}`);
testLink.target = '_blank';
testLink.rel = 'noopener noreferrer';
details.appendChild(testLink);
}
} else {
const description = document.createElement('small');
description.textContent = 'Added for your own publisher filters.';
details.appendChild(description);
}
}
const enabledLabel = document.createElement('label');
enabledLabel.className = 'switch-wrap';
const enabled = document.createElement('input');
enabled.type = 'checkbox';
enabled.className = 'profile-enabled';
enabled.checked = profile.enabled;
enabled.setAttribute('aria-label', `Enable ${profile.name}`);
enabledLabel.append(enabled, document.createTextNode('Enable'));
const actionLabel = document.createElement('label');
actionLabel.textContent = 'Action';
const action = document.createElement('select');
action.className = 'profile-action';
action.setAttribute('aria-label', `${profile.name} action`);
appendOptions(action, [
['none', 'Context only'],
['badge', 'Badge only'],
['highlight', 'Highlight'],
['dim', 'Dim'],
['hide', 'Hide']
], profile.action);
actionLabel.appendChild(action);
const colorLabel = document.createElement('label');
colorLabel.textContent = 'Color';
const color = document.createElement('input');
color.type = 'color';
color.className = 'profile-color';
color.value = profile.color;
color.setAttribute('aria-label', `${profile.name} color`);
colorLabel.appendChild(color);
const confidenceLabel = document.createElement('label');
confidenceLabel.textContent = 'Match policy';
const confidence = document.createElement('select');
confidence.className = 'profile-confidence';
confidence.setAttribute('aria-label', `${profile.name} match policy`);
appendOptions(confidence, [
['confirmed-only', 'Confirmed only'],
['confirmed-and-potential', 'Include potential']
], profile.confidencePolicy);
confidenceLabel.appendChild(confidence);
const remove = document.createElement('button');
remove.type = 'button';
remove.className = 'remove-profile';
remove.title = profile.source === 'builtin' ? 'Built-in profiles cannot be removed, but they can be disabled.' : `Remove ${profile.name}`;
remove.setAttribute('aria-label', remove.title);
remove.textContent = '×';
remove.disabled = profile.source === 'builtin';
row.append(details, enabledLabel, actionLabel, colorLabel, confidenceLabel, remove);
profileList.appendChild(row);
}
}
function readRows() {
const byId = new Map(watchlist.profiles.map(profile => [profile.id, profile]));
for (const row of profileList.querySelectorAll('.profile-row')) {
const profile = byId.get(row.dataset.profileId);
if (!profile) continue;
profile.enabled = row.querySelector('.profile-enabled').checked;
profile.action = row.querySelector('.profile-action').value;
profile.color = row.querySelector('.profile-color').value;
profile.confidencePolicy = row.querySelector('.profile-confidence').value;
}
watchlist = api.sanitizeSettings({ ...watchlist, profiles: Array.from(byId.values()), communityWarningsEnabled: $('communityWarningsEnabledOptions').checked });
return watchlist;
}
function slugify(value) {
return String(value || '').toLowerCase().trim().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 48);
}
function commaValues(value) {
return String(value || '').split(',').map(part => part.trim()).filter(Boolean);
}
$('addProfile').addEventListener('click', () => {
readRows();
const name = $('customName').value.trim();
const baseId = slugify(name);
if (!baseId) return setStatus('Enter a publisher name.', true);
if (watchlist.profiles.length >= 50) return setStatus('Remove a publisher before adding another. The limit is 50.', true);
let id = baseId;
const existingIds = new Set(watchlist.profiles.map(profile => profile.id));
let suffix = 2;
while (existingIds.has(id)) id = `${baseId.slice(0, 42)}-${suffix++}`;
const profile = api.normalizeProfile({
id, name,
domains: commaValues($('customDomains').value),
doiPrefixes: commaValues($('customDoiPrefixes').value),
enabled: true, action: $('customAction').value, color: $('customColor').value,
confidencePolicy: 'confirmed-only'
});
if (!profile) return setStatus('Enter a valid domain or DOI prefix.', true);
watchlist.profiles.push(profile);
for (const field of ['customName', 'customDomains', 'customDoiPrefixes']) $(field).value = '';
renderProfiles();
profileList.querySelector(`[data-profile-id="${profile.id}"] .profile-enabled`)?.focus();
setStatus(`${profile.name} added. Save settings to apply it.`);
});
profileList.addEventListener('click', event => {
const button = event.target.closest('.remove-profile');
if (!button || button.disabled) return;
readRows();
const id = button.closest('.profile-row').dataset.profileId;
watchlist.profiles = watchlist.profiles.filter(profile => profile.id !== id || profile.source === 'builtin');
renderProfiles();
profileList.querySelector('.profile-enabled')?.focus();
setStatus('Publisher removed. Save settings to apply the change.');
});
$('exportProfiles').addEventListener('click', () => {
readRows();
$('profileJson').value = JSON.stringify(watchlist, null, 2);
$('profileJson').focus();
$('profileJson').select();
setStatus('Publisher settings exported. Copy the JSON below.');
});
$('importProfiles').addEventListener('click', () => {
try {
const parsed = JSON.parse($('profileJson').value);
const sanitized = api.sanitizeSettings(parsed);
if (!Array.isArray(parsed?.profiles) || !sanitized.profiles.length) throw new Error('No valid profiles');
watchlist = sanitized;
renderProfiles();
setStatus('Profiles imported. Save settings to apply them.');
} catch {
setStatus('Enter a JSON export containing publisher profiles.', true);
}
});
$('resetProfiles').addEventListener('click', () => {
watchlist = api.defaultSettings();
renderProfiles();
setStatus('Default MDPI and Frontiers profiles restored. Save settings to apply them.');
});
$('save').addEventListener('click', () => {
void (async () => {
readRows();
const integrityEnabled = $('integrityLookupsEnabledOptions').checked;
const ncbiEnabled = $('ncbiApiEnabledOptions').checked;
const consent = await setFirefoxDataConsent(integrityEnabled || ncbiEnabled).catch(() => false);
if ((integrityEnabled || ncbiEnabled) && !consent) {
$('integrityLookupsEnabledOptions').checked = false;
$('ncbiApiEnabledOptions').checked = false;
return setStatus('Firefox data permission was not granted.', true);
}
const mdpi = watchlist.profiles.find(profile => profile.id === 'mdpi');
chrome.storage.sync.set({
publisherWatchlist: watchlist,
interfaceLanguage: $('interfaceLanguageOptions').value,
integrityLookupsEnabled: integrityEnabled,
ncbiApiEnabled: ncbiEnabled,
loggingEnabled: $('loggingEnabledOptions').checked,
mode: mdpi?.action === 'hide' ? 'hide' : 'highlight',
highlightPotentialMdpiSites: mdpi?.confidencePolicy === 'confirmed-and-potential',
potentialMdpiHighlightColor: mdpi?.color || api.BUILTIN_PROFILES[0].color
}, () => {
if (chrome.runtime.lastError) return setStatus('Could not save settings.', true);
chrome.storage.local?.set({ notandiaLookupSetupDismissed: true }, () => void chrome.runtime.lastError);
chrome.tabs?.query?.({}, tabs => {
for (const tab of tabs || []) {
if (!Number.isInteger(tab.id)) continue;
chrome.tabs.sendMessage(tab.id, { type: 'notandiaSetInterfaceLanguage', language: $('interfaceLanguageOptions').value }, () => void chrome.runtime.lastError);
chrome.tabs.sendMessage(tab.id, { type: 'forcePublisherRescan' }, () => void chrome.runtime.lastError);
chrome.tabs.sendMessage(tab.id, { type: 'forceIntegrityRescan' }, () => void chrome.runtime.lastError);
}
});
setStatus('Settings saved.');
globalThis.NotandiaI18n?.setLanguage($('interfaceLanguageOptions').value);
});
})();
});
function load() {
chrome.storage.sync.get({
publisherWatchlist: null,
mode: null,
highlightPotentialMdpiSites: null,
potentialMdpiHighlightColor: null,
integrityLookupsEnabled: false,
ncbiApiEnabled: false,
loggingEnabled: false,
interfaceLanguage: 'system'
}, stored => {
if (chrome.runtime.lastError) return setStatus('Could not load settings.', true);
watchlist = api.migrateLegacySettings(stored);
selectOption($('interfaceLanguageOptions'), ['system', 'en', 'it'].includes(stored.interfaceLanguage) ? stored.interfaceLanguage : 'system');
renderProfiles();
$('ncbiApiEnabledOptions').checked = stored.ncbiApiEnabled === true;
$('loggingEnabledOptions').checked = stored.loggingEnabled === true;
void hasFirefoxDataConsent().then(permitted => {
$('integrityLookupsEnabledOptions').checked = stored.integrityLookupsEnabled === true && permitted;
$('ncbiApiEnabledOptions').checked = stored.ncbiApiEnabled === true && permitted;
}).catch(() => {
$('integrityLookupsEnabledOptions').checked = false;
$('ncbiApiEnabledOptions').checked = false;
});
if (!stored.publisherWatchlist || stored.publisherWatchlist.schemaVersion !== api.SCHEMA_VERSION) {
chrome.storage.sync.set({ publisherWatchlist: watchlist });
}
});
}
document.addEventListener('DOMContentLoaded', load);