This repository was archived by the owner on Apr 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomepage.js
More file actions
78 lines (75 loc) · 2.16 KB
/
homepage.js
File metadata and controls
78 lines (75 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// I just love stackoverflow
// https://stackoverflow.com/questions/12982156/select-copy-text-using-javascript-or-jquery/34503498
function copy_text(element) {
//Before we copy, we are going to select the text.
var text = document.getElementById(element);
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
//add to clipboard.
document.execCommand('copy');
$(text).notify("Copied start command", "success");
}
function decompile_code() {
var str = $("#inputgeneratorcode").val();
str = str.replace(/^.start /, '');
var raw = LZString.decompressFromEncodedURIComponent(str);
var data = {};
try {
data = JSON.parse(raw);
} catch (err) {
$("#inputgeneratorcode").notify("Error reading this generator code", "error");
return;
}
var json = {
m: {},
...data
};
var k = Object.keys(json.m);
$(".mines-num").val('');
for (var i = 0; i < k.length; i++) {
$(".mines-num[num=" + k[i] + "]").val(json.m[k[i]]);
}
$(".board-num").val('');
for (var i = 0; i < k.length; i++) {
$(".board-num[num=" + k[i] + "]").val(json.b[k[i]]);
}
$("#inputgeneratorcode").notify("Loaded data from generator code", "success");
generate_code();
}
function generate_code() {
var data = {
m: {},
b: {}
}
var $mines = $(".mines-num")
for (var i = 0; i < $mines.length; i++) {
try {
var j = parseInt($($mines[i]).val());
if (j > 0 && j != null) {
data.m[$($mines[i]).attr('num')] = j;
}
} catch (err) {
/* ignore this? */
}
}
var $board = $(".board-num")
for (var i = 0; i < $board.length; i++) {
try {
var j = parseInt($($board[i]).val());
if (j > 0 && j != null) {
data.b[$($board[i]).attr('num')] = j;
}
} catch (err) {
/* ignore this? */
}
}
$("#outputgeneratorcode").text(">start " + LZString.compressToEncodedURIComponent(JSON.stringify(data)));
}
$(document).ready(function () {
$("#inputgeneratorbutton").click(decompile_code);
$(".mines-num").on('keyup', generate_code);
$(".board-num").on('keyup', generate_code);
})