MediaWiki:Common.js: Difference between revisions

From Kitesurf Wiki
Jump to navigation Jump to search
(Created page with "→‎Any JavaScript here will be loaded for all users on every page load.: // We'll wait for the DOM to be ready before attaching our logic $(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()...")
 
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* 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 (let 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>');
    }
}


// We'll wait for the DOM to be ready before attaching our logic
$(document).ready(function() {
$(document).ready(function() {
   // Check if we're on the form page; replace 'Form:YourForm' with the actual form name
   // Check if we're on the form page; replace 'Form:YourForm' with the actual form name
Line 7: Line 42:
     // Attach event to your input element; replace 'input[name="wpCityName"]' with the actual input name
     // Attach event to your input element; replace 'input[name="wpCityName"]' with the actual input name
     $('input[name="page_name"]').on('input', function() {
     $('input[name="page_name"]').on('input', function() {
       const searchValue = $(this).val();
       $('.wvres,.wvreshead').remove();
       const endpointUrl = `https://www.wikidata.org/w/api.php?action=wbsearchentities&search=${searchValue}&language=en&format=json&origin=*`;
      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
       // Perform the API request
      $.getJSON(endpointUrl, function(data) {
        // setTimeout(function() { appendResults(['hello', 'hello2'])},500);
        console.log(data);
          
         if (data.search.length > 0) {
      $.ajax({
          const firstResult = data.search.slice(0,5);
        url: endpointUrl,
          // Display the result; replace '#result' with where you'd like the result displayed
        dataType: 'json',
          $('#result').html(`Found: ${firstResult.label} (Wikidata ID: <a href="https://www.wikidata.org/wiki/${firstResult.id}">${firstResult.id}</a>)`);
        method: 'GET',
        } else {
        // headers: {
          $('#result').html('No results found');
        //  'User-Agent': 'jQuery/3.6.1 (admin@kitesurf.travel) mediawiki/1.4'
        }
        // },
      }).fail(function() {
        success: function(data) {
        $('#result').html('An error occurred');
          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=*`;
    }
});
});
})();

Revision as of 18:54, 23 September 2023

/* 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 (let 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=*`;
    }
});
})();