Utente:Tino/TradottoDa.js: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
Nessun oggetto della modifica |
rimuovo categorie |
||
(6 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 10:
* @author [[Utente:Tino]]
*/
/* <nowiki> */
(function (mw, $) {
'use strict';
// configuration vars
var keyCode = 89; // y key
var shift = false;
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.keyCode === keyCode &&
}
/*
Riga 46 ⟶ 70:
];
// text
var text = '{{tradotto da|' + lang + '|';
var index;
Riga 68 ⟶ 92:
// insert template completion
inputArea.textSelection('setSelection', {▼
start: cursor - 2,▼
▲ end: cursor
▲ });
inputArea.textSelection('encapsulateSelection', {
peri: text,
Riga 99 ⟶ 119:
dataType: 'jsonp',
success: function(data) {
if (!data.success)
return;
var wiki = lang + 'wiki';
Riga 104 ⟶ 127:
var index;
for (index in data.entities) {
// the voice may not exist in the specified language
if (!data.entities[index].sitelinks[wiki])
var title = data.entities[index].sitelinks[wiki].title;
Riga 114 ⟶ 142:
$(function() {
inputArea.keydown(function(e) {
//
if (!triggerCondition(e
▲ mw.config.get('wgNamespaceNumber') === 1 /* talkpage */) {
var prevText, cursor, beginning, langMatch;
// get language from the input in the textarea (last 2 chars)▼
var lang = inputArea.val().substring(cursor - 2, cursor);▼
beginning = prevText.toLowerCase().lastIndexOf('{{tradotto da');
prevText = prevText.substring(beginning);
{start: beginning, end: cursor});
langMatch = prevText.match(/^{{tradotto da\|([a-z]{2})$/);
if (!langMatch || !langMatch[1])
return;
queryWikidata(langMatch[1]);
▲ // get cursor position on keypress
▲ cursor = inputArea.textSelection('getCaretPosition');
▲ queryWikidata(lang);
▲ }
});
// prevent default browser action, when needed only
inputArea.keypress(function(e) {
if (
▲ e.ctrlKey && e.keyCode === 89 /* "y" key */ ) {
return keystrokeDefault;
▲ }
});
});
}(mediaWiki, jQuery));
/* </nowiki> */
|