-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
153 lines (144 loc) · 6.37 KB
/
index.html
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html lang="en">
<head>
<title>Web Bot</title>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="bootstrap-dark/bootstrap-dark.css" />
<link rel="stylesheet" href="css/prism.css" />
</head>
<body>
<div class="container">
<form action="" method="post">
<div class="form-group">
<label for="chatCommand" class="col col-form-label">Chat Command:</label>
<div class="input-group">
<input type="text" class="form-control" name="chatCommand" id="chatCommand" />
<div class="input-group-append">
<button type="submit" class="btn btn-outline-primary" id="submitMessage">Send</button>
<button type="button" class="btn btn-outline-danger" id="cleanMessages">Clear</button>
<button
type="button"
class="btn btn-outline-dark dropdown-toggle dropdown-toggle-split"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
<span class="sr-only">Toggle Dropdown</span>
</button>
<div class="dropdown-menu" id="historyMenu"></div>
</div>
</div>
</div>
</form>
<div class="row">
<div class="col"><div id="chat"></div></div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
crossorigin="anonymous"
></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/marked@0.6.1/marked.min.js"
integrity="sha256-dRC6ZSSJmlr23+v1PSka2NCREJXHfn1djETNTjmGFQ4="
crossorigin="anonymous"
></script>
<script src="js/prism.js"></script>
<script>
const $chatCommand = $('#chatCommand');
const chat = $('#chat');
let messageCount = 0;
$('#submitMessage').on('click', e => {
e.preventDefault();
const command = $chatCommand.val();
const card = $('<div class="card">');
const id = `card${messageCount++}`;
const button = appendHeaderToCard(card, command, id);
$.ajax({
url: '/command',
method: 'POST',
data: { chatCommand: command }
}).done(msg => appendContentToCard(card, msg, id, button));
chat.prepend(card);
saveCommand(command);
});
$('#cleanMessages').on('click', () => chat.empty());
function appendHeaderToCard(card, content, id) {
const button = $('<button class="btn btn-link">')
.attr('data-toggle', 'collapse')
.attr('data-target', `#${id}`)
.text(`"${content}" [${new Date()}]`);
const cardHeader = $('<div class="card-header">');
const h5 = $('<h5 class="mb-0">');
card.append(cardHeader);
cardHeader.append(h5);
h5.append(button);
return button;
}
function appendContentToCard(card, content, id, button) {
var filteredContent = filter(content);
var markedContent = marked(filteredContent);
const cardBody = $('<pre class="card-body">').html(markedContent);
const collapseWrapper = $('<div class="collapse">')
.attr('id', id)
.attr('data-parent', '#chat')
.append(cardBody);
card.append(collapseWrapper);
button.get(0).click();
highlightCode(card);
}
function filter(content) {
return content;
// Comment the return to apply the filters
var jsonStartIndex = content.indexOf('{');
var jsonEndIndex = content.lastIndexOf('}');
var jsonString = content.substring(jsonStartIndex, jsonEndIndex + 1);
var obj = JSON.parse(jsonString);
var output = JSON.stringify(
{
alignment: obj.alignment,
seed: obj.initialSeed
},
null,
2
);
return '```json\r\n' + output + '\r\n```';
}
function highlightCode(card) {
Prism.highlightElement($('.card-body code', card).get(0));
}
// History
const commands = [];
const $historyMenu = $('#historyMenu');
$historyMenu.on('click', 'a', function(event) {
event.preventDefault();
console.log(this);
var command = $(this).text();
$chatCommand.val(command);
});
function saveCommand(command) {
if (!commands.some(c => c === command)) {
commands.push(command);
$historyMenu.prepend($(`<a class="dropdown-item" href="#">${command}</a>`));
}
}
Prism.highlightAll();
</script>
</body>
</html>