Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Wrap everything in a closure, to avoid polluting the global namespace
- (() => {
- // Install jQuery
- var jqry = document.createElement('script');
- jqry.src = "https://code.jquery.com/jquery-3.3.1.min.js";
- document.getElementsByTagName('head')[0].appendChild(jqry);
- // Install firebase
- var firebs = document.createElement('script');
- firebs.src = "https://www.gstatic.com/firebasejs/7.9.1/firebase-app.js";
- document.getElementsByTagName('head')[0].appendChild(firebs);
- var firest = document.createElement('script');
- firest.src = "https://www.gstatic.com/firebasejs/7.9.1/firebase-firestore.js";
- document.getElementsByTagName('head')[0].appendChild(firest);
- // Add a delay so dependencies can load
- setTimeout(() => {
- // Your web app's Firebase configuration
- var firebaseConfig = {
- apiKey: "AIzaSyBVFhBn2-ssqHMGIgW8KRj3HbnrIMN6sYk",
- authDomain: "crosswords-44d95.firebaseapp.com",
- databaseURL: "https://crosswords-44d95.firebaseio.com",
- projectId: "crosswords-44d95",
- storageBucket: "crosswords-44d95.appspot.com",
- messagingSenderId: "794243401614",
- appId: "1:794243401614:web:dc2fc93575091367af46df"
- };
- // Initialize Firebase
- firebase.initializeApp(firebaseConfig);
- var db = firebase.firestore();
- function getLetters() {
- return $('svg>g>g').map((i, item) => {
- return $($(item).find('text:last-child text')[0]).text();
- }).toArray();
- }
- function getCookie(name) {
- var value = "; " + document.cookie;
- var parts = value.split("; " + name + "=");
- if (parts.length == 2) return parts.pop().split(";").shift();
- }
- function clearAuxillaryLetters() {
- $('.colab_mod').remove();
- }
- function appendLetter(index, letter) {
- var last = $('svg>g>g:nth-child(' + (index-1) + ')>text:last-child');
- console.log(last);
- $('svg').append(
- $(document.createElementNS('http://www.w3.org/2000/svg', 'text'))
- .text(letter)
- .addClass('colab_mod')
- .attr('x', last.attr('x'))
- .attr('y', last.attr('y'))
- .attr('font-size', last.attr('font-size'))
- .attr('text-anchor', last.attr('text-anchor'))
- .css('opacity', 0.5));
- }
- function syncWithFirebase(doc) {
- var other_participants = doc.data();
- delete other_participants[myId];
- console.log("Current data: ", other_participants);
- var my_letters = getLetters();
- clearAuxillaryLetters();
- for (key in other_participants) {
- var their_letters = other_participants[key];
- for (var i = 0; i < my_letters.length; i++) {
- console.log(i, my_letters[i], their_letters[i]);
- if (my_letters[i] != their_letters[i]) {
- appendLetter(i, their_letters[i]);
- }
- }
- }
- }
- var myId = getCookie("nyt-a");
- var docId = window.location.pathname.replace(/\//g,"_");
- document.addEventListener('keyup', () => {
- var letters = getLetters();
- console.log('Sending board to Firebase.', letters);
- var doc = {};
- doc[myId] = letters;
- db.collection('crosswords')
- .doc(docId)
- .update(doc);
- });
- db.collection("crosswords").doc(docId).onSnapshot(function(doc) {
- syncWithFirebase(doc);
- });
- }, 100);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement