forked from samliew/SO-mod-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MigrationHelper.user.js
346 lines (299 loc) · 11.2 KB
/
MigrationHelper.user.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// ==UserScript==
// @name Migration Helper
// @description Dropdown list of migration targets displaying site icon/logo/header images and links to the selected site's on-topic page and mod list. Displays additional information for custom flagger for selected network site.
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 4.1.13
//
// @match https://*.stackoverflow.com/*
// @match https://*.serverfault.com/*
// @match https://*.superuser.com/*
// @match https://*.askubuntu.com/*
// @match https://*.mathoverflow.net/*
// @match https://*.stackapps.com/*
// @match https://*.stackexchange.com/*
// @match https://stackoverflowteams.com/*
//
// @exclude https://api.stackexchange.com/*
// @exclude https://data.stackexchange.com/*
// @exclude https://contests.stackoverflow.com/*
// @exclude https://winterbash*.stackexchange.com/*
// @exclude *chat.*
// @exclude *blog.*
// @exclude */tour
//
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/se-ajax-common.js
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/common.js
// ==/UserScript==
/* globals StackExchange, store */
/// <reference types="./globals" />
'use strict';
// This is a moderator-only userscript
if (!isModerator()) return;
const cdn = 'https://cdn.sstatic.net/Sites/';
const apikey = 'Wjm8SDrrbQSDSUwcLaifHA((';
let networkSites, networkSitenames, flaggeraccounts;
function getNetworkSites() {
const fullkey = 'NetworkSites';
let v = JSON.parse(store.getItem(fullkey));
return new Promise(function (resolve, reject) {
if (v != null) { resolve(v); return; }
$.get(`${seApiUrl}/sites?pagesize=999&filter=!)QmDp1jjtiQg0J)1qAulk5k1&key=${apikey}`)
.done(function (data) {
store.setItem(fullkey, JSON.stringify(data.items));
resolve(data.items);
})
.fail(reject);
});
}
function getMainNetworkSites() {
return new Promise(function (resolve, reject) {
getNetworkSites()
.then(function (items) {
resolve(items.filter(v => v.site_type == 'main_site'));
})
.catch(reject);
});
}
function getNetworkUidForUser(uid) {
return new Promise(function (resolve, reject) {
if (typeof uid === 'undefined' || uid == null) { reject(); return; }
$.get(`https://stackoverflow.com/users/${uid}?tab=profile`)
.done(function (data) {
const networkUid = Number($(data).find('.additional-links a:last, .communities:first a:last').first().attr('href').split('//')[1].match(/\/\d+\/?/)[0].replace(/\//g, '')) || null;
resolve(networkUid);
})
.fail(reject);
});
}
function getNetworkAccountsViaApi(networkUid) {
return new Promise(function (resolve, reject) {
if (typeof networkUid === 'undefined' || networkUid == null) { reject(); return; }
// via API
$.get(`${seApiUrl}/users/${networkUid}/associated?filter=!*LB1tJQ3xNMaIJ-W`)
.done(function (data) {
resolve(data.items);
})
.fail(reject);
});
}
function getNetworkAccountsForLocalUser(uid) {
return new Promise(function (resolve, reject) {
if (typeof uid === 'undefined' || uid == null) { reject(); return; }
// via mod dashboard
$.get(`https://stackoverflow.com/users/account-info/${uid}`)
.done(function (data) {
const v = $('.communities .list .row', data).map(function (i, el) {
const repText = $(el).find('.rep').text().trim();
const url = $(el).find('.site-hyperlink').attr('href');
const rep = Number(repText.replace(/\.(\d+)k/, '$100'));
return {
reputation: rep,
user_id: Number(url.match(/\/(\d+)\/$/)[0].replace(/\//g, '')),
site_url: url.match(/^https?:\/\/[^/]+/)[0],
site_name: $(el).find('.community-name').text(),
};
}).get();
resolve(v);
})
.fail(reject);
});
}
function getFlaggerAccounts(uid) {
return new Promise(function (resolve, reject) {
if (flaggeraccounts) {
resolve(flaggeraccounts);
return;
}
getNetworkAccountsForLocalUser(uid).then(function (v) {
flaggeraccounts = v;
resolve(flaggeraccounts);
});
});
}
function updateMigrationPane() {
if ($('#close-question-form').length == 0 || typeof networkSites === 'undefined') return;
const anywhere = $('#migrate-anywhere');
const container = anywhere.closest('li');
const closeSubmitBtn = $('#close-question-form .popup-submit');
const siteTargetField = $('#destinationSiteIdAC').attr('type', 'hidden');
const migflaggerStats = $(`<div id="migflagger-stats"></div>`).hide().prependTo(container);
let flaggerUid, flaggerName;
// Detect flagger and suggested site(s)
const modQueueFlags = anywhere.parents('.flagged-post-row').find('.flag-row:not(.js-cleared)');
const flags = $('.active-flag').filter((i, el) => /\b(migrated?|moved?|site)\b/i.test(el.innerText) || $(el).find('a').length != 0);
const suggestedSite = modQueueFlags.add(flags).map(function (i, el) {
const flagtext = el.innerText.toLowerCase();
const site = networkSites.filter(v => flagtext.contains(v.name.toLowerCase().replace('&', '&')) || flagtext.contains(v.site_url.replace('https://', '').replace(/\.[a-z]+/gi, '')));
return site.length > 0 ? {
elem: $(el),
name: site[0].name,
slug: site[0].api_site_parameter,
site_url: site[0].site_url
} : null;
}).get(0);
// Preload flagger's network accounts
if (flags.length > 0 && suggestedSite) {
const flaggerLink = suggestedSite.elem.siblings('a');
flaggerUid = getUserId(flaggerLink.attr('href'));
flaggerName = flaggerLink.text();
if (flaggerUid) {
getFlaggerAccounts(flaggerUid);
}
}
const siteDesc = $(`<div id="site-desc"><div>none selected</div></div>`);
const siteDropdown = $(`<select id="network-site-selector" class="js-chosen-select" data-placeholder="-- select site --"><option value="">-- select site --</option></select>`).insertAfter(siteTargetField).after(siteDesc)
.on('change', function (evt) {
if (evt.target.selectedIndex < 0) return;
const sOpt = evt.target.options[evt.target.selectedIndex];
const sValue = $(this).val();
const sUrl = (sOpt.dataset.url || "").replace('https://', '');
const sSlug = sOpt.dataset.slug;
const valid = sValue !== '';
if (valid) {
anywhere.trigger('click');
}
else {
migflaggerStats.hide();
}
anywhere.val(sUrl).attr('checked', valid).closest('li').toggleClass('action-selected', valid);
closeSubmitBtn.toggleClass('disabled-button', !valid);
const currsite = siteDesc.children().removeClass('active').eq(this.selectedIndex).addClass('active');
siteTargetField.val(sValue);
// Lazyload images
currsite.find('img').each(function (i, el) {
el.src = el.dataset.src;
});
if (!flaggerUid) return;
getFlaggerAccounts(flaggerUid).then(function (a) {
let sAccount = a.find(function (v) {
return v.site_url === sUrl || v.site_url.indexOf(sSlug) >= 0 || v.site_url.indexOf(sValue.replace(/\s/g, '').toLowerCase()) >= 0;
});
if (typeof sAccount === 'undefined') {
migflaggerStats.html(`Flagger ${flaggerName} not found on selected site.`).show();
}
else {
migflaggerStats.html(`Flagger <a href="${sAccount.site_url}/users/${sAccount.user_id}" target="_blank">${flaggerName}</a> has <span>${sAccount.reputation.toLocaleString()}</span> rep on selected site.</span>`).show();
}
});
});
let siteDescHtml = '';
let siteDropdownHtml = '';
networkSites.forEach(site => {
// Exclude current site
if (site.site_url === location.origin) return;
siteDescHtml += `
<div>
<div class="site-logos">
<img class="site-icon" data-src="${site.icon_url}" />
<div class="site-header" style="background: url('${cdn}${site.api_site_parameter}/img/bg-body.png'), url('${cdn}${site.api_site_parameter}/img/body-bg.svg'), url('${cdn}${site.api_site_parameter}/img/bg-site.png'), url('${cdn}${site.api_site_parameter}/img/bg-site.jpg');">
<img class="site-logo" data-src="${site.logo_url}" title="${site.name}" alt="${site.name}" />
</div>
</div>
Q&A for ${site.audience}<br><a href="${site.site_url}/help/on-topic" target="_blank">on-topic?</a> | <a href="${site.site_url}/users?tab=moderators" target="_blank">moderators</a>
</div>`;
siteDropdownHtml += `<option value="${site.name}" data-slug="${site.api_site_parameter}" data-url="${site.site_url}">${site.name}</option>`;
});
siteDesc.append(siteDescHtml);
siteDropdown.append(siteDropdownHtml);
// Preselect suggested site
console.log(suggestedSite);
if (suggestedSite) {
siteDropdown.val(suggestedSite.name.replace('&', '&')).triggerHandler('change');
}
$.getCachedScript('https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.jquery.min.js', function () {
$('.js-chosen-select').chosen({
allow_single_deselect: true,
no_results_text: "Oops, nothing found!",
});
});
}
// Append external stylesheet to head
addExternalStylesheet('https://cdnjs.cloudflare.com/ajax/libs/chosen/1.8.7/chosen.min.css', false);
// Append styles
addStylesheet(`
#network-site-selector {
padding: 8px 10px 7px;
cursor: pointer;
}
#site-desc {
margin-top: 10px;
}
#site-desc > div {
display: none;
padding-bottom: 10px;
}
#site-desc > div.active {
display: block;
}
#site-desc .site-logos {
display: flex;
align-items: center;
max-height: 80px;
width: calc(100% - 20px);
margin-bottom: 5px;
overflow: hidden;
background-color: var(--white);
}
#site-desc .site-icon {
max-width: 58px;
max-height: 58px;
background-color: var(--white);
}
#site-desc .site-header {
display: flex;
width: 100%;
align-items: center;
height: 80px;
padding: 11px;
background-color: var(--black-075) !important;
background-position: bottom -10px center !important;
}
#site-desc .site-logo {
max-width: 100%;
max-height: 80px;
}
#popup-close-question .action-list > li {
position: relative;
}
#migflagger-stats {
position: absolute;
top: 0;
right: 0;
padding: 5px 10px;
background: var(--black-150);
}
#migflagger-stats span {
font-weight: bold;
}
#popup-close-question #close-question-form {
overflow: initial;
}
.chosen-container {
min-width: 300px;
}
.chosen-container-single .chosen-single {
height: auto;
padding: 3px 10px;
}
.chosen-container-single .chosen-single div b {
background-position: top 5px left 0px;
}
`); // end stylesheet
// On script run
(function init() {
// Do not run if not on question page or mod queue
const cls = document.body.classList;
if (!cls.contains('question-page') && !cls.contains('mod-page')) return;
// Cache list in localstorage
getMainNetworkSites().then(v => {
networkSites = v;
networkSitenames = v.map(site => site.name);
});
// On any page update
$(document).ajaxComplete(function (event, xhr, settings) {
if (/.*\/flags\/questions\/\d+\/close\/popup\?.*/.test(settings.url)) {
updateMigrationPane();
}
});
})();