پرش به محتوا

مدیاویکی:Gadget-TranslationAssistant.js: تفاوت میان نسخه‌ها

از دانشنامه علوم انسانی
imported>Ebrahim
ایجاد ابزار جداگانه برای کمک به ترجمه
 
imported>Ebrahim
بدون خلاصۀ ویرایش
خط ۶: خط ۶:
}
}
function getEnTitle(title) {
function getTitle(page, source) {
return $.post('//tools.wmflabs.org/linkstranslator/', { from: mw.config.get('wgDBname'), to: 'enwiki', p: title }).then(function (result) {
return $.post('//tools.wmflabs.org/linkstranslator/', {
from: mw.config.get('wgDBname'),
to: source + 'wiki',
p: page
}).then(function (result) {
return Object.keys(result).map(function (x) { return result[x]; })[0];
return Object.keys(result).map(function (x) { return result[x]; })[0];
});
});
خط ۱۷: خط ۲۱:
run(enName);
run(enName);
} else {
} else {
($(mw.util.addPortletLink(
var link = $(mw.util.addPortletLink(
'p-tb',
'p-tb',
'#',
'#',
'کمک مترجم',
'کمک ترجمه',
't-translationhelper',
't-translationhelper'
'ابزار کمک مترجم'
));
)).click(function () {
$('a', link).click(function () {
getEnTitle(mw.config.get('wgPageName')).then(function (title) {
var source = mw.config.get('wgPageName');
run(title);
getTitle(mw.config.get('wgPageName'), source).then(function (title) {
run(title, $('#translationhelper').text());
});
});
}).css('font-weight', 'bold'));
}).css('font-weight', 'bold');
link.append(' ', $('<span contenteditable id="translationhelper">en</span>').keypress(function (e) {
if (e.which !== 13) return;
$(this).blur();
e.preventDefault();
}).click(function () {
document.execCommand('selectAll', false, null);
}));
}
}
function run(enName) {
function run(page, source) {
var enWikiFrame = $('<iframe>', {
var enWikiFrame = $('<iframe>', {
src: 'https://en.wikipedia.org' + mw.util.getUrl(enName) + '#tofawikiframe',
src: 'https://' + source + '.wikipedia.org' + mw.util.getUrl(page) + '#tofawikiframe',
style: 'width: 100%; height: 100%;'
style: 'width: 100%; height: 100%;'
});
});
خط ۴۴: خط ۵۷:
return window.tofawikipassenwikisource
return window.tofawikipassenwikisource
? window.tofawikipassenwikisource
? window.tofawikipassenwikisource
: getRawEnPage(enName).then(function (result) {
: getRawPage(page, source).then(function (result) {
return window.tofawikipassenwikisource = result;
return window.tofawikipassenwikisource = result;
});
});
خط ۶۴: خط ۷۷:
helperPage.insertAfter('#wpTextbox1');
helperPage.insertAfter('#wpTextbox1');
}
}
}
function getRawPage(page, source) {
return new mw.Api({ ajax: { url: '//' + source + '.wikipedia.org/w/api.php' } }).get({
action: 'query',
prop: 'revisions',
titles: page,
rvprop: 'content',
format: 'json',
origin: window.location.protocol + '//' + window.location.hostname
}).then(function (data) {
return data.query.pages && !data.query.pages[-1]
? Object.keys(data.query.pages).map(function (x) { return data.query.pages[x]; })[0].revisions[0]['*']
: '';
});
}
}
});
});

نسخهٔ ۲۷ ژوئن ۲۰۱۸، ساعت ۰۸:۵۵

$(function () {
	'use strict';
	
	if (['edit', 'submit'].indexOf(mw.config.get('wgAction')) === -1) {
		return; // only for edit mode for now
	}
	
	function getTitle(page, source) {
		return $.post('//tools.wmflabs.org/linkstranslator/', {
			from: mw.config.get('wgDBname'),
			to: source + 'wiki',
			p: page
		}).then(function (result) {
			return Object.keys(result).map(function (x) { return result[x]; })[0];
		});
	}
	
	var enName = new mw.Uri().query.enName;
	
	if (new mw.Uri().query.enName !== undefined) {
		run(enName);
	} else {
		var link = $(mw.util.addPortletLink(
			'p-tb',
			'#',
			'کمک ترجمه',
			't-translationhelper'
		));
		$('a', link).click(function () {
			var source = mw.config.get('wgPageName');
			getTitle(mw.config.get('wgPageName'), source).then(function (title) {
				run(title, $('#translationhelper').text());
			});
		}).css('font-weight', 'bold');
		link.append(' ', $('<span contenteditable id="translationhelper">en</span>').keypress(function (e) {
			if (e.which !== 13) return;
			
			$(this).blur();
			e.preventDefault();
		}).click(function () {
			document.execCommand('selectAll', false, null);
		}));
	}
	
	function run(page, source) {
		var enWikiFrame = $('<iframe>', {
			src: 'https://' + source + '.wikipedia.org' + mw.util.getUrl(page) + '#tofawikiframe',
			style: 'width: 100%; height: 100%;'
		});
		var enWikiSource = $('<textarea>', { style: 'display: none; width: 100%; height: 100%', dir: 'ltr', disabled: 'disabled' });
		var helperPage = $('<div>').append(
			$('<label>').append($('<input>', { type: 'checkbox' }).change(function () {
				if (this.checked) {
					enWikiFrame.hide();
					enWikiSource.show().val('صبر کنید…');
					$.Deferred().resolve().then(function (result) {
						return window.tofawikipassenwikisource
							? window.tofawikipassenwikisource
							: getRawPage(page, source).then(function (result) {
								return window.tofawikipassenwikisource = result;
							});
					}).then(function (result) {
						enWikiSource.val(result);
					});
				} else {
					enWikiFrame.show();
					enWikiSource.hide();
				}
			}), ' نمایش ویکی‌کد'), '<br>', enWikiFrame, enWikiSource
		);
		if (mw.config.get('tofawiki.sidebyside', true) === true) {
			$('.wikiEditor-ui .wikiEditor-ui-view').css('width', '50%');
			helperPage.attr('style', 'width: 50%; height: 500px; top: 0px; left: 0px; position: absolute;');
			helperPage.insertAfter('.wikiEditor-ui .wikiEditor-ui-view');
		} else {
			helperPage.attr('style', 'width: 100%; height: 400px;');
			helperPage.insertAfter('#wpTextbox1');
		}
	}
	
	function getRawPage(page, source) {
		return new mw.Api({ ajax: { url: '//' + source + '.wikipedia.org/w/api.php' } }).get({
			action: 'query',
			prop: 'revisions',
			titles: page,
			rvprop: 'content',
			format: 'json',
			origin: window.location.protocol + '//' + window.location.hostname
		}).then(function (data) {
			return data.query.pages && !data.query.pages[-1]
				? Object.keys(data.query.pages).map(function (x) { return data.query.pages[x]; })[0].revisions[0]['*']
				: '';
		});
	}
});