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
 
(11 intermediate revisions by the same user not shown)
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 (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) {
          setTimeout(function() { appendResults(data[1])},500);
        }
      });
    });
};


// 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
   if (mw.config.get('wgPageName') === 'Form:Beach') {
   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() {
     var waitForEl = function(selector, callback) {
       const searchValue = $(this).val();
       if ($(selector).length) {
       const endpointUrl = `https://www.wikidata.org/w/api.php?action=wbsearchentities&search=${searchValue}&language=en&format=json&origin=*`;
        callback();
     
       } else {
      // Perform the API request
        setTimeout(function() {
      $.getJSON(endpointUrl, function(data) {
          waitForEl(selector, callback);
        console.log(data);
         }, 100);
         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>)`);
    waitForEl('input[name="page_name"]', addInputListener);
        } else {
  }
          $('#result').html('No results found');
 
        }
  if (mw.config.get('wgPageName') === 'Special:FormEdit/Beach/Cabarete') {
      }).fail(function() {
    var endpointUrl = 'https://en.wikivoyage.org/w/api.php?action=opensearch&search=' + searchValue + '&format=json&origin=*';
        $('#result').html('An error occurred');
      });
    });
   }
   }
});
});
})();

Latest revision as of 01:04, 24 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 (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) {
           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 ($(selector).length) {
        callback();
      } else {
        setTimeout(function() {
          waitForEl(selector, callback);
        }, 100);
      }
    };
    
    waitForEl('input[name="page_name"]', 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=*';
  }
});
})();