render ipfs data urls

This commit is contained in:
Nicholas Jitkoff 2022-06-29 08:34:07 -07:00
parent 63932a356d
commit 5045f4be23

View file

@ -2,47 +2,52 @@ loadSyle(document.currentScript.src.replace("js", "css"))
loadScript("https://unpkg.com/ipfs@0.63.2/dist/index.min.js", (scr) => {
let cid = params.url.replace("ipfs:",'');
console.log("script", scr, Ipfs, params)
getData(cid);
})
async function test(cid) {
let file = {
path: "hello.txt",
content: "Hello World 101",
}
console.log("adding", file)
// async function test(cid) {
// let file = {
// path: "hello.txt",
// content: "Hello World 101",
// }
// console.log("adding", file)
const node = await Ipfs.create()
console.log("adding", file)
// const node = await Ipfs.create()
// console.log("adding", file)
const fileAdded = await node.add(file);
console.log("added", fileAdded)
}
async function addData(data) {
const node = await Ipfs.create()
const results = node.add(data)
for await (const { cid } of results) {
console.log(cid.toString())
}
}
// const fileAdded = await node.add(file);
// console.log("added", fileAdded)
// }
// async function addData(data) {
// const node = await Ipfs.create()
// const results = node.add(data)
// for await (const { cid } of results) {
// console.log(cid.toString())
// }
// }
async function getData(cid) {
const node = await Ipfs.create()
const stream = node.cat(cid)
const decoder = new TextDecoder()
let data = ''
console.log("Got IPFS Data", data)
let chunks = []
for await (const chunk of stream) { chunks.push(chunk) }
var a = new FileReader();
a.onload = function(e) {
console.log("Got IPFS Data", e.target.result)
location.href=e.target.result
for await (const chunk of stream) {
data += decoder.decode(chunk, { stream: true })
}
a.readAsDataURL(new Blob(chunks, {encoding:"UTF-8",type:"text/plain"}));
console.log("Got IPFS Data", data)
return data;
}
let url = params.url.replace("ipfs:", "https://ipfs.io/ipfs/");
location.href = url;
// location.href = url;