MediaWiki:Common.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */
(function () {
var css = '.wvres:hover{ font-weight: bold; } .wvres { cursor : pointer; padding: 6px 12px;} .wvreshead { padding: 4px 10px; } ';
var style = document.createElement('style');
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
document.getElementsByTagName('head')[0].appendChild(style);
var $createResultDiv = function (label) {
return $('<div class="wvres"><span>' + label + '</span></div>')
.click(function(){
$('input[name="page_name"]').val(label);
// $('.oo-ui-selectWidget').toggle(false);
});
};
var appendResults = function(data) {
$('.wvres,.wvreshead').remove();
if (data && data.length > 0) {
$('#ooui-1').append('<div class="wvreshead oo-ui-labelElement-label"><span style="color: gray;">Wikivoyage references:</span></div>');
for (var i = 0; i < data.length; i++ ) {
$('#ooui-1').append($createResultDiv(data[i]));
}
} else {
$('#ooui-1').append('<div class="wvres"><span>No Wikivoyage reference</span></div>');
}
}
var addInputListener = function () {
$('input[name="page_name"]').on('input', function() {
$('.wvres,.wvreshead').remove();
var searchValue = $(this).val();
var endpointUrl = 'https://en.wikivoyage.org/w/api.php?action=opensearch&search=' + searchValue + '&limit=5&format=json&origin=*';
// Perform the API request
// setTimeout(function() { appendResults(['hello', 'hello2'])},500);
$.ajax({
url: endpointUrl,
dataType: 'json',
method: 'GET',
// headers: {
// 'User-Agent': 'jQuery/3.6.1 (admin@kitesurf.travel) mediawiki/1.4'
// },
success: function(data) {
console.log(data);
setTimeout(function() { appendResults(data[1])},500);
}
});
});
};
$(document).ready(function() {
// Check if we're on the form page; replace 'Form:YourForm' with the actual form name
if (mw.config.get('wgPageName') === 'Form:Beach') {
var waitForEl = function(selector, callback) {
if ($('input[name="page_name"]').length) {
callback();
} else {
setTimeout(function() {
waitForEl(selector, addInputListener);
}, 100);
}
};
waitForEl(selector, addInputListener);
}
if (mw.config.get('wgPageName') === 'Special:FormEdit/Beach/Cabarete') {
var endpointUrl = 'https://en.wikivoyage.org/w/api.php?action=opensearch&search=' + searchValue + '&format=json&origin=*';
}
});
})();