MediaWiki:Common.js

From Kitesurf Wiki
Revision as of 00:46, 24 September 2023 by Admin (talk | contribs)
Jump to navigation Jump to search

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. */
console.log('hello');
(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>');
    }
}

$(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') {
    // Attach event to your input element; replace 'input[name="wpCityName"]' with the actual input name
    $('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);
         }
       });
    });
  }

    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=*';
    }
});
})();