-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.js
247 lines (218 loc) · 5.47 KB
/
Common.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// ONLY CONTAINS SCRIPTS THAT I MADE, MODIFIED, OR ONES MINE DEPEND ON (NAMELY CREATEPAGE, GETTOKEN) //
var apiUrl = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/api.php"
/* Script in here will be executed when the page is "ready" */
$(document).ready(getNumberOfActiveUsers);
/* show correct number of active users on the main page */
function getNumberOfActiveUsers() {
if (document.getElementById("active-users")) {
$.ajax({
url: apiUrl,
data: {
format: 'json',
action: 'query',
list: 'allusers',
aulimit: 500,
auactiveusers: true
},
dataType: 'json',
type: 'GET',
success: function(data) {
document.getElementById("active-users").innerHTML = data.query.allusers.length.toString();
},
error: function(xhr) {
console.log( 'Error: Request failed.' );
}
});
}
};
/* Template:Inventory tooltips */
var lastTouchTime = 0;
document.addEventListener('touchstart', updateLastTouchTime, true);
function updateLastTouchTime() {
lastTouchTime = new Date();
}
$(".tab-head").mousemove(function(e) {
if (e.buttons > 0) return;
if (new Date() - lastTouchTime < 500) return;
var countCssRules = document.styleSheets[0].cssRules.length;
var newRule = '.tab-head:hover:after{display: block; left: ' + (e.offsetX + 20) + 'px; top: ' + (e.offsetY + 20) + 'px;}';
document.styleSheets[0].insertRule(newRule, countCssRules);
});
$(".tab .factorio-icon").mousemove(function(e) {
if (e.buttons > 0) return;
if (new Date() - lastTouchTime < 500) return;
var countCssRules = document.styleSheets[0].cssRules.length;
$(e.currentTarget).children("a").attr("title", "");
var text = $(e.currentTarget).children("a").children("img").attr("alt");
var newRule = '.tab .factorio-icon:hover:after{display: block; ' + "content: '" + text + "'}";
document.styleSheets[0].insertRule(newRule, countCssRules);
});
/* Template:BlueprintString */
$(".bps-box").click(function(event) {
var copyTarget = document.createElement("input");
copyTarget.setAttribute("value", $( event.target ).children("p").html());
document.body.appendChild(copyTarget);
copyTarget.select();
document.execCommand("copy");
document.body.removeChild(copyTarget);
});
/* Template:Inventory */
$(".tab-head-1").click(function() {
$(".tab-head").removeClass("tab-head-active");
$(this).addClass("tab-head-active");
$(".tab").hide();
$(".tab-1").show();
});
$(".tab-head-2").click(function() {
$(".tab-head").removeClass("tab-head-active");
$(this).addClass("tab-head-active");
$(".tab").hide();
$(".tab-2").show();
});
$(".tab-head-3").click(function() {
$(".tab-head").removeClass("tab-head-active");
$(this).addClass("tab-head-active");
$(".tab").hide();
$(".tab-3").show();
});
$(".tab-head-4").click(function() {
$(".tab-head").removeClass("tab-head-active");
$(this).addClass("tab-head-active");
$(".tab").hide();
$(".tab-4").show();
});
//* General/generic functions *//
/* User is bot if userGroup.some(isBot) == true */
var userGroup = "";
function getUserGroup() {
$.ajax({
url: apiUrl,
data: {
format: 'json',
action: 'query',
meta: 'userinfo',
uiprop: 'groups',
},
async: false,
dataType: 'json',
type: 'GET',
success: function(data) {
userGroup = data.query.userinfo.groups
},
});
};
function isBot(group) {
return group == "bot";
}
/* Get token of this session */
var globalToken;
function getToken() {
$.ajax({
url: apiUrl,
data: {
format: 'json',
action: 'query',
meta: 'tokens',
bot: true
},
async: false,
dataType: 'json',
type: 'POST',
success: function( data ) {
globalToken = data.query.tokens.csrftoken;
},
error: function( xhr ) {
console.log("Failed to get token.");
}
});
}
function genericEditPage(title, content, summary) {
$.ajax({
url: apiUrl,
data: {
format: 'json',
action: 'edit',
title: title,
text: content,
token: globalToken,
summary: summary,
bot: true,
nocreate: true
},
dataType: 'json',
type: 'POST',
success: function( data ) {
console.log("Edited " + title);
},
error: function( xhr ) {
alert("Failed to edit " + title);
}
});
};
function createPage(pageTitle, content, summary) {
$.ajax({
url: apiUrl,
data: {
format: 'json',
action: 'edit',
title: pageTitle,
text: content,
token: globalToken,
summary: summary,
bot: true
},
async: false,
dataType: 'json',
type: 'POST',
success: function( data ) {
console.log("Created page: " + pageTitle);
},
error: function( xhr ) {
console.log("Failed to create page");
}
});
}
function getBacklinks(page) {
var backlinks = [];
$.ajax({
url: apiUrl,
data: {
format: 'json',
action: 'query',
list: 'backlinks',
bltitle: page,
bllimit: 1000,
},
async: false,
type: 'GET',
success: function( data ) {
backlinks = data.query.backlinks;
},
error: function( xhr ) {
alert( 'Error: Backlinks request failed.' );
}
});
return backlinks;
};
function getFileUsage(file) {
var imageusage = [];
$.ajax({
url: apiUrl,
data: {
format: 'json',
action: 'query',
list: 'imageusage',
iutitle: file,
iulimit: 1000
},
async: false,
type: 'GET',
success: function( data ) {
imageusage = data.query.imageusage;
},
error: function( xhr ) {
alert( 'Error: Imageusage request failed.' );
}
});
return imageusage;
};