Benutzer:Dapete/scaleImages.js

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
 function scaleImages() {
   // Scale factor, default: 1.5
   if (!scaleImageFactor) {
     scaleImageFactor = 1.5;
   }
   // Other settings:
   // * scaleImageThumb - scale thumbnail images
   // * scaleImageGallery - scale gallery images
   // * scaleImageOther - scale all images, overrides others
   if (scaleImageOther) {
     scaleImageThumb = true;
     scaleImageGallery = true;
   }
        
   // Get all images on upload.wikimedia.org that are scaled images, and rescale them
   var imgs = document.getElementsByTagName("img");
   var imgPxRE = /\/\d+(px-[^\/]*)$/;
   
   for (var i=0; i<imgs.length; i++) {
     var img = imgs[i];
     if (img.src.substr(0,28) == "http://upload.wikimedia.org/" && imgPxRE.test(img.src)) {
       if (
         scaleImageOther || 
         (scaleImageThumb && img.parentNode.parentNode.className == "thumbinner") || 
         (scaleImageGallery && img.parentNode.parentNode.className == "thumb")
       ) {
         var oldsrc = img.src;
         var oldwidth = img.width;
         var oldheight = img.height;
         img.width = Math.ceil(oldwidth * scaleImageFactor / 10) * 10;
         img.height = Math.ceil(oldheight * scaleImageFactor / 10) * 10;
         
         imgPxRE.exec(img.src);
         pxetc = "/" + img.width + RegExp.$1;
         img.src = oldsrc.replace(imgPxRE, pxetc);
       }
     }
   }

   // Get all gallerybox and thumb <div> tags - these need to be scaled, too
   var divs = document.getElementsByTagName("div");
   var pxRE = /(\d+)px/;
   for (var i=0; i<divs.length; i++) {
     var div = divs[i];
     // If asked to only change thumb'ed images, only do this for
     // "thumbinner" DIVs (these are used with normal thumbs). Otherwise
     // also do this for "thumb" DIVS (these are also used by thumbs, but
     // only in galleries do they have a width set.
     if (
       (scaleImageThumb && div.className == "thumbinner") ||
       (scaleImageGallery && div.className == "thumb")       
     ) {
       // Only work on DIVs that have a width set. This filters out "thumb"
       // DIVs outside galleries.
       if (pxRE.test(div.style.width)) {
         pxRE.exec(div.style.width);
         var oldwidth = RegExp.$1;
         var width = Math.ceil((oldwidth-2) * scaleImageFactor / 10) * 10;
         div.style.width = (width+2).toString() + "px";
         
         // Now comes a tricky part: MediaWiki centers the images in a
         // gallery by applying a padding at the top and bottom. So check if
         // we are in a gallery and do a few more things (including: making
         // the gallerybox bigger)
         
         divGallery = div.parentNode;
         if (divGallery.className == "gallerybox") {
           // First, scale the gallerybox like the thumb div
           divGallery.style.width = width.toString() + "px";
           
           // Get the image
           // The div contains a link...
           var a = div.firstChild;
           if (a && a.tagName == "A") {
             // Which contains the image...
             var img = a.firstChild;
             if (img && img.tagName == "IMG") {
               // Padding is half the difference between image height and
               // scaled gallery height (which is the same as the width)
               var padding = Math.ceil((width - img.height) / 2);
               div.style.padding = padding.toString() + "px 0pt";
             }
           }
         }
       }
     }
   }
 } 
 addOnloadHook (scaleImages);