مدیاویکی:Gadget-CatMaker.js: تفاوت میان نسخهها
ظاهر
imported>Yamaha5 بدون خلاصۀ ویرایش |
imported>Yamaha5 jsbeautifier |
||
| خط ۵: | خط ۵: | ||
*/ | */ | ||
function init() { | function init() { | ||
mw.loader.using(['jquery.ui.dialog'], function () { | |||
var formFind = '<div id="catmaker" title="ساخت رده به صورت خودکار"><form>'; | |||
formFind += '<p>ردهٔ انگلیسی <input type="text" id="enlink" size="20" value="Category:" /></p>'; | |||
formFind += '<p>رده به فارسی <input type="text" id="falink" size="20" value="رده:"/></p>'; | |||
formFind += '</form></div>'; | |||
var messageFind = $(formFind).appendTo('#content'); | |||
messageFind.dialog({ | |||
autoOpen: false, | |||
modal: true, | |||
width: 300, | |||
buttons: [{ | |||
id: 'catmaker-button-import', | |||
text: 'ساخت رده', | |||
click: function () { | |||
work(); | |||
} | |||
}] | |||
}); | |||
var PorpertyToolLink = mw.util.addPortletLink('p-views', '#', 'رده\u200cساز', 'ca-catmaker', 'ساخت رده به صورت خودکار'); | |||
$(PorpertyToolLink).click(show); | |||
}); | |||
} | |||
function show() { | |||
$('div#catmaker').dialog('open'); | |||
} | |||
function work() { | function work() { | ||
var enlink= document.getElementById("enlink").value; | var enlink = document.getElementById("enlink").value; | ||
var falink= document.getElementById("falink").value; | var falink = document.getElementById("falink").value; | ||
catParents(enlink).done(function (x) { | catParents(enlink).done(function (x) { | ||
$.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( | |||
falink, | |||
$.map(arguments, function (x) { | |||
return '[[' + x + ']]'; | |||
}).join('\n'), | |||
'ایجاد ردهٔ معادل به کمک ابزار رده\u200cساز', | |||
function () { | |||
mw.notify('رده به موفقیت ساخته شد. لطفاً صبر کنید.'); | |||
location.reload(); | |||
} | |||
); | |||
}); | }); | ||
}); | |||
} | } | ||
function savePage(title, text, summary, callback) { | 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) { | 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) { | |||
if (x) { | |||
return x['*']; | |||
} | |||
}); | |||
} | } | ||
function catParents(title) { | 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 () { | $(function () { | ||
init() | |||
/* | |||
'use strict'; | 'use strict'; | ||
if (mw.config.get('wgNamespaceNumber') !== 14 || ($('.noarticletext').length === 0 && new mw.Uri().query.redlink !== '1')) { | if (mw.config.get('wgNamespaceNumber') !== 14 || ($('.noarticletext').length === 0 && new mw.Uri().query.redlink !== '1')) { | ||
نسخهٔ ۲۸ مارس ۲۰۱۴، ساعت ۱۷:۱۹
/*global mw, $, prompt, location*/
/*jslint indent: 2*/
/*!
* @author User:Ebraminio, User:Yamaha5
*/
function init() {
mw.loader.using(['jquery.ui.dialog'], function () {
var formFind = '<div id="catmaker" title="ساخت رده به صورت خودکار"><form>';
formFind += '<p>ردهٔ انگلیسی <input type="text" id="enlink" size="20" value="Category:" /></p>';
formFind += '<p>رده به فارسی <input type="text" id="falink" size="20" value="رده:"/></p>';
formFind += '</form></div>';
var messageFind = $(formFind).appendTo('#content');
messageFind.dialog({
autoOpen: false,
modal: true,
width: 300,
buttons: [{
id: 'catmaker-button-import',
text: 'ساخت رده',
click: function () {
work();
}
}]
});
var PorpertyToolLink = mw.util.addPortletLink('p-views', '#', 'رده\u200cساز', 'ca-catmaker', 'ساخت رده به صورت خودکار');
$(PorpertyToolLink).click(show);
});
}
function show() {
$('div#catmaker').dialog('open');
}
function work() {
var enlink = document.getElementById("enlink").value;
var falink = document.getElementById("falink").value;
catParents(enlink).done(function (x) {
$.when.apply(null, $.map(x, function (y) {
return faWikiName(y);
})).done(function () {
savePage(
falink,
$.map(arguments, function (x) {
return '[[' + x + ']]';
}).join('\n'),
'ایجاد ردهٔ معادل به کمک ابزار رده\u200cساز',
function () {
mw.notify('رده به موفقیت ساخته شد. لطفاً صبر کنید.');
location.reload();
}
);
});
});
}
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) {
if (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 () {
init()
/*
'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('نام ردهٔ معادل انگلیسی؟');
});
*/
});