Support for even CSS url() rewriting!
This commit is contained in:
parent
c033769279
commit
c75b3dc50c
18
src/index.js
18
src/index.js
|
@ -8,18 +8,22 @@ function materialize_references_to_ipfs_content(node, element) {
|
||||||
// load that content and set it into the attribute as a blob.
|
// load that content and set it into the attribute as a blob.
|
||||||
//
|
//
|
||||||
// Return a promise for doing all of that.
|
// Return a promise for doing all of that.
|
||||||
// TODO: Support <div style="background-image: url(./styles/img/main/alpha-sunrise.png);"> rewriting.
|
for (let attribute_name of ['content', 'href', 'src', 'style']) {
|
||||||
for (let attribute_name of ['content', 'href', 'src']) {
|
|
||||||
let attribute_value = element.getAttribute(attribute_name)
|
let attribute_value = element.getAttribute(attribute_name)
|
||||||
if (attribute_value && attribute_value.startsWith('./')) {
|
|
||||||
|
if (attribute_value && attribute_value.includes('./')) {
|
||||||
let base_hash = window.location.pathname.match(/(^\/ipfs\/\w+)/)[1]
|
let base_hash = window.location.pathname.match(/(^\/ipfs\/\w+)/)[1]
|
||||||
let uri = base_hash + attribute_value.slice(1)
|
|
||||||
let anchor = document.createElement('a')
|
let anchor = document.createElement('a')
|
||||||
anchor.href = uri
|
let relative_url = attribute_value.match(/(.\/[^)]+)/)[1]
|
||||||
|
anchor.href = base_hash + relative_url.slice(1)
|
||||||
console.log('loading IPFS sub-content for ' + anchor.pathname)
|
console.log('loading IPFS sub-content for ' + anchor.pathname)
|
||||||
|
|
||||||
return node.files.cat(anchor.pathname).then(function (contents_buffer) {
|
return node.files.cat(anchor.pathname).then(function (contents_buffer) {
|
||||||
element.setAttribute(attribute_name, URL.createObjectURL(new Blob([contents_buffer])))
|
let materialized_value = attribute_value.replace(
|
||||||
|
relative_url,
|
||||||
|
URL.createObjectURL(new Blob([contents_buffer]))
|
||||||
|
)
|
||||||
|
element.setAttribute(attribute_name, materialized_value)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,7 +58,7 @@ function App() {
|
||||||
|
|
||||||
// TODO: Should only do this if it's actually an HTML document. Need to gracefully skip this for other content types.
|
// TODO: Should only do this if it's actually an HTML document. Need to gracefully skip this for other content types.
|
||||||
let contents_document = new DOMParser().parseFromString(contents_buffer.toString(), 'text/html')
|
let contents_document = new DOMParser().parseFromString(contents_buffer.toString(), 'text/html')
|
||||||
let elements = contents_document.querySelectorAll('link,meta,script')
|
let elements = contents_document.querySelectorAll('link,meta,script,div')
|
||||||
let promises = []
|
let promises = []
|
||||||
|
|
||||||
for (let element of elements) {
|
for (let element of elements) {
|
||||||
|
|
Reference in New Issue
Block a user