ผู้ใช้:Crowley666/js/export.js

จาก วิกิพจนานุกรม พจนานุกรมเสรี

หมายเหตุ: หลังเผยแพร่ คุณอาจต้องล้างแคชเว็บเบราว์เซอร์ของคุณเพื่อดูการเปลี่ยนแปลง

  • ไฟร์ฟอกซ์ / ซาฟารี: กด Shift ค้างขณะคลิก Reload หรือกด Ctrl-F5 หรือ Ctrl-R (⌘-R บนแมค)
  • กูเกิล โครม: กด Ctrl-Shift-R (⌘-Shift-R บนแมค)
  • อินเทอร์เน็ตเอกซ์พลอเรอร์ และ Edge: กด Ctrl ค้างขณะคลิก Refresh หรือกด Ctrl-F5
  • โอเปร่า: กด Ctrl-F5
// 见[[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') {
	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 (!from.endsWith('/documentation')) {
		from = from + '/documentation';
		to = to + '/documentation';
	}
	if (mw.config.get('wgWikibaseItemId')) {
		link = $("a.interlanguage-link-target[lang='th']").attr('href'); 
		if (link) {
			to = decodeURI(link.split('/wiki/')[1]) + '/documentation';
		}
	}
	exportpagecore(from, to);
}

function exportboth() {
	var from = pn, to = pn;
	if (from.endsWith('/documentation')) {
		from = from.replace('/documentation', '');
		to = to.replace('/documentation', '');
	}
	[from, to] = exportpagecore(from, to);
	exportpagecore(from + '/documentation', to + '/documentation');
}

function exportsandbox() {
	exportpagecore('Wiktionary:Sandbox', 'Wiktionary:Sandbox');
}

function revertsandbox() {
	const api = new mw.ForeignApi('https://th.wiktionary.org/w/api.php');
	api.postWithToken('csrf', {
		action: 'edit',
		title: 'Wiktionary:Sandbox',
		text: '{{sandbox}}',
		summary: '复原沙盒'
	}).fail(function(){ mw.notify('复原失败'); }).done(function(){ mw.notify('复原成功'); });
}

function exportpagecore(from, to) {
	if (mw.config.get('wgWikibaseItemId')) {
		link = $("a.interlanguage-link-target[lang='th']").attr('href'); 
		if (link) {
			to = decodeURI(link.split('/wiki/')[1]);
		}
	}
	var apifrom = new mw.Api();
	apifrom.get({
		action: 'query',
		titles: from,
		prop: 'revisions|langlinks',
		indexpageids: 1,
		rvprop: 'content',
		lllang: 'th'
	}).then(function(result) {
		result = result.query;
		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://th.wiktionary.org/w/api.php');
		return apito.postWithToken('csrf', {
			action: 'edit',
			title: to,
			text: text,
			summary: summary,
		}).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: 'thwiktionary',
					totitle: to,
					bot: 1
				}).fail(function() {
					mw.notify('链接失败,可能需要IPBE');
				}).done(function() {
					mw.notify('链接成功');
				});
			}
		});
	});
	return [from, to];
}

function canlink(p) {
	return !(p.endsWith('/documentation') || p.endsWith('/documentation')
		|| p.endsWith('/testcase') || p.endsWith('/testcases')
		|| p.startsWith('User:') ||p.startsWith('Module:User:'));
}