A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/app/scripts/lib/ipfsContent.js

41 lines
1.8 KiB

7 years ago
const extension = require('extensionizer')
const resolver = require('./resolver.js')
module.exports = function (provider) {
extension.webRequest.onBeforeRequest.addListener(details => {
7 years ago
const urlhttpreplace = details.url.replace(/\w+?:\/\//, '')
const url = urlhttpreplace.replace(/[\\/].*/g, '') // eslint-disable-line no-useless-escape
let domainhtml = urlhttpreplace.match(/[\\/].*/g) // eslint-disable-line no-useless-escape
7 years ago
let clearTime = null
7 years ago
const name = url.replace(/\/$/g, '')
if (domainhtml === null) domainhtml = ['']
7 years ago
extension.tabs.getSelected(null, tab => {
extension.tabs.update(tab.id, { url: 'loading.html' })
clearTime = setTimeout(() => {
return extension.tabs.update(tab.id, { url: '404.html' })
}, 60000)
resolver.resolve(name, provider).then(ipfsHash => {
clearTimeout(clearTime)
let url = 'https://ipfs.infura.io/ipfs/' + ipfsHash + domainhtml[0]
7 years ago
return fetch(url, { method: 'HEAD' }).then(response => response.status).then(statusCode => {
7 years ago
if (statusCode !== 200) return extension.tabs.update(tab.id, { url: '404.html' })
7 years ago
extension.tabs.update(tab.id, { url: url })
})
.catch(err => {
url = 'https://ipfs.infura.io/ipfs/' + ipfsHash + domainhtml[0]
7 years ago
extension.tabs.update(tab.id, {url: url})
return err
})
})
.catch(err => {
clearTimeout(clearTime)
const url = err === 'unsupport' ? 'unsupport' : 'error'
extension.tabs.update(tab.id, {url: `${url}.html?name=${name}`})
})
})
return { cancel: true }
}, {urls: ['*://*.eth/', '*://*.eth/*']})
7 years ago
}