MediaWiki:Common.js: Difference between revisions

From Kitesurf Wiki
Jump to navigation Jump to search
No edit summary
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. */
console.log('hello');
(function () {
(function () {
      
      
Line 34: Line 33:
}
}


$(document).ready(function() {
var addInputListener = function () {
console.log(mw.config.get('wgPageName'));
   $('input[name="page_name"]').on('input', function() {
   // Check if we're on the form page; replace 'Form:YourForm' with the actual form name
 
  if (mw.config.get('wgPageName') === 'Form:Beach') {
console.log('hello3');
    // Attach event to your input element; replace 'input[name="wpCityName"]' with the actual input name
    $('input[name="page_name"]').on('input', function() {
console.log('hello4');
       $('.wvres,.wvreshead').remove();
       $('.wvres,.wvreshead').remove();
       var searchValue = $(this).val();
       var searchValue = $(this).val();
Line 62: Line 56:
       });
       });
     });
     });
};
$(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') {
  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=*';
    var endpointUrl = 'https://en.wikivoyage.org/w/api.php?action=opensearch&search=' + searchValue + '&format=json&origin=*';
    }
  }
});
});
})();
})();

Revision as of 00:59, 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) {
           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=*';
  }
});
})();