-
Notifications
You must be signed in to change notification settings - Fork 325
/
Copy pathcontent.js
51 lines (45 loc) · 1.29 KB
/
content.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
'use strict'
import rawCode from './../../../dist/bundles/ipfsProxyContentScriptPayload.bundle.js'
const browser = require('webextension-polyfill')
const injectScript = require('./inject-script')
function init () {
const port = browser.runtime.connect({ name: 'ipfs-proxy' })
// Forward on messages from background to the page and vice versa
port.onMessage.addListener((data) => {
if (data && data.sender && data.sender.startsWith('postmsg-rpc/')) {
window.postMessage(data, '*')
}
})
window.addEventListener('message', (msg) => {
if (msg.data && msg.data.sender && msg.data.sender.startsWith('postmsg-rpc/')) {
port.postMessage(msg.data)
}
})
injectScript(rawCode)
}
function injectIpfsProxy () {
// Skip if proxy is already present
if (window.ipfs) {
return false
}
// Restricting window.ipfs to Secure Context
// See: https://github.com/ipfs-shipyard/ipfs-companion/issues/476
if (!window.isSecureContext) {
return false
}
// Skip if not in HTML context
// Check 1/2
const doctype = window.document.doctype
if (doctype && doctype.name !== 'html') {
return false
}
// Check 2/2
if (document.documentElement.nodeName !== 'HTML') {
return false
}
// Should be ok by now
return true
}
if (injectIpfsProxy()) {
init()
}