Skip to content

Commit

Permalink
More prototyping
Browse files Browse the repository at this point in the history
  • Loading branch information
victorb committed Feb 21, 2017
1 parent 0102064 commit c0d138f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 50 deletions.
20 changes: 20 additions & 0 deletions icons/ipfs-offline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"background": {
"scripts": ["dist/start-daemon.js"]
},
"browser_action": {
"default_icon": "icons/ipfs-offline.svg"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"devDependencies": {},
"scripts": {
"test": "mocha"
"test": "mocha",
"build": "browserify start-daemon.js -o dist/start-daemon.js"
},
"keywords": [],
"author": "",
Expand Down
25 changes: 14 additions & 11 deletions page-script.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
/* global browser */
/* global browser, cloneInto */
// window.wrappedJSObject.ipfs = false
console.log('declared a public ipfs object!')

const myPort = browser.runtime.connect({name: 'port-from-cs'})

const makeCall = (method, cb) => {
myPort.onMessage.addListener(function (m) {
myPort.onMessage.removeListener(this)
cb(m.response)
// myPort.onMessage.removeListener(this)
cb(cloneInto(m, window, {cloneFunctions: true}))
})
myPort.postMessage({method})
}

const createFunc = (method, callback) => {
return () => {
makeCall(method, callback)
}
}

const ipfs = {
id: (callback) => createFunc('id', callback)
id: (callback) => { makeCall('id', callback) }
}

window.ipfs = ipfs
// ipfs.id((id) => {
// console.log('lol', id)
// })

window.wrappedJSObject.ipfs = cloneInto(
ipfs,
window,
{cloneFunctions: true})

// window.ipfs = ipfs
// unsafeWindow.clonedContentScriptObject = cloneInto(ipfs, unsafeWindow)
// unsafeWindow.assignedContentScriptObject = ipfs;
56 changes: 22 additions & 34 deletions start-daemon.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
/* global browser */
let node = null
require('setimmediate') // TODO js-ipfs fails without this in the ID call
const spawn = require('./spawn-node.js')
browser.browserAction.setIcon({path: '/icons/ipfs-offline.svg'})

spawn({}, (err, ipfsNode) => {
if (err) throw err
node = ipfsNode
})

const methods = {
'id': (node, send) => {
node.id((err, id) => {
if (err) throw err
send(id)
})
}
}
browser.browserAction.setIcon({path: '/icons/ipfs.svg'})

browser.runtime.onConnect.addListener((port) => {
port.onMessage.addListener((m) => {
if (methods[m.method] !== undefined) {
methods[m.method](node, (res) => {
port.postMessage({response: res})
const methods = {
'id': (send) => {
console.log('method#id')
ipfsNode.id((err, id) => {
if (err) throw err
console.log('responding')
send(id)
})
}
}

browser.runtime.onConnect.addListener((port) => {
port.onMessage.addListener((m) => {
if (methods[m.method] !== undefined) {
console.log('can make this call')
methods[m.method]((res) => {
console.log('made it and responding!')
port.postMessage(res)
})
}
})
})
})

const handleErr = (err) => {
console.log('Something threw an error')
if (err) throw err
}

browser.tabs.query({}).then((tabs) => {
console.log(tabs)
}, handleErr).catch(handleErr)

// var addonScriptObject = {"greeting" : "hello from add-on"};
// contentWindow.addonScriptObject = cloneInto(addonScriptObject, contentWindow);

browser.tabs.executeScript({
file: '/page-script.js'
}).then(() => {
console.log('injected')
}, handleErr).catch(handleErr)
11 changes: 7 additions & 4 deletions testpage.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
<body>
<h1>Is IPFS in the browser yet?</h1>
<div id="status">Loading</div>
<div>Your ID: <span id='peerid'>N/A</span></div>
<script>
document.addEventListener('DOMContentLoaded', () => {
$status = document.querySelector('#status')
$peerID = document.querySelector('#peerid')
console.log(window.ipfs)
if (typeof window.ipfs === 'undefined') {
$status.innerText = 'nope'
} else {
$status.innerText = 'YEAH!'
window.ipfs.id((res) => {
window.res = res
console.log(res)
$peerID.innerText = res.id
})
}
// window.ipfs.id((res) => {
// console.log('from inside page')
// console.log(res)
// })
}, false)
</script>
</body>
Expand Down

0 comments on commit c0d138f

Please sign in to comment.