User:Crowley666/js/export.js
< User:Crowley666 | js
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer / Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:按 Ctrl-F5。
- 此腳本缺少說明文檔。請協助創建。
- 相關-{zh-hans:链接; zh-hant:連接}-:根頁面 • 根頁面的子頁面 • -{zh-hans:链入; zh-hant:連入}- • -{zh-hans:重定向; zh-hant:重新導向}- • 您的對應頁面
// 见[[mw:Manual:CORS]]、[[mw:API:Cross-site requests]]
mw.loader.using('mediawiki.ForeignApi');
var pn = mw.config.get('wgPageName');
if (pn.startsWith('User:Qnm/') || // User:Qnm/9至99随便用
pn.startsWith('Template:') || pn.startsWith('Module:')) {
mw.util.addPortletLink('p-cactions', 'javascript:exportpage()', '导出页面', 'ca-export-page',
'导出页面', '', document.getElementById('ca-history')); // 快捷键设为''
mw.util.addPortletLink('p-cactions', 'javascript:exportdoc()', '导出文档', 'ca-export-doc',
'导出文档', '', document.getElementById('ca-history'));
mw.util.addPortletLink('p-cactions', 'javascript:exportboth()', '导出两者', 'ca-export-both',
'导出两者', '', document.getElementById('ca-history'));
} else if (pn == 'Wiktionary:Sandbox' || pn == 'Wiktionary:沙盒') {
mw.util.addPortletLink('p-cactions', 'javascript:exportsandbox()', '导出沙盒', 'ca-export-sandbox',
'导出沙盒', '', document.getElementById('ca-history'));
mw.util.addPortletLink('p-cactions', 'javascript:revertsandbox()', '复原沙盒', 'ca-revert-sandbox',
'复原沙盒', '', document.getElementById('ca-history'));
}
function exportpage() {
var from = pn, to = pn;
exportpagecore(from, to);
}
function exportdoc() {
var from = pn, to = pn;
if (!isdoc(from)) {
from = from + '/documentation';
to = to + '/doc';
}
if (mw.config.get('wgWikibaseItemId')) {
link = $("a.interlanguage-link-target[lang='zh']").attr('href');
if (link) {
to = decodeURI(link.split('/wiki/')[1]) + '/doc';
}
}
exportpagecore(from, to);
}
function exportboth() {
var from = pn, to = pn;
if (isdoc(from)) {
from = from.replace('/documentation', '');
from = from.replace('/doc', '');
to = from;
}
[from, to] = exportpagecore(from, to);
exportpagecore(from + '/documentation', to + '/doc');
}
function exportsandbox() {
exportpagecore('Wiktionary:Sandbox', 'Wiktionary:沙盒');
}
function revertsandbox() {
const api = new mw.ForeignApi('https://zh.wiktionary.org/w/api.php');
api.postWithToken('csrf', {
action: 'edit',
title: 'Wiktionary:沙盒',
text: '{{sandbox}}',
summary: '复原沙盒'
}).fail(function(){ mw.notify('复原失败'); }).done(function(){ mw.notify('复原成功'); });
}
// 与import.js接口保持一致
function decapital(title) {
return title;
}
function exportpagecore(from, to) {
if (from.endsWith('/doc')) from = from.replace('/doc', '/documentation');
if (to.endsWith('/documentation')) to = to.replace('/documentation', '/doc');
from = decapital(from); // do nothing
if (mw.config.get('wgWikibaseItemId')) {
link = $("a.interlanguage-link-target[lang='zh']").attr('href');
if (link) {
to = decodeURI(link.split('/wiki/')[1]) + (isdoc(from) ? '/doc' : '');
}
}
var apifrom = new mw.Api();
apifrom.get({
action: 'query',
titles: from,
prop: 'revisions|langlinks',
indexpageids: 1,
rvprop: 'content',
lllang: 'zh'
}).then(function(result) {
result = result.query;
if (result.pageids[0] === "-1") {
mw.notify('页面不存在');
return;
}
var rpage = result.pages[result.pageids[0]];
if (rpage.langlinks) {
to = rpage.langlinks[0]['*'];
}
var text = rpage.revisions[0]['*'];
var summary = '[[User:EdwardAlexanderCrowley/js/import.js|搬运]]自[[:en:' + from + ']]';
if (from == 'Wiktionary:Sandbox') text = '{{sandbox}}\n' + text;
const apito = new mw.ForeignApi('https://zh.wiktionary.org/w/api.php');
return apito.postWithToken('csrf', {
action: 'edit',
title: to,
text: text,
summary: summary,
watchlist: 'nochange'
}).fail(function() {
mw.notify('搬运失败');
}).then(function() {
mw.notify('搬运成功');
if (!rpage.langlinks && canlink(from)) {
const apidata = new mw.ForeignApi('https://www.wikidata.org/w/api.php');
apidata.postWithToken('csrf', {
action: 'wblinktitles',
fromsite: 'enwiktionary',
fromtitle: from,
tosite: 'zhwiktionary',
totitle: to,
bot: 1
}).fail(function(result) {
var msg = { "no-external-page": "页面为重定向", "failed-save": "链接失败,可能需要IPBE"};
if (msg[result]) {
mw.notify(msg[result]);
} else {
mw.notify("未知错误:" + result);
}
}).done(function() {
mw.notify('链接成功');
});
}
});
});
return [from, to];
}
function canlink(p) {
return !(p.endsWith('/documentation') || p.endsWith('/doc')
|| p.endsWith('/testcase') || p.endsWith('/testcases')
|| p.startsWith('User:') || p.startsWith('Module:User:')
|| p.endsWith('.css'));
}
function isdoc(p) {
return p.endsWith('/documentation') || p.endsWith('/doc');
}