forked from inpageedit/inpageedit-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickRedirect.js
203 lines (193 loc) · 6.67 KB
/
quickRedirect.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
var mwApi = new mw.Api()
var config = mw.config.get()
const { _analytics: _analysis } = require('./_analytics')
const { _msg } = require('./_msg')
const { $br, $progress } = require('./_elements')
const { _resolveExists } = require('./_resolveExists')
const { preference } = require('./preference')
/**
* @module quickRedirect 快速重定向模块
* @param {'from'|'to'} type
*/
var quickRedirect = function (type = 'to') {
mw.hook('InPageEdit.quickRedirect').fire()
var text = '#REDIRECT [[:$1]]',
question,
target,
json = {
action: 'edit',
createonly: 1,
minor: preference.get('editMinor'),
format: 'json',
errorformat: 'plaintext',
},
summary
if (type === 'to') {
json.title = config.wgPageName
question = _msg(
'redirect-question-to',
'<b>' + config.wgPageName.replace(/_/g, ' ') + '</b>'
)
} else if (type === 'from') {
question = _msg(
'redirect-question-from',
'<b>' + config.wgPageName.replace(/_/g, ' ') + '</b>'
)
summary = _msg('redirect-summary') + ' → [[:' + config.wgPageName + ']]'
} else {
console.error('[InPageEdit] quickRedirect only accept "from" or "to"')
return
}
ssi_modal.show({
outSideClose: false,
className: 'in-page-edit quick-redirect',
center: true,
sizeClass: 'dialog',
title: _msg('redirect-title'),
content: $('<div>').append(
$('<section>').append(
$('<span>', { html: question }),
$br,
$('<input>', { id: 'redirect-page', style: 'width:96%' }).on(
'click',
function () {
$(this).css('box-shadow', '')
}
),
...(type === 'from'
? [
$br,
$('<label>', {
for: 'redirect-fragment',
text: _msg('redirect-question-fragment'),
}),
$('<input>', { id: 'redirect-fragment', style: 'width:96%' }),
]
: []),
$br,
$('<label>', { for: 'redirect-reason', text: _msg('editSummary') }),
$('<input>', { id: 'redirect-reason', style: 'width:96%' })
),
$($progress).css('display', 'none')
),
buttons: [
{
label: _msg('confirm'),
className: 'btn btn-primary btn-single okBtn',
method: function (a, modal) {
target = $('.in-page-edit.quick-redirect #redirect-page').val()
if (
target === '' ||
target.replace(/_/g, ' ') === config.wgPageName.replace(/_/g, ' ')
) {
$('.in-page-edit.quick-redirect #redirect-page').css(
'box-shadow',
'0 0 4px #f00'
)
return
}
_analysis('quick_redirect')
if (type === 'to') {
summary = _msg('redirect-summary') + ' → [[:' + target + ']]'
json.text = text.replace('$1', target)
} else if (type === 'from') {
let fragment = $('.in-page-edit.quick-redirect #redirect-fragment')
.val()
.trim()
if (fragment && !fragment.startsWith('#')) {
fragment = `#${fragment}`
}
json.title = target
json.text = text.replace('$1', `${config.wgPageName}${fragment}`)
}
if ($('.in-page-edit.quick-redirect #redirect-reason').val() !== '') {
summary =
summary +
' (' +
$('.in-page-edit.quick-redirect #redirect-reason').val() +
')'
}
json.summary = summary
$('.in-page-edit.quick-redirect .ipe-progress').show()
$('.in-page-edit.quick-redirect section').hide()
$('.in-page-edit.quick-redirect .okBtn').attr('disabled', 'disabled')
let promise = Promise.resolve()
if (preference.get('noRedirectIfConvertedTitleExists')) {
promise = mwApi.get({ titles: json.title, converttitles: 1, formatversion: 2 }).done(data => {
const convertedTitle = data.query.pages[0]
if (convertedTitle?.missing !== true) {
failed('articleexists', { fromPage: convertedTitle.title, errors: [{
'*': _msg('notify-redirect-converted-error')
}] })
throw null
}
}).fail((errorCode, errorThrown) => {
failed(errorCode, errorThrown)
throw null
})
}
promise.then(() => {
mwApi.postWithToken('csrf', json).done(successed).fail(failed)
}, () => {})
// 重定向成功
function successed(data) {
if (data.errors) {
failed(data.errors[0].code, data)
return
}
$('.in-page-edit.quick-redirect .ipe-progress').addClass('done')
ssi_modal.notify('success', {
className: 'in-page-edit',
content: _msg('notify-redirect-success'),
title: _msg('notify-success'),
})
if (type === 'to') {
window.location.reload()
} else {
$('.in-page-edit.quick-redirect .ipe-progress').addClass('done')
setTimeout(function () {
modal.close()
}, 2000)
}
}
// 重定向失败
function failed(errorCode, errorThrown) {
$('.in-page-edit.quick-redirect .ipe-progress').hide()
$('.in-page-edit.quick-redirect section').show()
$('.in-page-edit.quick-redirect .okBtn').attr('disabled', false)
$('.in-page-edit.quick-redirect .ipe-progress').addClass('done')
ssi_modal.notify('error', {
className: 'in-page-edit',
content:
_msg('notify-redirect-error') +
'<br>' +
errorThrown.errors[0]['*'] +
' (<code>' +
errorCode +
'</code>)',
title: _msg('notify-error'),
})
// 如果是由于页面存在,给出解决方案
if (errorCode === 'articleexists') {
var fromPage, toPage
if (type === 'from') {
fromPage = errorThrown.fromPage ?? target
toPage = config.wgPageName
} else if (type === 'to') {
fromPage = errorThrown.fromPage ?? config.wgPageName
toPage = target
}
_resolveExists(fromPage, {
delete: 'Delete for redirect to [[' + toPage + ']]',
edit: 'Modify for redirect',
})
}
}
},
},
],
})
}
module.exports = {
quickRedirect,
}