Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.     /* Wrap everything in a closure, to avoid polluting the global namespace */
  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.         var firebaseConfig = {
  20.         apiKey: "AIzaSyBVFhBn2-ssqHMGIgW8KRj3HbnrIMN6sYk",
  21.         authDomain: "crosswords-44d95.firebaseapp.com",
  22.         databaseURL: "https://crosswords-44d95.firebaseio.com",
  23.         projectId: "crosswords-44d95",
  24.         storageBucket: "crosswords-44d95.appspot.com",
  25.         messagingSenderId: "794243401614",
  26.         appId: "1:794243401614:web:dc2fc93575091367af46df"
  27.         };
  28.         firebase.initializeApp(firebaseConfig);
  29.  
  30.         var db = firebase.firestore();
  31.  
  32.         function getLetters() {
  33.             return $('svg>g>g').map((i, item) => {
  34.                 return $($(item).find('text:last-child text')[0]).text();
  35.             }).toArray();
  36.         }
  37.  
  38.         function getCookie(name) {
  39.           var value = "; " + document.cookie;
  40.           var parts = value.split("; " + name + "=");
  41.           if (parts.length == 2) return parts.pop().split(";").shift();
  42.         }
  43.  
  44.         function clearAuxillaryLetters() {
  45.             $('.colab_mod').remove();
  46.         }
  47.  
  48.         function appendLetter(index, letter) {
  49.             if (letter.length == 0) {
  50.                 return;
  51.             }
  52.             var last = $('svg>g>g:nth-child(' + (index+1) + ')>text:last-child');
  53.             console.log(last);
  54.             $('svg').append(
  55.                 $(document.createElementNS('http://www.w3.org/2000/svg', 'text'))
  56.                 .text(letter)
  57.                 .addClass('colab_mod')
  58.                 .attr('x', last.attr('x'))
  59.                 .attr('y', last.attr('y'))
  60.                 .attr('font-size', last.attr('font-size') / letter.length)
  61.                 .attr('text-anchor', last.attr('text-anchor'))
  62.                 .css('opacity', 0.5)
  63.                 .css('pointer-events', 'none'));
  64.         }
  65.  
  66.         function syncWithFirebase(doc) {
  67.             var other_participants = doc.data();
  68.             if (other_participants[myId]) {
  69.                 delete other_participants[myId];
  70.             }
  71.             console.log("Current data: ", other_participants);
  72.             var my_letters = getLetters();
  73.             clearAuxillaryLetters();
  74.             for (key in other_participants) {
  75.                 var their_letters = other_participants[key];
  76.                 for (var i = 0; i < my_letters.length; i++) {
  77.                     console.log(i, my_letters[i], their_letters[i]);
  78.                     if (my_letters[i] != their_letters[i]) {
  79.                         appendLetter(i, their_letters[i]);
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.  
  85.         var myId = getCookie("nyt-a");
  86.         var docId = window.location.pathname.replace(/\//g,"_");
  87.  
  88.         document.addEventListener('keyup', () => {
  89.             var letters = getLetters();
  90.             console.log('Sending board to Firebase.', letters);
  91.             var doc = {};
  92.             doc[myId] = letters;
  93.             db.collection('crosswords')
  94.                 .doc(docId)
  95.                 .set(doc, {merge: true});
  96.         });
  97.  
  98.         db.collection("crosswords").doc(docId).onSnapshot(function(doc) {
  99.             syncWithFirebase(doc);
  100.         });
  101.     }, 100);
  102. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement