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

Contenuto cancellato Contenuto aggiunto
Nessun oggetto della modifica
rimuovo categorie
 
(15 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') === 1 /* talkpage only */) {
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) {
// getregex languageto fromextract the inputdate infrom thea textareatimestamp
var cursorPositiondateRegex = $/(e.target[0-9]{4}).textSelection-('getCaretPosition'[0-9]{2}),-([0-9]{2}).*/;
lang = $('#wpTextbox1').val().substring(cursorPosition - 2, cursorPosition);
var wiki = lang + 'wiki';
keystrokeDefaultvar monthNames = false;[
'gennaio', 'febbraio', 'marzo', 'aprile',
var page = mw.config.get( 'maggio', 'giugno', 'luglio', 'wgTitleagosto');,
'settembre', 'ottobre', 'novembre', 'dicembre'
];
$.ajax({// text for the compiled template
var text = '{{tradotto da|' + lang url:+ '//www.wikidata.org/w/api.php|',;
data: {
var 'format': 'json',index;
for (index in voiceData.query.pages) 'action': 'wbgetentities',{
var page = 'sites': mwvoiceData.configquery.get('wgDBname'),pages[index];
'titles': page,
// extract date 'props': 'sitelinks',fields
var match = 'languages': lang,dateRegex.exec(page.revisions[0].timestamp);
'maxage': mw.config.get("wgUserName") == null ? 900 : 30,
// add voice 'smaxage': mw.config.get("wgUserName") == null ? 900 : 30,title
text += page.title + 'maxlag|': 1;
},// add date as: day monthname year
dataType:text += match[3] + 'jsonp ',
success: function(data) { + monthNames[match[2] - 1] + ' '
var monthNames = + match[1] + '|';
// add 'gennaio', 'febbraio', 'marzo', 'aprile',revid
text += 'maggio', 'giugno', 'luglio', 'agosto',page.revisions[0].revid;
// close 'settembre', 'ottobre', 'novembre', 'dicembre'template
text += ]; '}}'
// insert template completion
var text = lang + inputArea.textSelection('|encapsulateSelection';, {
varperi: index;text,
replace: true
for (index in data.entities}) {;
}
var title = data.entities[index].sitelinks[wiki].title;
}
});
}
$.ajax({
/*
url: '//' + lang + '.wikipedia.org/w/api.php',
* \brief Query wikidata to get the title in the other language, then
data: {
* the source wiki to get voice's last revision 'format': 'json',data.
* @param lang Language for the source of the translation.
'action': 'query',
*/
'titles': title,
function queryWikidata(lang) {
'prop': 'revisions'
},$.ajax({
dataTypeurl: 'jsonp//www.wikidata.org/w/api.php',
successdata: function(voiceData) {
'format': 'json',
// regex to extract the date from a timestamp
'action': 'wbgetentities',
var dateRegex = /([0-9]{4})-([0-9]{2})-([0-9]{2}).*/;
'sites': mw.config.get('wgDBname'),
'titles': for (index in voiceDatamw.queryconfig.pagesget('wgTitle') {,
'props': 'sitelinks',
var page = voiceData.query.pages[index];
'languages': lang
},
// extract date fields
dataType: 'jsonp',
var match = dateRegex.exec(page.revisions[0].timestamp);
success: function(data) {
// add voice title
if text += page(!data.title + '|';success)
// add date as: day monthname yearreturn;
text += match[3] + ' '
var wiki = + monthNames[match[2] - 1]lang + ' wiki';
+ match[1] + '|';
var // add revidindex;
for (index in data.entities) text += page.revisions[0].revid;{
// close template
// the voice may not exist in the specified language text += '}}'
if (!data.entities[index].sitelinks[wiki])
// insert template completionreturn;
var inputArea = $('#wpTextbox1');
var title = data.entities[index].sitelinks[wiki].title;
inputArea.textSelection('setSelection', {start: cursorPosition - 2, end: cursorPosition});
inputArea.textSelection('encapsulateSelection', {peri: text, replace: true});
queryOtherWikipedia(lang, }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> */