-
Notifications
You must be signed in to change notification settings - Fork 0
/
globalFuncs.js
132 lines (130 loc) · 3.34 KB
/
globalFuncs.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
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
function throwError(msg) {
throw msg;
}
function internalError(msg) {
Swal.fire({
icon: 'error',
title: 'Error: ' + msg,
showConfirmButton: false,
timer: 3000
})
console.log('Error:', msg);
}
function debug(msg) {
if (debuging) {
console.log(msg);
}
}
function say(environment, msg) {
console.log(SayText + msg);
}
function sign(num) {
if (num > 0) {
return 1;
}else if (num < 0) {
return -1;
}else {
return 0;
}
}
function WordToNum(word) {
if (word.indexOf('-') != -1) {
word = '-' + word.replace('-', '');
}
return Number(word);
}
function preAddress(currentAddress, command) {
if (f0Commands.hasOwnProperty(command) || f1Commands.hasOwnProperty(command)) {
if (f0Commands.hasOwnProperty(command)) {
return currentAddress-1;
}else {
return currentAddress-3;
}
}else {
return currentAddress;
}
}
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}
function download(data) {
Swal.fire({
html: `<input type="text" class="file-name" value="${chromeTabs.activeTabEl.querySelector('.chrome-tab-title').textContent}">
<span style="font-family: system-ui;font-weight: bold;margin: -8px;">.</span>
<select class="type-selector">
<option selected="selected">miku</option>
<option>mik</option>
<option>mis</option>
<option>txt</option>
</select>`,
confirmButtonText: 'Экспортировать'
}).then((result) => {
if (result.isConfirmed) {
let type = $('.type-selector').val();
let filename = $('.file-name').val() + '.' + type;
console.log(filename);
switch (type) {
case 'miku':
data = data.toString().replaceAll(',', '');
break;
case 'mik':
data = data.toString().replaceAll(',', '');
break;
case 'mis':
data = data.toString().replaceAll(',', '');
break;
case 'txt':
data = data.toString().replaceAll(',', '');
break;
default:
}
let file = new Blob([data.toString().replaceAll(',', '')]);
if (window.navigator.msSaveOrOpenBlob)
window.navigator.msSaveOrOpenBlob(file, filename);
else {
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
});
}
function open(memory) {
Swal.fire({
input: 'file',
confirmButtonText: 'Импортировать'
}).then((result) => {
if (result.isConfirmed) {
let reader = new FileReader();
reader.readAsText(result.value);
reader.onload = function() {
debug(reader.result);
let typeOfFile = result.value.name.substring(result.value.name.lastIndexOf('.')+1);
if (typeOfFile === 'miku') {
// TODO
}else if(typeOfFile === 'json'){
reader.result = JSON.parse(reader.result);
for (var i = 0; i < reader.result.length; i++) {
memory.write(i, reader.result[i], false);
}
}else {
for (var i = 0; i < reader.result.length; i+=2) {
if (reader.result.substring(i, i+2) != memory.line[i/2]) {
memory.write(i/2, reader.result.substring(i, i+2), false);
}
}
}
};
reader.onerror = function() {
console.log(reader.error);
};
}
});
}