Utente:Tino/TradottoDa.js: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
Riga 17:
// flag for default browser behaviour on the shortcut
var keystrokeDefault = true;
/*
* \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 to be added after the cursor position
var text = 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('setSelection', {
start: cursor - 2,
end: cursor
});
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) {
var wiki = lang + 'wiki';
var index;
for (index in data.entities) {
var title = data.entities[index].sitelinks[wiki].title;
queryOtherWikipedia(lang, title);
}
}
});
}
$(function() {
Riga 23 ⟶ 116:
if (!e.altKey && !e.shiftKey && !e.metaKey &&
e.ctrlKey && e.keyCode === 89 /* "y" key */ &&
mw.config.get('wgNamespaceNumber') === 1 /* talkpage
// suppress browser's default response on shortcut keystroke
Riga 29 ⟶ 122:
// get language from the input in the textarea (last 2 chars)
var
lang = inputArea.val().substring(
}
});
|