Widget: HouseTile: Unterschied zwischen den Versionen

Aus Herrenhäuser
Wechseln zu: Navigation, Suche
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
<div id="firstImageContainer">Loading image...</div>
<script>
<script>
var url = "https://wb.manorhouses.tibwiki.io/w/api.php";  
var url = "https://wb.manorhouses.tibwiki.io/w/api.php";  
Zeile 5: Zeile 7:
     action: "query",
     action: "query",
     prop: "images",
     prop: "images",
     titles: "Christinehof",
     titles: "Christinehof", // Page title whose first image you want to retrieve
     format: "json"
     format: "json"
};
};


url = url + "?origin=*";
url = url + "?origin=*";
return Object.keys(params).forEach(function(key){url += "&" + key + "=" + params[key];});
Object.keys(params).forEach(function(key) {
    url += "&" + key + "=" + params[key];
});


fetch(url)
fetch(url)
     .then(function(response){return response.json();})
     .then(function(response) {
        return response.json();
    })
     .then(function(response) {
     .then(function(response) {
         var pages = response.query.pages;
         var pages = response.query.pages;
        var firstImageTitle = null;
        // Find the first image title
         for (var page in pages) {
         for (var page in pages) {
             for (var img of pages[page].images) {
             if (pages[page].images && pages[page].images.length > 0) {
              return img.title;  
                firstImageTitle = pages[page].images[0].title;
              //console.log(img.title);
                break;  // Exit loop after getting the first image
             }
             }
        }
        // If a first image is found, display it
        if (firstImageTitle) {
            // Construct the URL to display the image
            var imageUrl = "https://wb.manorhouses.tibwiki.io/wiki/Special:FilePath/" + firstImageTitle.replace('File:', '');
            document.getElementById("firstImageContainer").innerHTML = '<img src="' + imageUrl + '" alt="First Image" style="max-width:100%;">';
        } else {
            document.getElementById("firstImageContainer").innerHTML = "No image found.";
         }
         }
     })
     })
     .catch(function(error){console.log(error);});
     .catch(function(error) {
        console.log("Error: " + error);
        document.getElementById("firstImageContainer").innerHTML = "Error retrieving image.";
    });
</script>
</script>

Version vom 22. Oktober 2024, 11:49 Uhr

Loading image...

<script> var url = "https://wb.manorhouses.tibwiki.io/w/api.php";

var params = {

   action: "query",
   prop: "images",
   titles: "Christinehof",  // Page title whose first image you want to retrieve
   format: "json"

};

url = url + "?origin=*"; Object.keys(params).forEach(function(key) {

   url += "&" + key + "=" + params[key];

});

fetch(url)

   .then(function(response) {
       return response.json();
   })
   .then(function(response) {
       var pages = response.query.pages;
       var firstImageTitle = null;
       // Find the first image title
       for (var page in pages) {
           if (pages[page].images && pages[page].images.length > 0) {
               firstImageTitle = pages[page].images[0].title;
               break;  // Exit loop after getting the first image
           }
       }
       // If a first image is found, display it
       if (firstImageTitle) {
           // Construct the URL to display the image
           var imageUrl = "https://wb.manorhouses.tibwiki.io/wiki/Special:FilePath/" + firstImageTitle.replace('File:', );
           document.getElementById("firstImageContainer").innerHTML = '<img src="' + imageUrl + '" alt="First Image" style="max-width:100%;">';
       } else {
           document.getElementById("firstImageContainer").innerHTML = "No image found.";
       }
   })
   .catch(function(error) {
       console.log("Error: " + error);
       document.getElementById("firstImageContainer").innerHTML = "Error retrieving image.";
   });

</script>