MediaWiki:Common.js

From Hitchwiki
Jump to navigation Jump to search

Note: After saving, 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: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/* Any JavaScript here will be loaded for all users on every page load. 
   This page can only be edited by users who are part of the the interface administrator group.
   See https://hitchwiki.org/en/Special:ActiveUsers?username=&groups%5B%5D=interface-admin&wpFormIdentifier=specialactiveusers
**/




/* More sensible default settings at Special:Block User
   https://github.com/Hitchwiki/hitchwiki/issues/23 */
if (document.querySelector('body.mw-special-Block')) { 
  document.querySelector('#mw-input-wpDisableEmail').click();
  document.querySelector('#mw-input-wpHardBlock').click();
  document.querySelector('#mw-input-wpExpiry').value = 'infinite';
}

/* for for iframe issue */
mw.hook && mw.hook('wikipage.content').add(function () {
  document.querySelectorAll('iframe[data-src]').forEach(function (f) {
    if (!f.src) f.src = f.dataset.src;
  });
});



/* Replace map tags with image that links to Hitchmap
   https://github.com/Hitchwiki/hitchwiki/issues/214 */
(function () {
  function initStaticMaps( $content ) {
    // $content may be a jQuery object (from mw.hook) or undefined (DOM fallback)
    var root = $content && $content.length ? $content[0] : document;
    var mapEls = root.querySelectorAll('.infobox-map');
    if (!mapEls || mapEls.length === 0) return;

    mapEls.forEach(function (mapbox) {
      if (!mapbox || mapbox.dataset.__staticMapDone) return;
      mapbox.dataset.__staticMapDone = '1';

      function findFloatAttr(attr) {
        var floatRegex = /-?\d+(\.\d+)?/;
        var txt = (mapbox.innerText || '');
        var m = txt.split(attr)[1];
        if (!m) return 0;
        var v = m.match(floatRegex);
        return v ? parseFloat(v[0]) : 0;
      }

      var lat = findFloatAttr('lat');
      var lon = findFloatAttr('lng');
      var zoom = findFloatAttr('zoom');
      var width = 300;
      var height = 300;

      function createStaticMap(mapContainer, lat, lon, zoom, width, height) {
        var TILE_SIZE = 256;
        var centerX = ((lon + 180) / 360) * Math.pow(2, zoom) * TILE_SIZE;
        var centerY = ((1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2) * Math.pow(2, zoom) * TILE_SIZE;
        var topLeftX = centerX - width / 2;
        var topLeftY = centerY - height / 2;
        var startTileX = Math.floor(topLeftX / TILE_SIZE);
        var startTileY = Math.floor(topLeftY / TILE_SIZE);
        var xOffset = -(topLeftX % TILE_SIZE);
        var yOffset = -(topLeftY % TILE_SIZE);
        var xTiles = Math.ceil(width / TILE_SIZE) + 1;
        var yTiles = Math.ceil(height / TILE_SIZE) + 1;

        mapContainer.style.width = width + 'px';
        mapContainer.style.height = height + 'px';
        mapContainer.style.position = 'relative';
        mapContainer.style.overflow = 'hidden';

        for (var x = 0; x < xTiles; x++) {
          for (var y = 0; y < yTiles; y++) {
            var img = document.createElement('img');
            img.src = 'https://tile.openstreetmap.org/' + zoom + '/' + (startTileX + x) + '/' + (startTileY + y) + '.png';
            img.style.position = 'absolute';
            img.style.width = TILE_SIZE + 'px';
            img.style.height = TILE_SIZE + 'px';
            img.style.left = (x * TILE_SIZE + xOffset) + 'px';
            img.style.top = (y * TILE_SIZE + yOffset) + 'px';
            mapContainer.appendChild(img);
          }
        }
      }

      var mapContainer = document.createElement('div');
      createStaticMap(mapContainer, lat, lon, zoom, width, height);

      var mapLink = document.createElement('a');
      mapLink.href = 'https://hitchmap.com/#location,' + lat + ',' + lon + ',' + zoom;
      mapLink.appendChild(mapContainer);

      mapbox.innerHTML = '';
      mapbox.appendChild(mapLink);
    });
  }

  // Prefer MediaWiki hook (fires on initial load and on AJAX content load)
  if (window.mw && mw.hook) {
    mw.hook('wikipage.content').add(initStaticMaps);
  }

  // DOMContentLoaded fallback for pages where mw.hook isn't available yet
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', function () { initStaticMaps(); });
  } else {
    // run once immediately in case content already present
    initStaticMaps();
  }
})();