|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> |
| 5 | + <link rel="stylesheet" href="./index.css"> |
| 6 | + </head> |
| 7 | + <body> |
| 8 | + <h2>Scuttle Shell Browser example</h2> |
| 9 | + <h1>v02 - todays post</h1> |
| 10 | + <p>This is an browser equivalent of |
| 11 | + <a href="https://github.com/mixmix/ssb-client-basic#v02---todays-post">https://github.com/mixmix/ssb-client-basic#v02---todays-post</a></p> |
| 12 | + <div id="output"> |
| 13 | + <div style="background-color: yellow;" class="after_wait"> |
| 14 | + This Page needs to access Scuttlebut. Make sure that Scuttle Shell Browser is installed and that "SSB access" is |
| 15 | + switched on for this page. |
| 16 | + </div> |
| 17 | + </div> |
| 18 | + <script src="connect-ssb.js"></script> |
| 19 | + <script> |
| 20 | + const outElem = document.getElementById('output') |
| 21 | + |
| 22 | + const opts = { |
| 23 | + limit: 100, |
| 24 | + reverse: true |
| 25 | + } |
| 26 | + connectSsb().then(server => { |
| 27 | + outElem.innerHTML = '' |
| 28 | + |
| 29 | + function prettyPrint (msg) { |
| 30 | + outElem.innerHTML += `<pre>${JSON.stringify(msg, null, 2)}</pre>` |
| 31 | + outElem.innerHTML += '<hr>' |
| 32 | + // this just print the full object out as a string that's been nicely indented |
| 33 | + // with each level of nesting |
| 34 | + } |
| 35 | + |
| 36 | + const today = new Date() |
| 37 | + const opts = { |
| 38 | + reverse: true, |
| 39 | + query: [ |
| 40 | + { |
| 41 | + $filter: { |
| 42 | + value: { |
| 43 | + content: { type: 'post' }, |
| 44 | + timestamp: { |
| 45 | + $gte: Number(startOfDay(today)), |
| 46 | + $lt: Number(startOfDay(today, +1)) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + }, |
| 51 | + { |
| 52 | + $map: { |
| 53 | + author: ['value', 'author'], |
| 54 | + timestamp: ['value', 'timestamp'], |
| 55 | + text: ['value', 'content', 'text'], |
| 56 | + root: ['value', 'content', 'root'] // the root messages of a thread, this is present if this post is a reply to another message |
| 57 | + } |
| 58 | + } |
| 59 | + ] |
| 60 | + } |
| 61 | + |
| 62 | + pull( |
| 63 | + server.query.read(opts), |
| 64 | + pull.collect(onDone) |
| 65 | + ) |
| 66 | + |
| 67 | + function onDone (err, msgs) { |
| 68 | + if (err) { |
| 69 | + console.error('oh noes', err) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + msgs.forEach(msg => { |
| 74 | + prettyPrint(msg) |
| 75 | + |
| 76 | + }) |
| 77 | + } |
| 78 | + |
| 79 | + }) |
| 80 | + function startOfDay (time = new Date(), dayOffset = 0) { |
| 81 | + // dayOffset = 0 means if this argument is not supplied to set it to default to 0 |
| 82 | + |
| 83 | + const year = time.getFullYear() |
| 84 | + const month = time.getMonth() |
| 85 | + const date = time.getDate() + dayOffset |
| 86 | + return new Date(year, month, date, 0, 0, 0) // 0 hours, 0 minutes, 0 secords |
| 87 | + } |
| 88 | + |
| 89 | + setTimeout(() => document.body.classList.add('waited'), 1000) |
| 90 | + </script> |
| 91 | + </body> |
| 92 | +</html> |
0 commit comments