مدیاویکی:Gadget-CatMaker.js: تفاوت میان نسخهها
ظاهر
imported>Yamaha5 بدون خلاصۀ ویرایش |
imported>Yamaha5 بدون خلاصۀ ویرایش |
||
| خط ۱: | خط ۱: | ||
/*global mw, $, prompt, location*/ | |||
/*jslint indent: 2*/ | |||
/*! | |||
* @author User:Ebraminio, User:Yamaha5 | |||
*/ | |||
function savePage(title, text, summary, callback) { | function savePage(title, text, summary, callback) { | ||
'use strict'; | |||
new mw.Api().post({ | new mw.Api().post({ | ||
action: 'edit', | action: 'edit', | ||
| خط ۱۹: | خط ۲۵: | ||
function faWikiName(title) { | function faWikiName(title) { | ||
'use strict'; | |||
return $.ajax({ | return $.ajax({ | ||
url: '//en.wikipedia.org/w/api.php', | url: '//en.wikipedia.org/w/api.php', | ||
data: { action: 'query', prop: 'langlinks', titles: title, redirects: 1, format: 'json', lllimit:500 }, | data: { action: 'query', prop: 'langlinks', titles: title, redirects: 1, format: 'json', lllimit: 500 }, | ||
dataType: 'jsonp' | dataType: 'jsonp' | ||
}).then(function (data) { | }).then(function (data) { | ||
| خط ۳۱: | خط ۳۸: | ||
function catParents(title) { | function catParents(title) { | ||
'use strict'; | |||
return $.ajax({ | return $.ajax({ | ||
url: '//en.wikipedia.org/w/api.php', | url: '//en.wikipedia.org/w/api.php', | ||
| خط ۴۱: | خط ۴۹: | ||
$(function () { | $(function () { | ||
if (wgNamespaceNumber !== 14 || $('.noarticletext').length === 0) { | 'use strict'; | ||
if (mw.config.get('wgNamespaceNumber') !== 14 || ($('.noarticletext').length === 0 && new mw.Uri().query.redlink !== '1')) { | |||
return; | return; | ||
} | } | ||
$(mw.util.addPortletLink('p-views', '#', ' | $(mw.util.addPortletLink('p-views', '#', 'رده\u200cساز', 'ca-catmaker')) | ||
.click(function (e) { | .click(function (e) { | ||
e.preventDefault(); | e.preventDefault(); | ||
| خط ۵۱: | خط ۶۰: | ||
$.when.apply(null, $.map(x, function (y) { return faWikiName(y); })).done(function () { | $.when.apply(null, $.map(x, function (y) { return faWikiName(y); })).done(function () { | ||
savePage( | savePage( | ||
wgPageName, | mw.config.get('wgPageName'), | ||
$.map(arguments, function (x) { return '[[' + x + ']]'; }).join('\n'), | $.map(arguments, function (x) { return '[[' + x + ']]'; }).join('\n'), | ||
'ایجاد ردهٔ معادل به کمک ابزار | 'ایجاد ردهٔ معادل به کمک ابزار رده\u200cساز', | ||
function () { | function () { | ||
mw.notify('رده به موفقیت ساخته شد. لطفاً صبر کنید.'); | |||
location.reload(); | |||
} | |||
); | ); | ||
}); | }); | ||
نسخهٔ ۲۸ مارس ۲۰۱۴، ساعت ۱۵:۵۲
/*global mw, $, prompt, location*/
/*jslint indent: 2*/
/*!
* @author User:Ebraminio, User:Yamaha5
*/
function savePage(title, text, summary, callback) {
'use strict';
new mw.Api().post({
action: 'edit',
title: title,
text: text,
summary: summary,
minor: '',
token: mw.user.tokens.get('editToken')
}).done(function (data) {
if (data.error && data.error.info) {
mw.notify(data.error.info);
} else {
callback();
}
}).fail(function (data) {
mw.notify(data);
});
}
function faWikiName(title) {
'use strict';
return $.ajax({
url: '//en.wikipedia.org/w/api.php',
data: { action: 'query', prop: 'langlinks', titles: title, redirects: 1, format: 'json', lllimit: 500 },
dataType: 'jsonp'
}).then(function (data) {
return $.grep($.map(data.query.pages, function (x) { return x; })[0].langlinks, function (x) { return x.lang === 'fa'; })[0];
}).then(function (x) {
return x['*'];
});
}
function catParents(title) {
'use strict';
return $.ajax({
url: '//en.wikipedia.org/w/api.php',
data: { action: 'query', prop: 'categories', titles: title, clshow: '!hidden', cllimit: 500, format: 'json' },
dataType: 'jsonp'
}).then(function (data) {
return $.map($.map(data.query.pages, function (x) { return x; })[0].categories, function (x) { return x.title; });
});
}
$(function () {
'use strict';
if (mw.config.get('wgNamespaceNumber') !== 14 || ($('.noarticletext').length === 0 && new mw.Uri().query.redlink !== '1')) {
return;
}
$(mw.util.addPortletLink('p-views', '#', 'رده\u200cساز', 'ca-catmaker'))
.click(function (e) {
e.preventDefault();
var title = prompt('نام ردهٔ معادل انگلیسی؟');
catParents(title).done(function (x) {
$.when.apply(null, $.map(x, function (y) { return faWikiName(y); })).done(function () {
savePage(
mw.config.get('wgPageName'),
$.map(arguments, function (x) { return '[[' + x + ']]'; }).join('\n'),
'ایجاد ردهٔ معادل به کمک ابزار رده\u200cساز',
function () {
mw.notify('رده به موفقیت ساخته شد. لطفاً صبر کنید.');
location.reload();
}
);
});
});
});
});