Guest User

Untitled

a guest
Jun 25th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. // Set name
  2. let color = ['brown', 'black', 'yellow', 'white', 'grey', 'red'][Math.floor(Math.random()*6)];
  3. let breed = ['ragamuffin', 'persian', 'siamese', 'siberian', 'birman', 'bombay', 'ragdoll'][Math.floor(Math.random()*7)];
  4. if (!localStorage.name) localStorage.name = color + '_' + breed;
  5.  
  6. // Utility functions
  7. let cookie = (name) => (document.cookie.match(new RegExp(`(?:^|; )${name}=(.*?)(?:$|;)`)) || [])[1];
  8. let esc = (str) => str.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, '&apos;');
  9.  
  10. // Sending messages
  11. let send = (msg) => fetch(`send?name=${encodeURIComponent(localStorage.name)}&msg=${encodeURIComponent(msg)}`,
  12. {credentials: 'include'}).then((res) => res.json()).then(handle);
  13. let display = (line) => conversation.insertAdjacentHTML('beforeend', `<p>${line}</p>`);
  14. let recaptcha_id = '6LeB410UAAAAAGkmQanWeqOdR6TACZTVypEEXHcu';
  15. window.addEventListener('load', function() {
  16. messagebox.addEventListener('keydown', function(event) {
  17. if (event.keyCode == 13 && messagebox.value != '') {
  18. if (messagebox.value == '/report') {
  19. grecaptcha.execute(recaptcha_id, {action: 'report'}).then((token) => send('/report ' + token));
  20. } else {
  21. send(messagebox.value);
  22. }
  23. messagebox.value = '';
  24. }
  25. });
  26. send('Hi all');
  27. });
  28.  
  29. // Receiving messages
  30. function handle(data) {
  31. ({
  32. undefined(data) {},
  33. error(data) { display(`Something went wrong :/ Check the console for error message.`); console.error(data); },
  34. name(data) { display(`${esc(data.old)} is now known as ${esc(data.name)}`); },
  35. rename(data) { localStorage.name = data.name; },
  36. secret(data) { display(`Successfully changed secret to <span data-secret="${esc(cookie('flag'))}">*****</span>`); },
  37. msg(data) {
  38. let you = (data.name == localStorage.name) ? ' (you)' : '';
  39. if (!you && data.msg == 'Hi all') send('Hi');
  40. display(`<span data-name="${esc(data.name)}">${esc(data.name)}${you}</span>: <span>${esc(data.msg)}</span>`);
  41. },
  42. ban(data) {
  43. if (data.name == localStorage.name) {
  44. document.cookie = 'banned=1; Path=/';
  45. sse.close();
  46. display(`You have been banned and from now on won't be able to receive and send messages.`);
  47. } else {
  48. display(`${esc(data.name)} was banned.<style>span[data-name^=${esc(data.name)}] { color: red; }</style>`);
  49. }
  50. },
  51. })[data.type](data);
  52. }
  53. let sse = new EventSource("receive");
  54. sse.onmessage = (msg) => handle(JSON.parse(msg.data));
  55.  
  56. // Say goodbye
  57. window.addEventListener('unload', () => navigator.sendBeacon(`send?name=${encodeURIComponent(localStorage.name)}&msg=Bye`));
  58.  
  59. // Admin helper function. Invoke this to automate banning people in a misbehaving room.
  60. // Note: the admin will already have their secret set in the cookie (it's a cookie with long expiration),
  61. // so no need to deal with /secret and such when joining a room.
  62. function cleanupRoomFullOfBadPeople() {
  63. send(`I've been notified that someone has brought up a forbidden topic. I will ruthlessly ban anyone who mentions d*gs going forward. Please just stop and start talking about cats for d*g's sake.`);
  64. last = conversation.lastElementChild;
  65. setInterval(function() {
  66. var p;
  67. while (p = last.nextElementSibling) {
  68. last = p;
  69. if (p.tagName != 'P' || p.children.length < 2) continue;
  70. var name = p.children[0].innerText;
  71. var msg = p.children[1].innerText;
  72. if (msg.match(/dog/i)) {
  73. send(`/ban ${name}`);
  74. send(`As I said, d*g talk will not be tolerated.`);
  75. }
  76. }
  77. }, 1000);
  78. }
Add Comment
Please, Sign In to add comment