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

Contenuto cancellato Contenuto aggiunto
Nessun oggetto della modifica
rimuovo categorie
 
(14 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;
/*
* \brief Check whether the script should perform its action for the
* trigger event.
* @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) {
// regex to extract the date from a timestamp
var dateRegex = /([0-9]{4})-([0-9]{2})-([0-9]{2}).*/;
var monthNames = [
'gennaio', 'febbraio', 'marzo', 'aprile',
'maggio', 'giugno', 'luglio', 'agosto',
'settembre', 'ottobre', 'novembre', 'dicembre'
];
// text for the compiled template
var text = '{{tradotto da|' + lang + '|';
var index;
for (index in voiceData.query.pages) {
var page = voiceData.query.pages[index];
// extract date fields
var match = dateRegex.exec(page.revisions[0].timestamp);
// add voice title
text += page.title + '|';
// add date as: day monthname year
text += match[3] + ' '
+ monthNames[match[2] - 1] + ' '
+ match[1] + '|';
// add revid
text += page.revisions[0].revid;
// close template
text += '}}'
// insert template completion
inputArea.textSelection('encapsulateSelection', {
peri: text,
replace: true
});
}
}
});
}
/*
* \brief Query wikidata to get the title in the other language, then
* the source wiki to get voice's last revision data.
* @param lang Language for the source of the translation.
*/
function queryWikidata(lang) {
$.ajax({
url: '//www.wikidata.org/w/api.php',
data: {
'format': 'json',
'action': 'wbgetentities',
'sites': mw.config.get('wgDBname'),
'titles': mw.config.get('wgTitle'),
'props': 'sitelinks',
'languages': lang
},
dataType: 'jsonp',
success: function(data) {
if (!data.success)
return;
var wiki = lang + 'wiki';
var index;
for (index in data.entities) {
// the voice may not exist in the specified language
if (!data.entities[index].sitelinks[wiki])
return;
var title = data.entities[index].sitelinks[wiki].title;
queryOtherWikipedia(lang, title);
}
}
});
}
$(function() {
inputArea.keydown(function(e) {
if// (!e.altKeycheck &&condition !e.shiftKeyto &&perform !e.metaKey &&the action
if (!triggerCondition(e))
e.ctrlKey && e.keyCode === 89 /* "y" key */ &&
return;
mw.config.get('wgNamespaceNumber') === 1 /* talkpage only */) {
// get language from the inputsuppress inbrowser's thedefault textarearesponse (laston 2shortcut chars)keystroke
keystrokeDefault = false;
var cursorPosition = $(e.target).textSelection('getCaretPosition'),
lang = inputArea.val().substring(cursorPosition - 2, cursorPosition);
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 varthe wikiinput =in langthe +textarea 'wiki';(last 2 chars)
langMatch = prevText.match(/^{{tradotto da\|([a-z]{2})$/);
keystrokeDefault = false;
if (!langMatch || !langMatch[1])
return;
queryWikidata(langMatch[1]);
var page = mw.config.get('wgTitle');
// query wikidata to get the title in the other language
$.ajax({
url: '//www.wikidata.org/w/api.php',
data: {
'format': 'json',
'action': 'wbgetentities',
'sites': mw.config.get('wgDBname'),
'titles': page,
'props': 'sitelinks',
'languages': lang,
'maxage': mw.config.get("wgUserName") == null ? 900 : 30,
'smaxage': mw.config.get("wgUserName") == null ? 900 : 30,
'maxlag': 1
},
dataType: 'jsonp',
success: function(data) {
var index;
for (index in data.entities) {
var title = data.entities[index].sitelinks[wiki].title;
// query the other wikipedia to get revision info
$.ajax({
url: '//' + lang + '.wikipedia.org/w/api.php',
data: {
'format': 'json',
'action': 'query',
'titles': title,
'prop': 'revisions'
},
dataType: 'jsonp',
success: function(voiceData) {
// regex to extract the date from a timestamp
var dateRegex = /([0-9]{4})-([0-9]{2})-([0-9]{2}).*/;
var monthNames = [
'gennaio', 'febbraio', 'marzo', 'aprile',
'maggio', 'giugno', 'luglio', 'agosto',
'settembre', 'ottobre', 'novembre', 'dicembre'
];
// text to be added after the cursor position
var text = lang + '|';
for (index in voiceData.query.pages) {
var page = voiceData.query.pages[index];
// extract date fields
var match = dateRegex.exec(page.revisions[0].timestamp);
// add voice title
text += page.title + '|';
// add date as: day monthname year
text += match[3] + ' '
+ monthNames[match[2] - 1] + ' '
+ match[1] + '|';
// add revid
text += page.revisions[0].revid;
// close template
text += '}}'
// insert template completion
inputArea.textSelection('setSelection', {
start: cursorPosition - 2,
end: cursorPosition
});
inputArea.textSelection('encapsulateSelection', {
peri: text,
replace: true
});
}
}
});
}
}
});
}
});
// prevent default browser action only, when needed only
inputArea.keypress(function(e) {
if (!triggerCondition(e.altKey && !e.shiftKey && !e.metaKey && ))
e.ctrlKey && e.keyCode === 89 /* "y" key */ ) {
return keystrokeDefault;
}
});
});
}(mediaWiki, jQuery));
 
/* </nowiki> */