-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathcommon.js
715 lines (644 loc) · 24.5 KB
/
common.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
'use strict'
/* eslint-env browser, webextensions */
/* global optionDefaults */
// INIT
// ===================================================================
var ipfs // ipfs-api instance
var state = {} // avoid redundant API reads by utilizing local cache of various states
// init happens on addon load in background/background.js
// eslint-disable-next-line no-unused-vars
async function init () {
try {
const options = await browser.storage.local.get(optionDefaults)
ipfs = initIpfsApi(options.ipfsApiUrl)
initStates(options)
registerListeners()
setApiStatusUpdateInterval(options.ipfsApiPollMs)
await storeMissingOptions(options, optionDefaults)
} catch (error) {
console.error('Unable to initialize addon due to error', error)
notify('notify_addonIssueTitle', 'notify_addonIssueMsg')
}
}
function initIpfsApi (ipfsApiUrl) {
const url = new URL(ipfsApiUrl)
return window.IpfsApi({host: url.hostname, port: url.port, procotol: url.protocol})
}
function initStates (options) {
// we store the most used values in optimized form
// to minimize performance impact on overall browsing experience
state.pubGwURL = new URL(options.publicGatewayUrl)
state.pubGwURLString = state.pubGwURL.toString()
state.redirect = options.useCustomGateway
state.apiURL = new URL(options.ipfsApiUrl)
state.apiURLString = state.apiURL.toString()
state.gwURL = new URL(options.customGatewayUrl)
state.gwURLString = state.gwURL.toString()
state.automaticMode = options.automaticMode
state.linkify = options.linkify
state.dnslink = options.dnslink
state.catchUnhandledProtocols = options.catchUnhandledProtocols
state.displayNotifications = options.displayNotifications
state.dnslinkCache = /* global LRUMap */ new LRUMap(1000)
}
function registerListeners () {
browser.webRequest.onBeforeSendHeaders.addListener(onBeforeSendHeaders, {urls: ['<all_urls>']}, ['blocking', 'requestHeaders'])
browser.webRequest.onBeforeRequest.addListener(onBeforeRequest, {urls: ['<all_urls>']}, ['blocking'])
browser.storage.onChanged.addListener(onStorageChange)
browser.tabs.onUpdated.addListener(onUpdatedTab)
browser.runtime.onMessage.addListener(onRuntimeMessage)
browser.runtime.onConnect.addListener(onRuntimeConnect)
}
// REDIRECT
// ===================================================================
function publicIpfsOrIpnsResource (url) {
// first, exclude gateway and api, otherwise we have infinite loop
if (!url.startsWith(state.gwURLString) && !url.startsWith(state.apiURLString)) {
// /ipfs/ is easy to validate, we just check if CID is correct and return if true
if (window.IsIpfs.ipfsUrl(url)) {
return true
}
// /ipns/ requires multiple stages/branches, as it can be FQDN with dnslink or CID
if (window.IsIpfs.ipnsUrl(url) && validIpnsPath(new URL(url).pathname)) {
return true
}
}
// everything else is not ipfs-related
return false
}
function validIpnsPath (path) {
if (window.IsIpfs.ipnsPath(path)) {
// we may have false-positives here, so we do additional checks below
const ipnsRoot = path.match(/^\/ipns\/([^/]+)/)[1]
// console.log('==> IPNS root', ipnsRoot)
// first check if root is a regular CID
if (window.IsIpfs.cid(ipnsRoot)) {
// console.log('==> IPNS is a valid CID', ipnsRoot)
return true
}
if (isDnslookupPossible() && cachedDnslinkLookup(ipnsRoot)) {
// console.log('==> IPNS for FQDN with valid dnslink: ', ipnsRoot)
return true
}
}
return false
}
function validIpfsOrIpnsPath (path) {
return window.IsIpfs.ipfsPath(path) || validIpnsPath(path)
}
function redirectToCustomGateway (requestUrl) {
const url = new URL(requestUrl)
url.protocol = state.gwURL.protocol
url.host = state.gwURL.host
url.port = state.gwURL.port
return { redirectUrl: url.toString() }
}
// HTTP Request Hooks
// ===================================================================
function onBeforeSendHeaders (request) {
if (request.url.startsWith(state.apiURLString)) {
// For some reason js-ipfs-api sent requests with "Origin: null" under Chrome
// which produced '403 - Forbidden' error.
// This workaround removes bogus header from API requests
for (let i = 0; i < request.requestHeaders.length; i++) {
let header = request.requestHeaders[i]
if (header.name === 'Origin' && (header.value == null || header.value === 'null')) {
request.requestHeaders.splice(i, 1)
break
}
}
}
return {
requestHeaders: request.requestHeaders
}
}
function onBeforeRequest (request) {
// skip requests to the custom gateway or API (otherwise we have too much recursion)
if (request.url.startsWith(state.gwURLString) || request.url.startsWith(state.apiURLString)) {
return
}
// poor-mans protocol handlers - https://github.com/ipfs/ipfs-companion/issues/164#issuecomment-328374052
if (state.catchUnhandledProtocols && mayContainUnhandledIpfsProtocol(request)) {
const fix = normalizedUnhandledIpfsProtocol(request)
if (fix) {
return fix
}
}
// handler for protocol_handlers from manifest.json
if (webPlusProtocolRequest(request)) {
// fix path passed via custom protocol
const fix = normalizedWebPlusRequest(request)
if (fix) {
return fix
}
}
// handle redirects to custom gateway
if (state.redirect) {
// Detect valid /ipfs/ and /ipns/ on any site
if (publicIpfsOrIpnsResource(request.url)) {
return redirectToCustomGateway(request.url)
}
// Look for dnslink in TXT records of visited sites
if (state.dnslink && isDnslookupSafeForURL(request.url)) {
return dnslinkLookupAndOptionalRedirect(request.url)
}
}
}
// PROTOCOL HANDLERS: web+ in Firefox (protocol_handlers from manifest.json)
// ===================================================================
const webPlusProtocolHandler = 'https://ipfs.io/web%2B'
function webPlusProtocolRequest (request) {
return request.url.startsWith(webPlusProtocolHandler)
}
function urlAtPublicGw (path) {
return new URL(`${state.pubGwURLString}${path}`).toString().replace(/([^:]\/)\/+/g, '$1')
}
function normalizedWebPlusRequest (request) {
const oldPath = decodeURIComponent(new URL(request.url).pathname)
let path = oldPath
path = path.replace(/^\/web\+dweb:\//i, '/') // web+dweb:/ipfs/Qm → /ipfs/Qm
path = path.replace(/^\/web\+ipfs:\/\//i, '/ipfs/') // web+ipfs://Qm → /ipfs/Qm
path = path.replace(/^\/web\+ipns:\/\//i, '/ipns/') // web+ipns://Qm → /ipns/Qm
if (oldPath !== path && window.IsIpfs.path(path)) {
return { redirectUrl: urlAtPublicGw(path) }
}
return null
}
// PROTOCOL HANDLERS: UNIVERSAL FALLBACK FOR UNHANDLED PROTOCOLS
// ===================================================================
const unhandledIpfsRE = /=(?:web%2B|)(ipfs(?=%3A%2F%2F)|ipns(?=%3A%2F%2F)|dweb(?=%3A%2Fip[f|n]s))%3A(?:%2F%2F|%2F)([^&]+)/
function mayContainUnhandledIpfsProtocol (request) {
return request.type === 'main_frame' && request.url.includes('%3A%2F')
}
function unhandledIpfsPath (requestUrl) {
const unhandled = requestUrl.match(unhandledIpfsRE)
if (unhandled && unhandled.length > 1) {
const unhandledProtocol = decodeURIComponent(unhandled[1])
const unhandledPath = `/${decodeURIComponent(unhandled[2])}`
return window.IsIpfs.path(unhandledPath) ? unhandledPath : `/${unhandledProtocol}${unhandledPath}`
}
return null
}
function normalizedUnhandledIpfsProtocol (request) {
const path = unhandledIpfsPath(request.url)
if (window.IsIpfs.path(path)) {
// replace search query with fake request to the public gateway
// (will be redirected later, if needed)
return { redirectUrl: urlAtPublicGw(path) }
}
}
// DNSLINK
// ===================================================================
function isDnslookupPossible () {
// DNS lookups require IPFS API to be up
// and have a confirmed connection to the internet
return state.peerCount > 0
}
function isDnslookupSafeForURL (requestUrl) {
// skip URLs that could produce infinite recursion or weird loops
return isDnslookupPossible() &&
requestUrl.startsWith('http') &&
!window.IsIpfs.url(requestUrl) &&
!requestUrl.startsWith(state.apiURLString) &&
!requestUrl.startsWith(state.gwURLString)
}
function dnslinkLookupAndOptionalRedirect (requestUrl) {
const url = new URL(requestUrl)
const fqdn = url.hostname
const dnslink = cachedDnslinkLookup(fqdn)
if (dnslink) {
return redirectToDnslinkPath(url, dnslink)
}
}
function cachedDnslinkLookup (fqdn) {
let dnslink = state.dnslinkCache.get(fqdn)
if (typeof dnslink === 'undefined') {
try {
console.log('dnslink cache miss for: ' + fqdn)
dnslink = readDnslinkFromTxtRecord(fqdn)
if (dnslink) {
state.dnslinkCache.set(fqdn, dnslink)
console.log(`Resolved dnslink: '${fqdn}' -> '${dnslink}'`)
} else {
state.dnslinkCache.set(fqdn, false)
console.log(`Resolved NO dnslink for '${fqdn}'`)
}
} catch (error) {
console.error(`Error in dnslinkLookupAndOptionalRedirect for '${fqdn}'`)
console.error(error)
}
} else {
console.log(`Resolved via cached dnslink: '${fqdn}' -> '${dnslink}'`)
}
return dnslink
}
function redirectToDnslinkPath (url, dnslink) {
url.protocol = state.gwURL.protocol
url.host = state.gwURL.host
url.port = state.gwURL.port
url.pathname = dnslink + url.pathname
return { redirectUrl: url.toString() }
}
function readDnslinkFromTxtRecord (fqdn) {
// js-ipfs-api does not provide method for fetching this
// TODO: revisit after https://github.com/ipfs/js-ipfs-api/issues/501 is addressed
const apiCall = state.apiURLString + '/api/v0/dns/' + fqdn
const xhr = new XMLHttpRequest() // older XHR API us used because window.fetch appends Origin which causes error 403 in go-ipfs
// synchronous mode with small timeout
// (it is okay, because we do it only once, then it is cached and read via cachedDnslinkLookup)
xhr.open('GET', apiCall, false)
xhr.setRequestHeader('Accept', 'application/json')
xhr.send(null)
if (xhr.status === 200) {
const dnslink = JSON.parse(xhr.responseText).Path
// console.log('readDnslinkFromTxtRecord', readDnslinkFromTxtRecord)
if (!window.IsIpfs.path(dnslink)) {
throw new Error(`dnslink for '${fqdn}' is not a valid IPFS path: '${dnslink}'`)
}
return dnslink
} else if (xhr.status === 500) {
// go-ipfs returns 500 if host has no dnslink
// TODO: find/fill an upstream bug to make this more intuitive
return false
} else {
throw new Error(xhr.statusText)
}
}
// RUNTIME MESSAGES (one-off messaging)
// ===================================================================
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/sendMessage
function onRuntimeMessage (request, sender) {
// console.log((sender.tab ? 'Message from a content script:' + sender.tab.url : 'Message from the extension'), request)
if (request.pubGwUrlForIpfsOrIpnsPath) {
const path = request.pubGwUrlForIpfsOrIpnsPath
const result = validIpfsOrIpnsPath(path) ? urlAtPublicGw(path) : null
return Promise.resolve({pubGwUrlForIpfsOrIpnsPath: result})
}
}
// PORTS (connection-based messaging)
// ===================================================================
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/connect
// Make a connection between different contexts inside the add-on,
// e.g. signalling between browser action popup and background page that works
// in everywhere, even in private contexts (https://github.com/ipfs/ipfs-companion/issues/243)
const browserActionPortName = 'browser-action-port'
var browserActionPort
function onRuntimeConnect (port) {
// console.log('onConnect', port)
if (port.name === browserActionPortName) {
browserActionPort = port
browserActionPort.onMessage.addListener(handleMessageFromBrowserAction)
browserActionPort.onDisconnect.addListener(() => { browserActionPort = null })
sendStatusUpdateToBrowserAction()
}
}
function handleMessageFromBrowserAction (message) {
// console.log('In background script, received message from browser action', message)
if (message.event === 'notification') {
notify(message.title, message.message)
}
}
async function sendStatusUpdateToBrowserAction () {
if (browserActionPort) {
const info = {
peerCount: state.peerCount,
gwURLString: state.gwURLString,
pubGwURLString: state.pubGwURLString,
currentTab: await browser.tabs.query({active: true, currentWindow: true}).then(tabs => tabs[0])
}
try {
let v = await ipfs.version()
if (v) {
info.gatewayVersion = v.commit ? v.version + '/' + v.commit : v.version
}
} catch (error) {
info.gatewayVersion = null
}
if (info.currentTab) {
info.ipfsPageActionsContext = isIpfsPageActionsContext(info.currentTab.url)
}
browserActionPort.postMessage({statusUpdate: info})
}
}
// GUI
// ===================================================================
function notify (titleKey, messageKey, messageParam) {
const title = browser.i18n.getMessage(titleKey)
let message
if (messageKey.startsWith('notify_')) {
message = messageParam ? browser.i18n.getMessage(messageKey, messageParam) : browser.i18n.getMessage(messageKey)
} else {
message = messageKey
}
if (state.displayNotifications) {
browser.notifications.create({
'type': 'basic',
'iconUrl': browser.extension.getURL('icons/ipfs-logo-on.svg'),
'title': title,
'message': message
})
}
console.log(`[ipfs-companion] ${title}: ${message}`)
}
// contextMenus
// -------------------------------------------------------------------
const contextMenuUploadToIpfs = 'contextMenu_UploadToIpfs'
browser.contextMenus.create({
id: contextMenuUploadToIpfs,
title: browser.i18n.getMessage(contextMenuUploadToIpfs),
contexts: ['image', 'video', 'audio'],
onclick: addFromURL
})
function inFirefox () {
return !!navigator.userAgent.match('Firefox')
}
async function addFromURL (info) {
try {
if (inFirefox()) {
// workaround due to https://github.com/ipfs/ipfs-companion/issues/227
const fetchOptions = {
cache: 'force-cache',
referrer: info.pageUrl
}
// console.log('addFromURL.info', info)
// console.log('addFromURL.fetchOptions', fetchOptions)
const response = await fetch(info.srcUrl, fetchOptions)
const reader = new FileReader()
reader.onloadend = () => {
const buffer = ipfs.Buffer.from(reader.result)
ipfs.add(buffer, uploadResultHandler)
}
reader.readAsArrayBuffer(await response.blob())
} else {
ipfs.util.addFromURL(info.srcUrl, uploadResultHandler)
}
} catch (error) {
console.error(`Error for ${contextMenuUploadToIpfs}`, error)
if (error.message === 'NetworkError when attempting to fetch resource.') {
notify('notify_uploadErrorTitle', 'notify_uploadTrackingProtectionErrorMsg')
console.warn('IPFS upload often fails because remote file can not be downloaded due to Tracking Protection. See details at: https://github.com/ipfs/ipfs-companion/issues/227')
browser.tabs.create({
'url': 'https://github.com/ipfs/ipfs-companion/issues/227'
})
} else {
notify('notify_uploadErrorTitle', 'notify_inlineErrorMsg', `${error.message}`)
}
}
}
function uploadResultHandler (err, result) {
if (err || !result) {
console.error('ipfs add error', err, result)
notify('notify_uploadErrorTitle', 'notify_inlineErrorMsg', `${err}`)
return
}
result.forEach(function (file) {
if (file && file.hash) {
browser.tabs.create({
'url': new URL(state.gwURLString + '/ipfs/' + file.hash).toString()
})
console.log('successfully stored', file.hash)
}
})
}
function updateContextMenus () {
browser.contextMenus.update(contextMenuUploadToIpfs, {enabled: state.peerCount > 0})
}
// Page Actions
// -------------------------------------------------------------------
// used in browser-action popup
// eslint-disable-next-line no-unused-vars
function isIpfsPageActionsContext (url) {
return window.IsIpfs.url(url) && !url.startsWith(state.apiURLString)
}
async function onUpdatedTab (tabId, changeInfo, tab) {
if (changeInfo.status === 'complete' && tab.url && tab.url.startsWith('http')) {
if (state.linkify) {
console.log(`[ipfs-companion] Running linkfyDOM for ${tab.url}`)
try {
await browser.tabs.executeScript(tabId, {
file: '/src/lib/npm/browser-polyfill.min.js',
matchAboutBlank: false,
allFrames: true,
runAt: 'document_start'
})
await browser.tabs.executeScript(tabId, {
file: '/src/lib/linkifyDOM.js',
matchAboutBlank: false,
allFrames: true,
runAt: 'document_idle'
})
} catch (error) {
console.error(`Unable to linkify DOM at '${tab.url}' due to`, error)
}
}
if (state.catchUnhandledProtocols) {
// console.log(`[ipfs-companion] Normalizing links with unhandled protocols at ${tab.url}`)
// See: https://github.com/ipfs/ipfs-companion/issues/286
try {
// pass the URL of user-preffered public gateway
await browser.tabs.executeScript(tabId, {
code: `window.ipfsCompanionPubGwURL = '${state.pubGwURLString}'`,
matchAboutBlank: false,
allFrames: true,
runAt: 'document_start'
})
// inject script that normalizes `href` and `src` containing unhandled protocols
await browser.tabs.executeScript(tabId, {
file: '/src/lib/normalizeLinksWithUnhandledProtocols.js',
matchAboutBlank: false,
allFrames: true,
runAt: 'document_end'
})
} catch (error) {
console.error(`Unable to normalize links at '${tab.url}' due to`, error)
}
}
}
}
// API STATUS UPDATES
// -------------------------------------------------------------------
// API is polled for peer count every ipfsApiPollMs
const offlinePeerCount = -1
const idleInSecs = 5 * 60
var apiStatusUpdateInterval
function setApiStatusUpdateInterval (ipfsApiPollMs) {
if (apiStatusUpdateInterval) {
clearInterval(apiStatusUpdateInterval)
}
apiStatusUpdateInterval = setInterval(() => runIfNotIdle(apiStatusUpdate), ipfsApiPollMs)
apiStatusUpdate()
}
async function apiStatusUpdate () {
let oldPeerCount = state.peerCount
state.peerCount = await getSwarmPeerCount()
updatePeerCountDependentStates(oldPeerCount, state.peerCount)
sendStatusUpdateToBrowserAction()
}
function updatePeerCountDependentStates (oldPeerCount, newPeerCount) {
updateAutomaticModeRedirectState(oldPeerCount, newPeerCount)
updateBrowserActionBadge()
updateContextMenus()
}
async function getSwarmPeerCount () {
try {
const peerInfos = await ipfs.swarm.peers()
return peerInfos.length
} catch (error) {
// console.error(`Error while ipfs.swarm.peers: ${err}`)
return offlinePeerCount
}
}
async function runIfNotIdle (action) {
try {
const state = await browser.idle.queryState(idleInSecs)
if (state === 'active') {
return action()
}
} catch (error) {
console.error('Unable to read idle state, executing action without idle check', error)
return action()
}
}
// browserAction
// -------------------------------------------------------------------
async function updateBrowserActionBadge () {
let badgeText, badgeColor, badgeIcon
badgeText = state.peerCount.toString()
if (state.peerCount > 0) {
// All is good (online with peers)
badgeColor = '#418B8E'
badgeIcon = '/icons/ipfs-logo-on.svg'
} else if (state.peerCount === 0) {
// API is online but no peers
badgeColor = 'red'
badgeIcon = '/icons/ipfs-logo-on.svg'
} else {
// API is offline
badgeText = ''
badgeColor = '#8C8C8C'
badgeIcon = '/icons/ipfs-logo-off.svg'
}
try {
await browser.browserAction.setBadgeBackgroundColor({color: badgeColor})
await browser.browserAction.setBadgeText({text: badgeText})
await setBrowserActionIcon(badgeIcon)
} catch (error) {
console.error('Unable to update browserAction badge due to error', error)
}
}
async function setBrowserActionIcon (iconPath) {
let iconDefinition = {path: iconPath}
try {
// Try SVG first -- Firefox supports it natively
await browser.browserAction.setIcon(iconDefinition)
} catch (error) {
// Fallback!
// Chromium does not support SVG [ticket below is 8 years old, I can't even..]
// https://bugs.chromium.org/p/chromium/issues/detail?id=29683
// Still, we want icon, so we precompute rasters of popular sizes and use them instead
await browser.browserAction.setIcon(rasterIconDefinition(iconPath))
}
}
function rasterIconDefinition (svgPath) {
// icon sizes to cover ranges from:
// - https://bugs.chromium.org/p/chromium/issues/detail?id=647182
// - https://developer.chrome.com/extensions/manifest/icons
return {
'path': {
'19': rasterIconPath(svgPath, 19),
'38': rasterIconPath(svgPath, 38),
'128': rasterIconPath(svgPath, 128)
}
}
}
function rasterIconPath (iconPath, size) {
// point at precomputed PNG file
let baseName = /\/icons\/(.+)\.svg/.exec(iconPath)[1]
return `/icons/png/${baseName}_${size}.png`
}
/* Easter-Egg: PoC that generates raster on the fly ;-)
function rasterIconData (iconPath, size) {
let icon = new Image()
icon.src = iconPath
let canvas = document.createElement('canvas')
let context = canvas.getContext('2d')
context.clearRect(0, 0, size, size)
context.drawImage(icon, 0, 0, size, size)
return context.getImageData(0, 0, size, size)
}
*/
// OPTIONS
// ===================================================================
function updateAutomaticModeRedirectState (oldPeerCount, newPeerCount) {
// enable/disable gw redirect based on API going online or offline
if (state.automaticMode) {
if (oldPeerCount < 1 && newPeerCount > 0 && !state.redirect) {
browser.storage.local.set({useCustomGateway: true})
.then(() => notify('notify_apiOnlineTitle', 'notify_apiOnlineAutomaticModeMsg'))
} else if (oldPeerCount > 0 && newPeerCount < 1 && state.redirect) {
browser.storage.local.set({useCustomGateway: false})
.then(() => notify('notify_apiOfflineTitle', 'notify_apiOfflineAutomaticModeMsg'))
}
}
}
function storeMissingOptions (read, defaults) {
const requiredKeys = Object.keys(defaults)
const changes = new Set()
requiredKeys.map(key => {
// limit work to defaults and missing values
if (!read.hasOwnProperty(key) || read[key] === defaults[key]) {
changes.add(new Promise((resolve, reject) => {
browser.storage.local.get(key).then(data => {
if (!data[key]) { // detect and fix key without value in storage
let option = {}
option[key] = defaults[key]
browser.storage.local.set(option)
.then(data => { resolve(`updated:${key}`) })
.catch(error => { reject(error) })
} else {
resolve(`nochange:${key}`)
}
})
}))
}
})
return Promise.all(changes)
}
function onStorageChange (changes, area) { // eslint-disable-line no-unused-vars
for (let key in changes) {
let change = changes[key]
if (change.oldValue !== change.newValue) {
// debug info
// console.info(`Storage key "${key}" in namespace "${area}" changed. Old value was "${change.oldValue}", new value is "${change.newValue}".`)
if (key === 'ipfsApiUrl') {
state.apiURL = new URL(change.newValue)
state.apiURLString = state.apiURL.toString()
ipfs = initIpfsApi(state.apiURLString)
apiStatusUpdate()
} else if (key === 'ipfsApiPollMs') {
setApiStatusUpdateInterval(change.newValue)
} else if (key === 'customGatewayUrl') {
state.gwURL = new URL(change.newValue)
state.gwURLString = state.gwURL.toString()
} else if (key === 'publicGatewayUrl') {
state.pubGwURL = new URL(change.newValue)
state.pubGwURLString = state.pubGwURL.toString()
} else if (key === 'useCustomGateway') {
state.redirect = change.newValue
} else if (key === 'linkify') {
state.linkify = change.newValue
} else if (key === 'catchUnhandledProtocols') {
state.catchUnhandledProtocols = change.newValue
} else if (key === 'displayNotifications') {
state.displayNotifications = change.newValue
} else if (key === 'automaticMode') {
state.automaticMode = change.newValue
} else if (key === 'dnslink') {
state.dnslink = change.newValue
}
}
}
}
// OTHER
// ===================================================================
// It's always worse than it seems