MediaWiki:Common.js

From Kitesurf Wiki
Revision as of 22:22, 19 September 2023 by Admin (talk | contribs) (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()...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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. */

// 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() {
      const searchValue = $(this).val();
      const endpointUrl = `https://www.wikidata.org/w/api.php?action=wbsearchentities&search=${searchValue}&language=en&format=json&origin=*`;
      
      // Perform the API request
      $.getJSON(endpointUrl, function(data) {
         console.log(data);
        if (data.search.length > 0) {
          const firstResult = data.search.slice(0,5);
          // Display the result; replace '#result' with where you'd like the result displayed
          $('#result').html(`Found: ${firstResult.label} (Wikidata ID: <a href="https://www.wikidata.org/wiki/${firstResult.id}">${firstResult.id}</a>)`);
        } else {
          $('#result').html('No results found');
        }
      }).fail(function() {
        $('#result').html('An error occurred');
      });
    });
  }
});