Skip to content

Instantly share code, notes, and snippets.

@umnikos
Last active January 31, 2022 14:09
Show Gist options
  • Save umnikos/bbba9f1e22a63420ad3bc3713af7286c to your computer and use it in GitHub Desktop.
Save umnikos/bbba9f1e22a63420ad3bc3713af7286c to your computer and use it in GitHub Desktop.
Kaboom save and load buttons (https://pwmarcz.pl/kaboom/)
// ==UserScript==
// @name Kaboom save and load
// @namespace Kaboom
// @match https://pwmarcz.pl/kaboom/
// @grant none
// @version 1.0
// @author umnikos
// ==/UserScript==
function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
// recursing version of Object.assign
function mergeDeep(target, source) {
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return target;
}
function saveGame() {
return JSON.stringify(game);
}
function loadGame(mygame) {
const width = parseInt(document.getElementById('width').value, 10);
const height = parseInt(document.getElementById('height').value, 10);
const numMines = parseInt(document.getElementById('numMines').value, 10);
const gameElement = document.getElementById('game');
gameElement.innerHTML = '';
game = new Game(width, height, numMines);
game = mergeDeep(new Game(), JSON.parse(mygame));
game.mount(gameElement);
updateSettings();
updateSize();
}
function exportGame() {
console.log("exporting game...");
saved_game = saveGame();
let filename = "kaboom_save.txt";
let data = saved_game;
let type = "text/plain";
var file = new Blob([data], {type: type});
var output = document.createElement("a");
var url = URL.createObjectURL(file);
output.href = url;
output.download = filename;
output.click();
}
function importGame() {
var input = document.createElement('input');
input.type = 'file';
input.onchange = (function(e) {
var file = e.target.files[0];
var reader = new FileReader();
reader.readAsText(file,'UTF-8');
reader.onload = (function(readerEvent) {
var content = readerEvent.target.result;
saved_game = content;
loadGame(saved_game);
});
});
input.click();
}
function makeSaveLoadButtons() {
let undoButton = document.getElementById('undo');
let mydiv = document.createElement('div');
undoButton.parentElement.parentElement.insertBefore(mydiv, undoButton.parentElement);
saveButton = document.createElement('button');
saveButton.onclick = (function() {
saved_game = saveGame();
});
saveButton.classList = undoButton.classList;
saveButton.innerHTML = "Save";
loadButton = document.createElement('button');
loadButton.onclick = (function() {
loadGame(saved_game);
});
loadButton.classList = undoButton.classList;
loadButton.innerHTML = "Load";
exportButton = document.createElement('button');
exportButton.onclick = (function() {
exportGame();
});
exportButton.classList = undoButton.classList;
exportButton.innerHTML = "Save & Export";
importButton = document.createElement('button');
importButton.onclick = (function() {
importGame();
});
importButton.classList = undoButton.classList;
importButton.innerHTML = "Import & Load";
// order to taste
mydiv.append(importButton);
mydiv.append(" ");
mydiv.append(loadButton);
mydiv.append(" ");
mydiv.append(saveButton);
mydiv.append(" ");
mydiv.append(exportButton);
}
(function() {
window.mergeDeep = mergeDeep;
window.saveGame = saveGame;
window.loadGame = loadGame;
makeSaveLoadButtons();
saved_game = null;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment