Open main menu

Difference between revisions of "MediaWiki:Common.js"

 
(10 intermediate revisions by 2 users 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.  
 +
  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
 +
**/
  
/* https://github.com/Hitchwiki/hitchwiki/issues/23 */
+
console.log('TEST')
if ($('body.mw-special-Block').length) {  
+
 
   $('#mw-input-wpDisableEmail').click();
+
/* More sensible default settings at Special:Block User
   $('#mw-input-wpHardBlock').click();
+
  https://github.com/Hitchwiki/hitchwiki/issues/23 */
   $('#mw-input-wpExpiry')[0].value = 'infinite';
+
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';
 +
}
 +
 
 +
 
 +
/* Replace map tags with image that links to Hitchmap
 +
  https://github.com/Hitchwiki/hitchwiki/issues/214 */
 +
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);
 +
    }
 +
  }
 
}
 
}
  
/*async function createStaticMap(mapContainer, lat, lon, zoom, width, height) {const TILE_SIZE = 256; const centerX = ((lon + 180) / 360) * Math.pow(2, zoom) * TILE_SIZE; const 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; const topLeftX = centerX - width / 2, topLeftY = centerY - height / 2; const startTileX = Math.floor(topLeftX / TILE_SIZE), startTileY = Math.floor(topLeftY / TILE_SIZE); const xOffset = -(topLeftX % TILE_SIZE), yOffset = -(topLeftY % TILE_SIZE); const xTiles = Math.ceil(width / TILE_SIZE) + 1, yTiles = Math.ceil(height / TILE_SIZE) + 1; mapContainer.style.cssText = `width:${width}px;height:${height}px;position:relative;overflow:hidden`; for (let x = 0; x < xTiles; x++) {for (let y = 0; y < yTiles; y++) {const img = document.createElement("img"); img.src = `https://tile.openstreetmap.org/${zoom}/${startTileX + x}/${startTileY + y}.png`; img.style.cssText = `position:absolute;width:${TILE_SIZE}px;height:${TILE_SIZE}px;left:${(x * TILE_SIZE) + xOffset}px;top:${(y * TILE_SIZE) + yOffset}px`; mapContainer.appendChild(img);}}}
 
*/
 
 
var mapbox = document.querySelector('.infobox-map');
 
var mapbox = document.querySelector('.infobox-map');
  
 
function findFloatAttr(attr) {
 
function findFloatAttr(attr) {
    let floatRegex = /\d+(\.\d+)?/
+
  var floatRegex = /-?\d+(\.\d+)?/;
    return +mapbox.innerText.split(attr)[1].match(floatRegex)[0]
+
  var attrValue = mapbox.innerText.split(attr)[1].match(floatRegex);
 +
  return attrValue ? parseFloat(attrValue[0]) : 0;
 
}
 
}
  
Line 32: Line 72:
 
// Create an anchor element that wraps the map container
 
// Create an anchor element that wraps the map container
 
var mapLink = document.createElement("a");
 
var mapLink = document.createElement("a");
mapLink.href = `https://hitchmap.com/#location,${lat},${lon},${zoom}`;
+
mapLink.href = 'https://hitchmap.com/#location,' + lat + ',' + lon + ',' + zoom;
 
mapLink.appendChild(mapContainer);
 
mapLink.appendChild(mapContainer);
  

Latest revision as of 17:41, 24 February 2025

/* 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
**/

console.log('TEST')

/* 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';
}


/* Replace map tags with image that links to Hitchmap
   https://github.com/Hitchwiki/hitchwiki/issues/214 */
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 mapbox = document.querySelector('.infobox-map');

function findFloatAttr(attr) {
  var floatRegex = /-?\d+(\.\d+)?/;
  var attrValue = mapbox.innerText.split(attr)[1].match(floatRegex);
  return attrValue ? parseFloat(attrValue[0]) : 0;
}

// Extract the map attributes
var lat = findFloatAttr("lat");
var lon = findFloatAttr("lng");
var zoom = findFloatAttr("zoom");
var width = 300; // Set width for the static map display
var height = 300; // Set height for the static map display

// Create a container div for the static map
var mapContainer = document.createElement("div");

// Call the function to create the static map inside the container
createStaticMap(mapContainer, lat, lon, zoom, width, height);

// Create an anchor element that wraps the map container
var mapLink = document.createElement("a");
mapLink.href = 'https://hitchmap.com/#location,' + lat + ',' + lon + ',' + zoom;
mapLink.appendChild(mapContainer);

// Replace the original <map> element with the link containing the map container
mapbox.innerHTML = '';
mapbox.appendChild(mapLink);