انتقل إلى المحتوى

ميدياويكي:Li-fr-Installer.js

من ويكيبيديا، الموسوعة الحرة

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات.

// <nowiki> // DO NOT REMOVE THIS LINE EVER
// Author: User:Ebraminio
(function () {
	'use strict';

	function enToken() {
		return $.ajax({
			url: '//fr.wikipedia.org/w/api.php',
			data: {
				action: 'query',
				meta: 'tokens',
				format: 'json',
				origin: location.protocol + '//' + location.hostname
			},
			xhrFields: {
				withCredentials: true
			}
		}).then(function (data) { return data.query.tokens.csrftoken; });
	}
	
	function loadEnPage(title) {
		return new mw.Api({ ajax: { url: location.protocol + '//fr.wikipedia.org/w/api.php' } }).get({
			action: 'query',
			prop: 'revisions',
			titles: title,
			rvprop: 'content',
			format: 'json',
			origin: location.protocol + '//' + location.hostname
		}).then(function (data) {
			try {
				return $.map(data.query.pages, function (value) { return value; })[0].revisions[0]['*'];
			} catch (ignore) { return ''; }
		});
	}
	
	function saveEnPage(title, token, text, summary) {
		return $.ajax({
			url: '//fr.wikipedia.org/w/api.php',
			type: 'POST',
			data: {
				action: 'edit',
				title: title,
				text: text,
				summary: summary,
				minor: '',
				token: token,
				format: 'json',
				origin: location.protocol + '//' + location.hostname
			},
			xhrFields: {
				withCredentials: true
			}
		}).then(function (data) {
			if (data.error && data.error.info) {
				mw.notify(data.error.info);
				throw data.error.info;
			}
			if (data.edit && data.edit.nochange !== undefined) {
				throw 'no change!';
			}
		}, function (data) {
			mw.notify(data);
		});
	}
	
	$.when(
		$.ready, 
		mw.loader.using('jquery.ui', 'mediawiki.util')
	).then(function () {
		if (mw.config.get('wgCanonicalSpecialPageName') !== "Blankpage" || mw.config.get('wgPageName').split('/')[1] !== "ArticleTranslator") {
			return;
		}
		if (mw.config.get('wgUserName') === null) {
			alert('عليك تسجيل الدخول أولا.');
			history.back();
			return;
		}
		$('#content').html($('<div>', {
			id: 'installermessage',
			text: 'جار العمل . . .',
			style: 'font-size: 200%; text-align: center; margin: 3em 0;'
		}));
		var userCommon = 'User:' + mw.config.get('wgUserName') + '/common.js';
		loadEnPage(userCommon).then(function (content) {
			if (content.indexOf('ArticleTranslator') !== -1) {
				$('#installermessage').html('يبدو قد تم تفعيل المترجم لك من قبل.<br><br><small>إن كنت تواجه مشكلة في التفعيل الآلي، يمكنك تجربة <a href="//ar.wikipedia.org/wiki/%D9%85%D8%B3%D8%A7%D8%B9%D8%AF%D8%A9%3A%D9%85%D8%AA%D8%B1%D8%AC%D9%85_%D8%A7%D9%84%D9%88%D8%B5%D9%84%D8%A7%D8%AA%23.D8.A7.D9.84.D8.AA.D9.81.D8.B9.D9.8A.D9.84_.D8.A7.D9.84.D9.8A.D8.AF.D9.88.D9.8A">التفعيل اليدوي</a>.</small>');
				return;
			}
			$('#installermessage').empty().append(
				$('<span>', { class: 'mw-ui-button mw-ui-primary', text: 'تنصيب ArticleTranslator!', style: 'width: auto; top: 50%;' }).click(function () {
					return enToken().then(function (token) {
						return saveEnPage(
							userCommon,
							token,
							'mw.loader.load("//en.wikipedia.org/w/index.php?title=User:Ebraminio/ArticleTranslator.js&action=raw&ctype=text/javascript");\n' +
								content
									.replace('importScript("User:Ebraminio/ArticleTranslator.js");\n', '')
									.replace('importScriptURI("//en.wikipedia.org/w/index.php?title=User:Ebraminio/ArticleTranslator.js&action=raw&ctype=text/javascript");\n', ''),
							'+[[:ar:مساعدة:مترجم الوصلات|ArticleTranslator]]'
						);
					}).then(function () {
						location.href = "//fr.wikipedia.org/wiki/" + userCommon;
					}, function (e) {
						mw.notify('لم ينجح التنصيب ' + e);
					});
				}),
				$('<div>', {
					text: 'سوف يظهر لك في أعلى منتصف كل صفحة (translate links to fa!) وبهذا ستتمكن من ترجمة الوصلات من لغة إلي أخرى.',
					style: 'padding: 2em; font-size: 50%'
				})
			);
		});
	});

}());