-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds IPFS to page only when option is set
- Loading branch information
Showing
7 changed files
with
126 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,26 @@ | ||
'use strict' | ||
/* eslint-env browser, webextensions */ | ||
|
||
const browser = require('webextension-polyfill') | ||
const once = require('./once') | ||
const injectScript = require('./inject-script') | ||
|
||
const port = browser.runtime.connect({ name: 'ipfs-proxy' }) | ||
window.ipfsCompanion = window.ipfsCompanion || {} | ||
window.ipfsCompanion.once = window.ipfsCompanion.once || {} | ||
|
||
// Forward on messages from background to the page and vice versa | ||
port.onMessage.addListener((data) => window.postMessage(data, '*')) | ||
// Only run this once for this window! | ||
// URL can change (history API) which causes this script to be executed again, | ||
// but it only needs to be setup once per window... | ||
once('ipfsProxyContentScriptExecuted', () => { | ||
const port = browser.runtime.connect({ name: 'ipfs-proxy' }) | ||
|
||
window.addEventListener('message', (msg) => { | ||
if (msg.data && msg.data.sender === 'postmsg-rpc/client') { | ||
port.postMessage(msg.data) | ||
} | ||
}) | ||
// Forward on messages from background to the page and vice versa | ||
port.onMessage.addListener((data) => window.postMessage(data, '*')) | ||
|
||
function injectPageScript () { | ||
try { | ||
const scriptTag = document.createElement('script') | ||
scriptTag.src = browser.extension.getURL('dist/contentScripts/ipfs-proxy/page.js') | ||
scriptTag.onload = function () { | ||
this.parentNode.removeChild(this) | ||
window.addEventListener('message', (msg) => { | ||
if (msg.data && msg.data.sender === 'postmsg-rpc/client') { | ||
port.postMessage(msg.data) | ||
} | ||
const container = document.head || document.documentElement | ||
container.insertBefore(scriptTag, container.children[0]) | ||
} catch (err) { | ||
console.error('Failed to inject ipfs-proxy/page.js', err) | ||
} | ||
} | ||
}) | ||
|
||
injectPageScript() | ||
injectScript(browser.extension.getURL('dist/contentScripts/ipfs-proxy/page.js')) | ||
}, { store: window.ipfsCompanion.once })() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function injectScript (src, target, opts) { | ||
opts = opts || {} | ||
const doc = opts.document || document | ||
|
||
const scriptTag = doc.createElement('script') | ||
scriptTag.src = src | ||
scriptTag.onload = function () { | ||
this.parentNode.removeChild(this) | ||
} | ||
|
||
target = doc.head || doc.documentElement | ||
target.appendChild(scriptTag) | ||
} | ||
|
||
module.exports = injectScript |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const store = {} | ||
|
||
// call a function only once and store it's return value | ||
function once (id, func, opts) { | ||
opts = opts || {} | ||
opts.store = opts.store || store | ||
|
||
if (Object.keys(opts.store).includes(id)) { | ||
return () => opts.store[id] | ||
} | ||
|
||
return function () { | ||
opts.store[id] = func.apply(null, arguments) | ||
return opts.store[id] | ||
} | ||
} | ||
|
||
module.exports = once |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters