-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddons.user.js
More file actions
203 lines (182 loc) · 5.45 KB
/
Addons.user.js
File metadata and controls
203 lines (182 loc) · 5.45 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// ==UserScript==
// @name Spacom.Addons
// @version 0.1.2
// @namespace http://dimio.org/
// @description Provide Spacom.Addons library functions on spacom.ru
// @author dimio (dimio@dimio.org)
// @license MIT
// @homepage https://github.com/dimio/userscripts-spacom.ru-addons
// @supportURL https://github.com/dimio/userscripts-spacom.ru-addons/issues
// @supportURL https://spacom.ru/forum/discussion/47/polzovatelskie-skripty
// @encoding utf-8
// @match http*://spacom.ru/*
// @include http*://spacom.ru/*
// @run-at document-start
// ==/UserScript==
console.log(GM_info.script.name, 'booted v.', GM_info.script.version);
(function (window) {
'use strict';
window.unsafeWindow = window.unsafeWindow || window;
const w = unsafeWindow;
if (w.self !== w.top) {
return;
}
if (!w.Addons) {
w.Addons = {
// w: w,
Common: {},
Decor: {},
Design: {},
DOM: {},
Fleets: {},
Map: {},
Sort: {},
Stars: {},
};
}
const Addons = w.Addons;
Addons.Common = {
waitFor(obj, prop, callback) {
const token = setInterval(() => {
if (typeof obj[prop] !== 'undefined') {
clearInterval(token);
callback(obj[prop]);
}
}, 0);
},
waitObj(obj, callback) {
const token = setInterval(() => {
if (this.isVariableDefined(obj) && this.isObjNotEmpty(obj)) {
clearInterval(token);
callback(obj);
}
}, 0);
},
getObjCenter(opt) {
const obj = opt.obj;
const mode = opt.mode;
let center;
switch (mode) {
case 'mark':
center = w.getCenterXY(obj.x, obj.y);
break;
case 'viewzone':
if (+obj.turn === 0) {
center = w.getCenterXY(obj.x, obj.y);
}
else {
center = {x: obj.start_x, y: obj.start_y};
}
break;
}
return center;
},
isVariableDefined(value) {
if (typeof value === 'undefined') {
return false; //retrun value >>> 0
}
return value !== null;
},
isObjNotEmpty(obj) {
return Object.keys(obj).length !== 0 || obj.size !== 0;
},
};
Addons.DOM = {
appendClickableIcon(opt) {
const elem = opt.elem;
const icon = opt.icon;
const css_name = opt.css_name;
const title = opt.title;
const callback = opt.cb;
const appended_elem = $(elem);
const clickable_icon = document.createElement('a');
clickable_icon.href = '#';
clickable_icon.title = title;
clickable_icon.setAttribute('onclick', `${callback}; return false;`);
clickable_icon.innerHTML = ` <i class="fa ${icon}" id="${css_name}"></i></a>`;
appended_elem.append(clickable_icon);
return elem;
},
makeClickable(opt) {
const elem = opt.elem;
const icon = opt.icon;
const css_name = opt.css_name;
const title = opt.title;
const callback = opt.cb;
let text = elem.innerText;
if (icon) {
text = `<i id="${css_name}" class="fa ${icon}"></i> ${text}`;
}
elem.innerHTML = `<a href="#" title="${title}" onclick="${callback}; return false;">${text}</a>`;
return elem;
},
replaceContent(elem, clearElem) {
// replaceContent(elem) {
if (clearElem) {
$(elem).empty();
}
for (let i = 2; i < arguments.length; i++) {
// for (let i = 1; i < arguments.length; i++) {
$(elem).append(arguments[i]);
}
return elem;
},
createNaviBarButton(name, last_el_num, callback) {
const last = $(`#navi > div:nth-child(${last_el_num})`);
$(last).parent().css({
//width: `${parseInt($(last).parent().css('width'), 10) + 15}px`,
width: 'fit-content',
'padding-left': '5px',
'padding-right': '5px',
});
$(last).parent().children('*').css({
//'margin-left': '10px',
'margin-right': '5px',
});
const next = $(`<div class="${last.attr(
'class')}" onclick="${callback};"><a href="#">${name}</a></div>`);
last.after(next);
return next;
},
createActionButton(btn_text, css_class, css_id) {
return $(`<button class="btn-action" id="${css_id}" onclick="return false;">
<i class="${css_class}" aria-hidden="true"></i> <br><span class="button-text">${btn_text}</span>
<br></button>`);
},
createMapButton(css_class, id, title, imgSrc) {
const img = (imgSrc) ? `<img alt="" src="${imgSrc}"/>` : '';
const last = $('#radar + div');
const next = $(
`<div id="${id}" title="${title}">
<i class="fa ${css_class} fa-2x">
${img}
</i>
</div>`)
.css({
'z-index': last.css('z-index'),
position: last.css('position'),
cursor: last.css('cursor'),
color: last.css('color'),
right: last.css('right'),
bottom: `${parseInt(last.css('bottom'), 10) + 40}px`,
});
last.before(next);
return next;
},
};
Addons.Sort = {
alphabetically(a, b) {
return a.localeCompare(b);
},
numerically(a, b) {
return parseFloat(a) - parseFloat(b);
},
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
uniqueArray(array) {
// return array.filter((value, index, self) => {
// return self.indexOf(value) === index;
// })
return [...new Set(array)]
},
};
})(window);