﻿function shouldRunScript() {
    // Check if the current URL contains "/nyheder/"
    return window.location.href.includes('/nyheder/');
}

if (shouldRunScript()) {
    function generateArticleSchema() {
        const headlineElement = document.querySelector('h1'); // Get headline from h1 tag
        const dateElement = document.querySelector('body > article.ewii-top-header.js-ewii-top-header.ewii-top-header--simple > div > p'); // Get date from specified selector
        const descriptionElement = document.querySelector('body > article.ewii-top-header.js-ewii-top-header.ewii-top-header--simple > div > div'); // Get description from specified selector
        const imageElement = document.querySelector('body > main > article > article.ewii-content-image > picture > source:nth-child(1)'); // Get image from specified selector
        const publisherElement = document.querySelector('#organization'); // Get publisher from specified ID

        // Get the relative URL of the image from the srcset attribute
        const relativeImageUrl = imageElement ? imageElement.getAttribute('srcset') : null;

        // Concatenate the base URL with the relative URL to get the full image URL
        const fullImageUrl = relativeImageUrl ? 'https://www.ewii.dk/' + relativeImageUrl : null;

        const articleSchema = {
            "@context": "https://schema.org",
            "@type": "NewsArticle",
            "headline": headlineElement ? headlineElement.textContent.trim() : null,
            "datePublished": dateElement ? dateElement.textContent.trim() : null,
            "description": descriptionElement ? descriptionElement.textContent.trim() : null,
            "image": fullImageUrl,
            "publisher": {
                "@type": "Organization",
                "name": publisherElement ? publisherElement.textContent.trim() : "EWII",
                "url": "https://www.ewii.dk/"
            }
        };

        return {
            "@context": "https://schema.org",
            "@graph": [articleSchema]
        };
    }

    // Example usage
    const articleSchemaGraph = generateArticleSchema();

    // Convert to JSON-LD and log
    const articleJsonLd = JSON.stringify(articleSchemaGraph, null, 2);
    console.log(articleJsonLd);
}