Utente:Tino/TradottoDa.js: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Nuova pagina: /* * Inserisce il template {{tradotto da}} ricavando automaticamente * i dati da wikidata e dalla wiki di origine. La lingua viene ricavata dagli * ultimi due caratt...
 
rimuovo categorie
 
(19 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1:
/*
* InserisceCompila il template {{tradotto da}} ricavando automaticamente
* i dati da wikidata e dalla wiki di origine. LaPer lingual'uso, vieneinserire ricavatail daglitesto
* {{tradotto da|ll
* ultimi due caratteri che precedono il cursore.
* e premere lo shortcut. La lingua viene ricavata dagli ultimi due caratteri
* (ll nell'esempio) che precedono il cursore.
*
* Shortcut: Ctrl + y
Riga 8 ⟶ 10:
* @author [[Utente:Tino]]
*/
 
/* <nowiki> */
 
(function (mw, $) {
'use strict';
// configuration vars
var keyCode = 89; // y key
var alt = false;
var shift = false;
var meta = false;
var ctrl = true;
var ns = 1;
var inputArea = $('#wpTextbox1');
// flag for default browser behaviour on the shortcut
var keystrokeDefault = true;
$(function() {/*
* \brief Check whether the script should perform its action for the
$('#wpTextbox1').keydown(function(e) {
* if (!e.altKeytrigger && !eevent.shiftKey && !e.metaKey &&
* @param e Triggering event.
e.ctrlKey && e.keyCode === 89 /* "y" key */ &&
*/
mw.config.get('wgNamespaceNumber') === 2) {
function triggerCondition(e) {
return e.altKey == alt &&
e.shiftKey == shift &&
e.metaKey == meta &&
e.ctrlKey == ctrl &&
e.keyCode === keyCode &&
mw.config.get('wgNamespaceNumber') === ns;
}
/*
* \brief Query the other Wikipedia to get revision info, then add the text
* to complete the template.
* @param lang Language of the foreign wiki.
* @param title Title of the voice in the foreign wiki.
*/
function queryOtherWikipedia(lang, title) {
$.ajax({
url: '//' + lang + '.wikipedia.org/w/api.php',
data: {
'format': 'json',
'action': 'query',
'titles': title,
'prop': 'revisions'
},
dataType: 'jsonp',
success: function(voiceData) {
var// lang,regex cursorPosition;to extract the date from a timestamp
var dateRegex = /([0-9]{4})-([0-9]{2})-([0-9]{2}).*/;
cursorPositionvar monthNames = $(e.target).textSelection('getCaretPosition');[
'gennaio', 'febbraio', 'marzo', 'aprile',
lang = $('#wpTextbox1').val().substring(cursorPosition - 2, cursorPosition);
'maggio', 'giugno', 'luglio', 'agosto',
var wiki = lang +'settembre', 'ottobre', 'novembre', 'wikidicembre';
];
keystrokeDefault// =text false;for the compiled template
var text = '{{tradotto da|' + lang + '|';
var page = mw.config.get('wgTitle');
$.ajax({var index;
for (index in url: '//wwwvoiceData.wikidataquery.org/w/api.php',pages) {
data:var {page = voiceData.query.pages[index];
'format': 'json',
// extract date 'action': 'wbgetentities',fields
var match = 'sites': wiki,dateRegex.exec(page.revisions[0].timestamp);
'titles': page,
// add voice 'props': 'sitelinks',title
text += page.title + 'languages|': lang,;
// add date 'maxage'as: mw.config.get("wgUserName")day == null ? 900 :monthname 30,year
text += 'smaxage':match[3] mw.config.get("wgUserName")+ ==' null ? 900 :' 30,
'maxlag': + monthNames[match[2] - 1] + ' '
}, + match[1] + '|';
dataType:// 'jsonp',add revid
success:text function(data)+= {page.revisions[0].revid;
// close var monthNames = [template
text 'gennaio', 'febbraio', 'marzo', += 'aprile}}',
'maggio', 'giugno', 'luglio', 'agosto',
// insert template 'settembre', 'ottobre', 'novembre', 'dicembre'completion
inputArea.textSelection('encapsulateSelection', {
];
peri: text,
varreplace: text = '{{tradotto da|' + lang + '|';true
var index});
}
}
});
for (index in data.entities) {
}
var title = data.entities[index].sitelinks[wiki].title;
/*
$.ajax({
* \brief Query wikidata to get the title in the other language, then
url: '//' + lang + '.wikipedia.org/w/api.php',
* the source wiki to get voice's last revision data: {.
* @param lang Language for the source of the translation.
'format': 'json',
*/
'action': 'query',
function queryWikidata(lang) {
'titles': title,
$.ajax({
'prop': 'revisions'
url: }'//www.wikidata.org/w/api.php',
dataTypedata: 'jsonp',{
success'format': function(voiceData) {'json',
'action': 'wbgetentities',
// regex to extract the date from a timestamp
'sites': mw.config.get('wgDBname'),
var dateRegex = /([0-9]{4})-([0-9]{2})-([0-9]{2}).*/;
'titles': mw.config.get('wgTitle'),
'props': for (index in voiceData.query.pages) {'sitelinks',
'languages': lang
var page = voiceData.query.pages[index];
},
dataType: 'jsonp',
// extract date fields
success: function(data) {
var match = dateRegex.exec(page.revisions[0].timestamp);

if (!data.success)
// add voice titlereturn;
text += page.title + '|';
var wiki = lang + // add date as: day monthname year'wiki';
text += match[3] + ' '
var + monthNames[match[2] - 1] + ' 'index;
for (index in data.entities) + match[1] + '|';{
// add revid
// the voice may not exist in the specified language text += page.revisions[0].revid;
if // close template(!data.entities[index].sitelinks[wiki])
return;
text += '}}\n'
var title = data.entities[index].sitelinks[wiki].title;
// insert template at the beginning of the page
$queryOtherWikipedia('#wpTextbox1').val(textlang, + $('#wpTextbox1').val()title);
}
}
});
}
}
});
}
});
}
$(function() {
inputArea.keydown(function(e) {
// check condition to perform the action
if (!triggerCondition(e))
return;
// suppress browser's default response on shortcut keystroke
keystrokeDefault = false;
var prevText, cursor, beginning, langMatch;
// get cursor position and select "{{tradotto da|ll" on keypress
cursor = inputArea.textSelection('getCaretPosition');
prevText = inputArea.val().substring(0, cursor);
beginning = prevText.toLowerCase().lastIndexOf('{{tradotto da');
prevText = prevText.substring(beginning);
inputArea.textSelection('setSelection',
{start: beginning, end: cursor});
// get language from the input in the textarea (last 2 chars)
langMatch = prevText.match(/^{{tradotto da\|([a-z]{2})$/);
if (!langMatch || !langMatch[1])
return;
queryWikidata(langMatch[1]);
});
// prevent default browser action, when needed only
$('#wpTextbox1').keypress(function(e) {
inputArea.keypress(function(e) {
if (!e.altKey && !e.shiftKey && !e.metaKey &&
if (triggerCondition(e))
e.ctrlKey && e.keyCode === 89 /* "y" key */ ) {
return keystrokeDefault;
}
});
});
}(mediaWiki, jQuery));
 
/* </nowiki> */