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:
/*
*
* i dati da wikidata e dalla wiki di origine.
* {{tradotto da|ll
* 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;
* \brief Check whether the script should perform its action for the
*
* @param e Triggering event.
*/
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 dateRegex = /([0-9]{4})-([0-9]{2})-([0-9]{2}).*/;
'gennaio', 'febbraio', 'marzo', 'aprile',
'maggio', 'giugno', 'luglio', 'agosto',
];
var text = '{{tradotto da|' + lang + '|';
for (index in
// extract date
var match =
// add voice
text += page.title + '
// add date
text +=
// close
text
// insert template
inputArea.textSelection('encapsulateSelection', {
peri: text,
}
} });
}
/*
* \brief Query wikidata to get the title in the other language, then
* the source wiki to get voice's last
* @param lang Language for the source of the translation.
*/
function queryWikidata(lang) {
$.ajax({
url:
'action': 'wbgetentities',
'sites': mw.config.get('wgDBname'),
'titles':
'props':
'languages': lang
dataType: 'jsonp',
success: function(data) {
if var wiki = lang +
var
for (index in data.entities)
// the voice may not exist in the specified language
if
return;
var 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
inputArea.keypress(function(e) {
if (triggerCondition(e))
return keystrokeDefault;
});
});
}(mediaWiki, jQuery));
/* </nowiki> */
|