2048 save game

This script allows to save and restore any previous state of the popular 2048 game.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        2048 save game
// @description This script allows to save and restore any previous state of the popular 2048 game.
// @namespace   https://github.com/Lorentz83/
// @include     http://gabrielecirulli.github.io/2048/
// @version     1.0
// @grant       none
// @icon        https://raw.githubusercontent.com/Lorentz83/userscripts/master/2048SaveGame/icon.png
// @license     GPLv2; http://www.gnu.org/licenses/
// ==/UserScript==

var game = {
	stateBox : null,
    isOver : function() {
        var over = document.querySelector('div.game-message.game-over');
        return over !== null;
    },
	state : function() {
		var bk = window.localStorage.getItem('gameStateBK');
		var date = 'none';
		if ( bk !== null) {
			date = JSON.parse(bk).date;
		}
		game.stateBox.textContent = date;
	},
	save : function() {
        if ( game.isOver() ){
            alert('the game is over!');
            return;
        }
		var state = window.localStorage.getItem('gameState');
		var date = new Date();
		date = date.getFullYear()+'-'+date.getMonth()+'-'+date.getDate()+' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();;
		var bk = {
			date : date,
			state : state
		}
		window.localStorage.setItem('gameStateBK', JSON.stringify(bk));
		game.state();
		this.blur();
	} ,
	restore : function() {
		var bk = window.localStorage.getItem('gameStateBK');
		if ( bk !== null) {
			window.localStorage.setItem('gameState', JSON.parse(bk).state);
			window.location.reload();
		}
        this.blur();
	}
}


var gc = document.querySelector('.game-container');
var saveBox = document.createElement('div');
gc.parentNode.insertBefore(saveBox, gc.nextSibling);


var saveBtn = document.createElement('input');
saveBtn.type='button';
saveBtn.value='save';
saveBtn.addEventListener('click', game.save);
saveBox.appendChild(saveBtn);

var restoreBtn = document.createElement('input');
restoreBtn.type='button';
restoreBtn.value='restore';
restoreBtn.addEventListener('click', game.restore);
saveBox.appendChild(restoreBtn);

var span = document.createElement('span');
saveBox.appendChild(span);
span.textContent = 'last saved: ';
game.stateBox = document.createElement('span');
span.appendChild(game.stateBox);

game.state();