forked from AnneTrue/nexus-clash-interface-tweaks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
saveLogs.js
62 lines (57 loc) · 2.53 KB
/
saveLogs.js
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
const saveLogs = {
module: async (api) => {
const mod = await api.registerModule(
'logSaver',
'Message Log Saver',
'global',
'Enables saving NC message log into a text file with a single press of a button.\n' +
'You\'ll need to reload before seeing the dowload button.',
);
const download = (filename, text) => {
const element = document.createElement('a');
element.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(text);
element.download = filename;
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
const charLogsRequest = (charId, callback) => {
GM.xmlHttpRequest({
method: 'POST',
url: `https://www.nexusclash.com/clash.php?op=character&id=${charId}`,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: 'displaymessages=Display',
onload: callback
});
}
const logSaver = () => {
'use strict';
if (!mod.API.inGame) return;
const charID = mod.API.charinfo.id;
document.querySelector('#nexus-tweaks-settings-button').addEventListener('click', function() {
const dwldButton = document.querySelector('#nexus-tweaks-setting-logSaver')
.appendChild(document.createElement('tr'))
.appendChild(document.createElement('td'))
.appendChild(document.createElement('input'));
dwldButton.type = 'button';
dwldButton.value = 'Download Logs';
dwldButton.style.color = 'black';
dwldButton.onclick = function () {
charLogsRequest(charID, async function(response) {
var parser = new DOMParser();
var htmlDoc = parser.parseFromString(response.responseText, 'text/html');
const logDiv = htmlDoc.querySelector('div#Messages');
const logs = logDiv.textContent;
const d = new Date();
download(`NC logs ${d.getFullYear()}-${d.getMonth()+1}-${d.getDate()} ${api.charinfo.name}.txt`, logs);
});
};
});
}
await mod.registerMethod(
'sync',
logSaver
);
}
}