-
Notifications
You must be signed in to change notification settings - Fork 4
/
background-script.js
132 lines (122 loc) · 3.89 KB
/
background-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
require('setimmediate') // TODO js-ipfs fails without this in the ID call
const spawn = require('./spawn-node.js')
const extension = require('extensionizer')
extension.browserAction.setIcon({path: '/icons/ipfs-offline.svg'})
let ipfs
extension.browserAction.setBadgeText({text: '...'})
spawn({}, (err, ipfsNode) => {
if (err) throw err
console.log('got node')
ipfs = ipfsNode
window.ipfsNode = ipfsNode
const ID = ipfsNode._peerInfo.id._idB58String
extension.browserAction.setIcon({path: '/icons/ipfs.svg'})
extension.browserAction.setBadgeText({text: "0"})
setInterval(() => {
console.log('checking for peers')
ipfs.swarm.peers((err, peers) => {
if (err) throw err
console.log('peers', peers)
const text = peers.length.toString()
extension.browserAction.setBadgeText({text})
})
}, 1000)
// Toggle status of node
// browser.browserAction.onClicked.addListener(() => {
// if (ipfsNode.isOnline().isOnline) {
// ipfsNode.goOffline(() => {
// browser.browserAction.setIcon({path: '/icons/ipfs-offline.svg'})
// browser.browserAction.setBadgeText({text: 'Offline'})
// })
// } else {
// ipfsNode.goOnline(() => {
// browser.browserAction.setIcon({path: '/icons/ipfs.svg'})
// })
// }
// })
const seenWelcomeScreen = window.localStorage.getItem('seen-welcome-screen') || false
console.log('seen welcome screen already?', seenWelcomeScreen)
if (!seenWelcomeScreen) {
extension.tabs.create({url: 'welcome-screen.html'})
window.localStorage.setItem('seen-welcome-screen', true)
}
console.log('settings methods for communcation')
const methods = {
id: (args, send) => {
ipfsNode.id((err, res) => {
send({err, res})
})
},
peers: (args, send) => {
ipfsNode.swarm.peers((err, res) => {
send({err, res})
})
},
isOnline: (args, send) => {
console.log('background checking if online')
setImmediate(() => {
send({err: null, res: ipfsNode._libp2pNode.isOnline})
})
},
goOnline: (args, send) => {
ipfsNode.start(() => {
extension.browserAction.setIcon({path: '/icons/ipfs.svg'})
send({err: null, res: true})
})
},
goOffline: (args, send) => {
ipfsNode.stop(() => {
extension.browserAction.setIcon({path: '/icons/ipfs-offline.svg'})
extension.browserAction.setBadgeText({text: 'Offline'})
send({err: null, res: true})
})
},
swarmAddresses: () => {
ipfsNode.config.get((err, config) => {
let adddresses = []
config.Addresses.Swarm.forEach((address) => {
adddresses.push(address + '/ipfs/' + ID)
})
send({err, res: addresses})
})
},
add: (args, send) => {
ipfsNode.files.add(new Buffer(args), (err, res) => {
send({err, res})
})
},
cat: (args, send) => {
// TODO right now we join all the data and send it
// should be sending a stream instead
ipfsNode.files.cat(args, (err, res) => {
if (err) send({err})
console.log('getting ', res)
let data = ''
res.on('data', (d) => {
console.log('data ', d)
data = data + d.toString()
})
res.on('end', () => {
console.log('sending', data)
send({err: null, res: data})
})
})
}
}
console.log('setting up browser communication')
extension.runtime.onConnect.addListener((port) => {
console.log('got port, listening')
port.onMessage.addListener((m) => {
if (methods[m.method] !== undefined) {
console.log('can make this call')
methods[m.method](m.args, (res) => {
console.log('made it and responding!')
console.log(res)
port.postMessage(res)
})
} else {
throw new Error('Method ' + m.method + ' is currently not exposed')
}
})
})
})