Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
victorb committed Dec 11, 2017
1 parent 8526a1d commit 6b0ac8b
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
15 changes: 15 additions & 0 deletions add-on/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@
"page": "dist/background/background.html"
},

"content_scripts": [
{
"matches": [
"file://*/*",
"http://*/*",
"https://*/*"
],
"js": [
"src/contentScripts/injectGlobal.js"
],
"run_at": "document_start",
"all_frames": true
}
],

"browser_action": {
"browser_style": false,
"default_icon": {
Expand Down
24 changes: 24 additions & 0 deletions add-on/src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,27 @@ const createIpfsCompanion = require('../lib/ipfs-companion')
document.addEventListener('DOMContentLoaded', async () => {
window.ipfsCompanion = await createIpfsCompanion()
})

var portFromCS;

function connected(p) {
portFromCS = p;
portFromCS.onMessage.addListener(function(m) {
if (m.from === 'content-script') {
console.log('received background-page <- content-script', m)
if (m.func === 'id') {
window.ipfsCompanion.ipfs.id((err, res) => {
console.log('sending background-page -> content-script')
portFromCS.postMessage({func: 'id', err, res, from: 'background-page'})
})
}
}
});
}

browser.runtime.onConnect.addListener(connected);
//
// browser.browserAction.onClicked.addListener(function() {
// portFromCS.postMessage({greeting: "they clicked the button!"});
// });

85 changes: 85 additions & 0 deletions add-on/src/contentScripts/injectGlobal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict'
/* eslint-env browser, webextensions */

/*
* Content-script for injection a window.ipfs global into every
* page. Getting content and ID works like normal but adding
* or pinning content requires a permission dialog to have been
* accepted.
*/

// const ipfsApi = require('ipfs-api')
// const ipfs = new ipfsApi()

// a call looks like {func: 'add', args: [buffer]}
//
var myPort = browser.runtime.connect({name: 'post-from-cs'})

myPort.onMessage.addListener(function(m) {
console.log('received content-script <- background-page', m)
console.log('sending content-script -> inpage')
// repeating here because addEventListener catches this again?
window.postMessage({...m, from: 'content-script'}, '*')
// this needs to be sent to
})

window.addEventListener('message', (msg) => {
if (msg.data.from === 'inpage') {
console.log('received content-script <- inpage', msg)
console.log('sending content-script -> background-page')
myPort.postMessage({...msg.data, from: 'content-script'})
}
})

const inpageBundle = `
window.ipfs = {
id: (callback) => {
console.log('=== FIRST ===')
window.addEventListener('message', function(msg) {
if (msg.data.from === 'content-script') {
console.log('received inpage <- content-script', msg)
callback(msg.data.err, msg.data.res)
window.removeEventListener('message', arguments.callee, false)
}
}, false)
window.postMessage({func: 'id', args: [], from: 'inpage'}, '*')
},
add: (content, callback) => {
console.log('=== FIRST ===')
window.addEventListener('message', function(msg) {
if (msg.data.from === 'content-script') {
console.log('received inpage <- content-script', msg)
callback(msg.data.err, msg.data.res)
window.removeEventListener('message', arguments.callee, false)
}
}, false)
window.postMessage({func: 'id', args: [], from: 'inpage'}, '*')
},
// add: (content, callback) => {
// console.log('adding content')
// // postMsg()
// // ipfs.add(content, callback)
// callback(null, [{hash: 'lol'}])
// },
// cat: (hash, callback) => {
// callback(null, 'This is the content you had since before')
// }
}
`

function setupInjection () {
try {
// inject in-page script
var scriptTag = document.createElement('script')
scriptTag.textContent = inpageBundle
scriptTag.onload = function () {
this.parentNode.removeChild(this)
}
var container = document.head || document.documentElement
container.insertBefore(scriptTag, container.children[0])
} catch (e) {
console.error('Inpage IPFS loading failed', e)
}
}

setupInjection()
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"postmerge": "run-s postcheckout",
"precommit": "run-s -s clean build lint",
"prepush": "run-s -s precommit test build",
"firefox": "web-ext run -s add-on/ --browser-console",
"firefox": "web-ext run -s add-on/ --browser-console -u http://localhost:8080/ipfs/Qmd3rcQvdGLaUCbsW3REx54ACWSX3UPipFwB6K8suW9da9",
"yarn-build": "npx yarn@1.3.2 && npx yarn@1.3.2 build",
"docker-build": "docker run -it --rm --name ipfs-companion-build -v \"$PWD\":/src -w /src node:8.9.1 /bin/bash -c \"npm run yarn-build\""
},
Expand Down

0 comments on commit 6b0ac8b

Please sign in to comment.