Skip to content

Instantly share code, notes, and snippets.

@jamesray1
Last active December 30, 2017 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesray1/3d74e26fe7bf6fdad42e54e9b53d3629 to your computer and use it in GitHub Desktop.
Save jamesray1/3d74e26fe7bf6fdad42e54e9b53d3629 to your computer and use it in GitHub Desktop.
{
"address": "1kdy2Ds8dbd6rg3oHy2TouwfYFjtJ3Pag",
"address_index": 12005246,
"background-color": "#FFF",
"clone_root": "template-new",
"cloned_from": "1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D",
"description": "",
"files": {
"index.html": {
"sha512": "d09673b02682be2bb8eccb39944234c8bcfeb497813790ec1d7de2103c706a00",
"size": 1161
},
"js/ZeroFrame.js": {
"sha512": "42125c7aa72496455e044e3fd940e0f05db86824c781381edb7a70f71a5f0882",
"size": 3370
}
},
"ignore": "data/.*",
"includes": {
"data/users/content.json": {
"signers": [],
"signers_required": 1
}
},
"inner_path": "content.json",
"modified": 1514642602,
"postmessage_nonce_security": true,
"signers_sign": "HAesGM/2vj9FboTQi2oisaT6cGlzU9Ov40Nc20D/Et2dLJwNXv0i/SpmONNSodjNFRxp0jQ5vFp8fEKk9nZVD8c=",
"signs": {
"1kdy2Ds8dbd6rg3oHy2TouwfYFjtJ3Pag": "HGQvnEMQauxriSyNi/2gWAS3OQ81bnVoPry2zRqwzNu9HWfZOd0NpOwUK0Ww0szLhXc+uJ4CamH/0n9JZlPKCHs="
},
"signs_required": 1,
"title": "my new site",
"translate": ["js/all.js"],
"zeronet_version": "0.6.0"
}
{
"db_name": "ZeroChat",
"db_file": "data/zerochat.db",
"version": 2,
"maps": {
"users/.+/data.json": {
"to_table": [ "message" ]
},
"users/.+/content.json": {
"to_json_table": [ "cert_user_id" ],
"file_name": "data.json"
}
},
"tables": {
"json": {
"cols": [
["json_id", "INTEGER PRIMARY KEY AUTOINCREMENT"],
["directory", "TEXT"],
["file_name", "TEXT"],
["cert_user_id", "TEXT"]
],
"indexes": ["CREATE UNIQUE INDEX path ON json(directory, file_name)"],
"schema_changed": 10
},
"message": {
"cols": [
["body", "TEXT"],
["date_added", "INTEGER"],
["json_id", "INTEGER REFERENCES json (json_id)"]
],
"indexes": ["CREATE UNIQUE INDEX message_key ON message(json_id, date_added)"],
"schema_changed": 10
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>New ZeroNet site!</title>
<meta charset="utf-8">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<base href="" target="_top" id="base">
<script>base.href = document.location.href.replace("/media", "").replace("index.html", "").replace(/[&?]wrapper=False/, "").replace(/[&?]wrapper_nonce=[A-Za-z0-9]+/, "")</script>
<style>
* { font-family: monospace; line-height: 1.5em; font-size: 13px; vertical-align: middle; }
body { background-color: white; }
input#message { padding: 5px; width: 50%; }
input#send { height: 34px; margin-left: -2px; }
ul { padding: 0px; }
li { list-style-type: none; border-top: 1px solid #eee; padding: 5px 0px; }
li:nth-child(odd) { background-color: #F9FAFD; }
li b { color: #3F51B5; }
</style>
</head>
<body>
<a href="#Select+user" id="select_user" onclick='return page.selectUser()'>Select user</a>:
<input type="text" id="message" onkeypress="if (event.keyCode == 13) page.sendMessage()">
<input type="button" id="send" value="Send!" onclick="return page.sendMessage()"/>
<ul id="messages">
<li>Welcome to ZeroChat!</li>
</ul>
<script type="text/javascript" src="js/ZeroFrame.js"></script>
<script>
class ZeroChat extends ZeroFrame {
addMessage (username, message) {
var message_escaped = message.replace(/</g, "&lt;").replace(/>/g, "&gt;") // Escape html tags in the message
this.messages.innerHTML += "<li><b>" + username + "</b>: " + message_escaped + "</li>"
}
onOpenWebsocket () {
this.messages = document.getElementById("messages")
this.addMessage("System", "Ready to call ZeroFrame API!")
this.cmd("siteInfo", {}, (site_info) => {
if (site_info.cert_user_id)
document.getElementById("select_user").innerText = site_info.cert_user_id
this.site_info = site_info
})
this.loadMessages() //Contrary to as claimed, after adding this and reloading the page, I did not see the messages that I typed in, although they were in the data.json file, and data/zerochat.db was added.
}
selectUser () {
this.cmd("certSelect", {accepted_domains: ["zeroid.bit"]})
return false
}
onRequest (cmd, message) {
if (cmd == "setSiteInfo") {
if (message.params.cert_user_id)
document.getElementById("select_user").innerHTML = message.params.cert_user_id
else
document.getElementById("select_user").innerHTML = "Select user"
this.site_info = message.params // Save site info data to allow access it later
}
// Reload messages if new file arrives. Adding this causes the selected user, e.g. jamesray@zeroid.bit, to change to "Select user", and sent messages are then not recorded in the log data.json file in /users/address
if (message.params.event[0] == "file_done")
this.loadMessages()
}
}
sendMessage () {
if (!this.site_info.cert_user_id) { // No account selected, display error
this.cmd("wrapperNotification", ["info", "Please, select your account."])
return false
}
// This is our data file path
var data_inner_path = "data/users/" + this.site_info.auth_address + "/data.json"
var content_inner_path = "data/users/" + this.site_info.auth_address + "/content.json"
// Load our current messages
this.cmd("fileGet", {"inner_path": data_inner_path, "required": false}, (data) => {
if (data) // Parse current data file
data = JSON.parse(data)
else // Not exists yet, use default data
data = { "message": [] }
// Add the new message to data
data.message.push({
"body": document.getElementById("message").value,
"date_added": Date.now()
})
// Encode data array to utf8 json text
var json_raw = unescape(encodeURIComponent(JSON.stringify(data, undefined, '\t')))
// Write file to disk
this.cmd("fileWrite", [data_inner_path, btoa(json_raw)], (res) => {
if (res == "ok") {
// Reset the message input
document.getElementById("message").value = ""
// Sign the changed file in our user's directory
this.cmd("siteSign", {"inner_path": content_inner_path}, (res) => {
this.loadMessages() // Reload messages
// Publish to other users
this.cmd("sitePublish", {"inner_path": content_inner_path, "sign": false})
})
} else {
this.cmd("wrapperNotification", ["error", "File write error: #{res}"])
}
// Sign the changed file in our user's directory
this.cmd("siteSign", {"inner_path": content_inner_path}, (res) => {
this.loadMessages() // Reload messages
})
this.cmd("siteSign", {"inner_path": content_inner_path}, (res) => {
this.loadMessages() // Reload messages
})
return false
}
loadMessages () {
this.cmd("dbQuery", ["SELECT * FROM message LEFT JOIN json USING (json_id) ORDER BY date_added DESC"], (messages) => {
document.getElementById("messages").innerHTML = "" // Always start with empty messages
for (var i=0; i < messages.length; i++) {
this.addMessage(messages[i].cert_user_id, messages[i].body)
}
})
}
}
page = new ZeroChat()
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment