This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revise README changes and add example web page
- Loading branch information
Showing
2 changed files
with
129 additions
and
15 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>IPFS in the Browser</title> | ||
<script src="https://unpkg.com/ipfs/dist/index.min.js"></script> | ||
<script type="text/javascript"> | ||
// Set this if you have a libp2p-webrtc-star server | ||
// Something like | ||
// const SIGNALING_SERVER = '/libp2p-webrtc-star/ip4/127.0.0.1/tcp/9090/ws/ipfs/' | ||
const SIGNALING_SERVER = null | ||
|
||
// Make an IPFS node | ||
var ipfs = new Ipfs() | ||
|
||
// Init a repo in the given IPFS node it if hasn't got one already. Calls the | ||
// setup callback, passing the normal callback, after first initialization. | ||
// Calls the normal callback directly after subsequent initializations. Calls | ||
// the normal callback with an error parameter if there is an error. | ||
function initIfNeeded (ipfs, setup, callback) { | ||
ipfs.init((err) => { | ||
if (!err) { | ||
// This is the first time we have started a node | ||
setup(callback) | ||
} else if (err.message == 'repo already exists') { | ||
// The node already exists | ||
callback() | ||
} else { | ||
callback(err) | ||
} | ||
}) | ||
} | ||
|
||
// Init the node | ||
initIfNeeded(ipfs, (callback) => { | ||
// On first initialization, do some setup | ||
// Get the node config we just init-ed | ||
ipfs.config.get((err, config) => { | ||
if (err) { | ||
throw err | ||
} | ||
|
||
if (SIGNALING_SERVER) { | ||
|
||
// Add at least one libp2p-webrtc-star address. Without an address like this | ||
// the libp2p-webrtc-star transport won't be installed, and the resulting | ||
// node won't be able to dial out to libp2p-webrtc-star addresses. | ||
var star_addr = (SIGNALING_SERVER + config.Identity.PeerID) | ||
ipfs.config.set('Addresses.Swarm[1]', star_addr, (err) => { | ||
if (err) { | ||
throw err | ||
} | ||
// Continue down the already-initialized code path | ||
callback() | ||
}) | ||
} else { | ||
// No signaling server is known. Just cointinue without it. | ||
// We don't want to spam the console in our demo | ||
callback() | ||
} | ||
}) | ||
}, (err) => { | ||
// If the repo was already initialized, or after the first-time initialization | ||
// code is run, we'll do this. | ||
if (err) { | ||
throw err | ||
} | ||
// Have the node set itself up | ||
ipfs.load(() => { | ||
// Go online and connect to things | ||
ipfs.goOnline(() => { | ||
console.log('Online status: ', ipfs.isOnline() ? 'online' : 'offline') | ||
document.getElementById("status").innerHTML= 'Node status: ' + (ipfs.isOnline() ? 'online' : 'offline') | ||
// TODO: Write your code here! | ||
// Use methods like ipfs.files.add, ipfs.files.get, and so on in here | ||
// Methods requiring buffers can use ipfs.Buffer | ||
}) | ||
}) | ||
}) | ||
</script> | ||
</head> | ||
<body> | ||
<h1>IPFS in the Browser</h1> | ||
<p>This page creates an IPFS node in your browser and drops it into the global Javascript namespace as <em>ipfs</em>. Open the console to play around with it.</p> | ||
<p>Note that opening two tabs of this page in the same browser won't work well, because they will share node configuration. You'll end up trying to run two instances of the same node, with the same private key and identity, which is a Bad Idea.</p> | ||
<div id="status">Node status: offline</div> | ||
</body> | ||
</html> |