Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. // Wrap everything in a closure, to avoid polluting the global namespace
  2. (() => {
  3. // Install jQuery
  4. var jqry = document.createElement('script');
  5. jqry.src = "https://code.jquery.com/jquery-3.3.1.min.js";
  6. document.getElementsByTagName('head')[0].appendChild(jqry);
  7.  
  8. // Install firebase
  9. var firebs = document.createElement('script');
  10. firebs.src = "https://www.gstatic.com/firebasejs/7.9.1/firebase-app.js";
  11. document.getElementsByTagName('head')[0].appendChild(firebs);
  12.  
  13. var firest = document.createElement('script');
  14. firest.src = "https://www.gstatic.com/firebasejs/7.9.1/firebase-firestore.js";
  15. document.getElementsByTagName('head')[0].appendChild(firest);
  16.  
  17. // Add a delay so dependencies can load
  18. setTimeout(() => {
  19. // Your web app's Firebase configuration
  20. var firebaseConfig = {
  21. apiKey: "AIzaSyBVFhBn2-ssqHMGIgW8KRj3HbnrIMN6sYk",
  22. authDomain: "crosswords-44d95.firebaseapp.com",
  23. databaseURL: "https://crosswords-44d95.firebaseio.com",
  24. projectId: "crosswords-44d95",
  25. storageBucket: "crosswords-44d95.appspot.com",
  26. messagingSenderId: "794243401614",
  27. appId: "1:794243401614:web:dc2fc93575091367af46df"
  28. };
  29. // Initialize Firebase
  30. firebase.initializeApp(firebaseConfig);
  31.  
  32. var db = firebase.firestore();
  33.  
  34. function getLetters() {
  35. return $('svg>g>g').map((i, item) => {
  36. return $($(item).find('text:last-child text')[0]).text();
  37. }).toArray();
  38. }
  39.  
  40. function getCookie(name) {
  41. var value = "; " + document.cookie;
  42. var parts = value.split("; " + name + "=");
  43. if (parts.length == 2) return parts.pop().split(";").shift();
  44. }
  45.  
  46. function clearAuxillaryLetters() {
  47. $('.colab_mod').remove();
  48. }
  49.  
  50. function appendLetter(index, letter) {
  51. var last = $('svg>g>g:nth-child(' + (index-1) + ')>text:last-child');
  52. console.log(last);
  53. $('svg').append(
  54. $(document.createElementNS('http://www.w3.org/2000/svg', 'text'))
  55. .text(letter)
  56. .addClass('colab_mod')
  57. .attr('x', last.attr('x'))
  58. .attr('y', last.attr('y'))
  59. .attr('font-size', last.attr('font-size'))
  60. .attr('text-anchor', last.attr('text-anchor'))
  61. .css('opacity', 0.5));
  62. }
  63.  
  64. function syncWithFirebase(doc) {
  65. var other_participants = doc.data();
  66. delete other_participants[myId];
  67. console.log("Current data: ", other_participants);
  68. var my_letters = getLetters();
  69. clearAuxillaryLetters();
  70. for (key in other_participants) {
  71. var their_letters = other_participants[key];
  72. for (var i = 0; i < my_letters.length; i++) {
  73. console.log(i, my_letters[i], their_letters[i]);
  74. if (my_letters[i] != their_letters[i]) {
  75. appendLetter(i, their_letters[i]);
  76. }
  77. }
  78. }
  79. }
  80.  
  81. var myId = getCookie("nyt-a");
  82. var docId = window.location.pathname.replace(/\//g,"_");
  83.  
  84. document.addEventListener('keyup', () => {
  85. var letters = getLetters();
  86. console.log('Sending board to Firebase.', letters);
  87. var doc = {};
  88. doc[myId] = letters;
  89. db.collection('crosswords')
  90. .doc(docId)
  91. .update(doc);
  92. });
  93.  
  94. db.collection("crosswords").doc(docId).onSnapshot(function(doc) {
  95. syncWithFirebase(doc);
  96. });
  97. }, 100);
  98. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement