index.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>A simple chat</title>
  6. <base href="" target="_top" id="base">
  7. <script>base.href = document.location.href.replace("/media", "").replace("index.html", "").replace(/[&?]wrapper=False/, "").replace(/[&?]wrapper_nonce=[A-Za-z0-9]+/, "")</script>
  8. <style>
  9. * {
  10. color: white;
  11. }
  12. .nick {
  13. font-weight: bold;
  14. }
  15. #history {
  16. list-style-type: none;
  17. margin: 0;
  18. padding: 0;
  19. color: #ccffcc;
  20. }
  21. #chat {
  22. overflow: scroll;
  23. overflow-x: hidden;
  24. overflow-y: scroll;
  25. height: 400px;
  26. }
  27. input {
  28. background-color: #4d0026;
  29. border: 2px solid black;
  30. }
  31. input[input="text"] {
  32. outline: 2px solid white;
  33. font-size: 1.1em;
  34. }
  35. button {
  36. background-color: #330011;
  37. border: 1px solid white;
  38. outline: 2px solid black;
  39. padding: 5px;
  40. }
  41. .note {
  42. max-width: 500px;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div id="chat">
  48. <ul id="history">
  49. </ul>
  50. </div>
  51. <button onclick="login()">Login</button>
  52. <input type="text" id="msgInput" value="" autocomplete="off">
  53. <button onclick="requireLogin(sendMessage)" id="sendButton">Send</button>
  54. <button onclick="requireLogin(sendMsgPong)" id="sendPong">Send pong</button>
  55. <button onclick="clearMessages()">Clear</button>
  56. <br><br>
  57. <input type="checkbox" id="notifyWhenSend" checked>
  58. <label for="notifyWhenSend">Notification when message is sent</label>
  59. <br>
  60. <input type="checkbox" id="notifyWhenReceive">
  61. <label for="notifyWhenReceive">Notification when message is received</label>
  62. <br>
  63. <input type="checkbox" id="autoping" checked>
  64. <label for="autoping">Automatically reply to a ping message with a pong</label>
  65. <br>
  66. <input type="checkbox" id="showping" checked>
  67. <label for="showping">Show ping/pong messages (also own)</label>
  68. <br><br>
  69. <p><button onclick="updateNumPeers()">Update</button> Peers: <span id="numPeers"></span></p>
  70. <button onclick="testConnection()">Test connection to UiServer</button>
  71. <br><br>
  72. <div class="note">
  73. Note: This zite requires the PeerMessage plugin in order to be able
  74. to send and receive chat messages. If you get the error message
  75. "Unknown command: peerBroadcast" it may mean that you have not
  76. installed the PeerMessage plugin.
  77. You can download it from <a href="/1CeEXxqemr5CcVQAAmrW13QYwZV4kAkQz6" target="_blank">here</a>.
  78. </div>
  79. <br>
  80. <div class="note">
  81. Tip: To see how many there are in the room, you can simply write ping.
  82. Anyone who has a tick on "Automatically reply to a ping message with a pong" will reply with a pong.
  83. </div>
  84. <script src="js/ZeroFrame.js"></script>
  85. <script>
  86. zeroFrame = new ZeroFrame();
  87. var identity_provider = ["zeroid.bit", "cryptoid.bit", "kaffie.bit", "zv.anonymous", "0ch.anonymous", "millchan"];
  88. var input = document.getElementById("msgInput");
  89. var popupIntervall = 3000;
  90. initPage();
  91. function initPage() {
  92. zeroFrame.cmd("siteInfo", [], (res) => {
  93. document.getElementById("numPeers").innerHTML = res.peers;
  94. if (res.tasks !== 0) {
  95. zeroFrame.cmd("wrapperNotification",
  96. ["info", "Zite is still downloading. This could affect the functionality.", popupIntervall]);
  97. }
  98. });
  99. }
  100. function htmlEncode(str) {
  101. return String(str).replace(/[^\w. ]/gi, (c) => {
  102. return '&#' + c.charCodeAt(0) + ';';
  103. });
  104. }
  105. function updateNumPeers() {
  106. zeroFrame.cmd("siteInfo", [], (res) => {
  107. document.getElementById("numPeers").innerHTML = res.peers;
  108. zeroFrame.cmd("wrapperNotification",
  109. ["done", "Number of peers updated.", popupIntervall]);
  110. });
  111. }
  112. function testConnection() {
  113. zeroFrame.cmd("ping", [], (pong) => {
  114. if (pong === "pong") {
  115. zeroFrame.cmd("wrapperNotification",
  116. ["done", "Connection to UiServer websocket successful.", popupIntervall]);
  117. } else {
  118. zeroFrame.cmd("wrapperNotification",
  119. ["error", "Connection to UiServer websocket failed."]);
  120. }
  121. });
  122. }
  123. function requireLogin(func, msg = true) {
  124. zeroFrame.cmd("siteInfo", [], (res) => {
  125. if (res.cert_user_id === null) {
  126. if (msg) {
  127. zeroFrame.cmd("wrapperNotification",
  128. ["error", "In order to use this function, an identity must be selected."]);
  129. }
  130. } else {
  131. func();
  132. }
  133. });
  134. }
  135. function login() {
  136. zeroFrame.cmd("certSelect", [identity_provider], (res) => {
  137. if (res === "ok") {
  138. zeroFrame.cmd("siteInfo", [], (res) => {
  139. if (res.cert_user_id !== null) {
  140. /* Show message when selecting an identity successfully. */
  141. zeroFrame.cmd("wrapperNotification",
  142. ["done", "Identity successfully selected.", popupIntervall]);
  143. }
  144. });
  145. }
  146. });
  147. }
  148. function sendMessage() {
  149. if (input.value !== "") {
  150. input.readOnly = true;
  151. zeroFrame.cmd("peerBroadcast", [input.value, false], (res) => {
  152. if (res.sent) {
  153. if (document.getElementById("notifyWhenSend").checked) {
  154. zeroFrame.cmd('wrapperNotification',
  155. ['done', 'Sent message!', popupIntervall]);
  156. }
  157. input.value = "";
  158. } else {
  159. var errorMsg = "Message not sent";
  160. if (res.error) {
  161. errorMsg += " - " + res.error;
  162. }
  163. errorMsg += "!";
  164. zeroFrame.cmd("wrapperNotification", ["error", errorMsg]);
  165. }
  166. input.readOnly = false;
  167. });
  168. }
  169. }
  170. function clearMessages() {
  171. document.getElementById("history").innerHTML = "";
  172. }
  173. function sendMsgPong() {
  174. sendPong(true);
  175. }
  176. function sendPong(msg = false) {
  177. zeroFrame.cmd("peerBroadcast", ["pong", false], (res) => {
  178. if (msg) {
  179. if (res.sent) {
  180. if (document.getElementById("notifyWhenSend").checked) {
  181. zeroFrame.cmd('wrapperNotification',
  182. ['done', 'Sent pong!', popupIntervall]);
  183. }
  184. } else {
  185. var errorMsg = "Pong not sent";
  186. if (res.error) {
  187. errorMsg += " - " + res.error;
  188. }
  189. errorMsg += "!";
  190. zeroFrame.cmd("wrapperNotification", ["error", errorMsg]);
  191. }
  192. }
  193. });
  194. }
  195. input.addEventListener("keyup", function(event) {
  196. /* send when press enter */
  197. if (event.keyCode === 13) {
  198. event.preventDefault();
  199. document.getElementById("sendButton").click();
  200. }
  201. });
  202. zeroFrame.onRequest = function(cmd, message) {
  203. if (cmd === "peerReceive") {
  204. /* receive message */
  205. var text = message.params.message;
  206. var nick = message.params.cert;
  207. if (text === "" || nick === "") {
  208. /* ban message when empty */
  209. zeroFrame.cmd("peerInvalid", [message.params.hash]);
  210. } else {
  211. // if ( ! ((text === "ping" || text === "pong") && ! document.getElementById("showping").checked) ) {
  212. if ( (text !== "ping") && (text !== "pong") || document.getElementById("showping").checked) {
  213. document.getElementById("history").innerHTML +=
  214. '<li><span class="nick">' + htmlEncode(nick) +
  215. '#' + htmlEncode(message.params.signed_by.substring(1, 5)) + '</span>: ' +
  216. '<span class="msg">' + htmlEncode(text) + '</span></li>' +
  217. "\n";
  218. /* autoscroll */
  219. var chat = document.getElementById("chat");
  220. chat.scrollTop = chat.scrollHeight;
  221. if (document.getElementById("notifyWhenReceive").checked) {
  222. zeroFrame.cmd('wrapperNotification',
  223. ['done', 'Message received!', popupIntervall]);
  224. }
  225. }
  226. /* broadcast message to other peers */
  227. zeroFrame.cmd("peerValid", [message.params.hash]);
  228. if (text === "ping" && document.getElementById("autoping").checked) {
  229. requireLogin(sendPong, false);
  230. }
  231. }
  232. }
  233. };
  234. </script>
  235. </body>
  236. </html>