_ _    _ _____  ___   __                       
 __      _(_) | _(_)___ / ( _ ) / /_   ___ ___  _ __ ___  
 \ \ /\ / / | |/ / | |_ \ / _ \| '_ \ / __/ _ \| '_ ` _ \ 
  \ V  V /| |   <| |___) | (_) | (_) | (_| (_) | | | | | |
   \_/\_/ |_|_|\_\_|____/ \___/ \___(_)___\___/|_| |_| |_|

Gebruiker:1Veertje/delpher.js

In dit artikel zullen we het onderwerp Gebruiker:1Veertje/delpher.js bespreken, een onderwerp dat vandaag de dag van groot belang is. Gebruiker:1Veertje/delpher.js heeft tot talloze debatten en controverses geleid, en heeft ook de interesse gewekt van experts en fans van verschillende disciplines. In deze zin is het belangrijk om alle aspecten en perspectieven die verband houden met Gebruiker:1Veertje/delpher.js in detail te analyseren, om de impact en reikwijdte ervan in de huidige samenleving volledig te begrijpen. In dit artikel zullen we verschillende gezichtspunten, relevante gegevens en mogelijke toekomstige implicaties van Gebruiker:1Veertje/delpher.js onderzoeken, met als doel een complete en verrijkende visie op dit belangrijke onderwerp te bieden.
// ==UserScript==
// @name         Delpher met Wikitext
// @supportURL   https://nl.wikipedia.orghttps://wiki386.com/nl/Gebruiker:1Veertje/Delpher_userscript
// @namespace    https://*.delpher.nl/nl/*
// @version      1.0.1
// @description  Delpher WP improvement
// @author       1Veertje
// @match        https://*.delpher.nl/nl/*
// @grant        none
// ==/UserScript==
//<noinclude><nowiki>

// Language mappings
const languages = {"Nederlands":"nl", "nl":"nl", "dut":"nl","Frans":"fr","fr":"fr", "fra":"fr", "Duits":"de", "de":"de"};
const today = new Date().toISOString().slice(0,10);
let collection = ''

// Wait for document to be ready
$(document).ready(function() {
    // Extract metadata and collection information
    const meta = JSON.parse(document.querySelector("div.js-object-viewer-wrapper").dataset.metadata);
    collection = document.querySelector("input").value;

    // Fetch data using AJAX
    $.ajax({
        dataType: "json",
        url: 'https://www.delpher.nl/nl/api/resource',
        data: {"identifier": meta.objectlevelId , "coll" : collection, "type":"dc"},
        success: handleData
    });
});

// Handle fetched data
function handleData(data) {
    const label = $( "label" )
    	.map((i, el) => el.innerText.trim()).get()
    	.filter(e => e !== 'Pagina')
        .reverse().toLowerCase();
    const meta = {};

    meta.title = data.title;
    //creator and contributor never are defined for newspaper articles so maybe skip for that collection?
    meta.creator = data.creator || '';
    meta.contributors = data.contributor || '';

    data.date = data.date.replace("-01-01 (schatting)", "").replace(" (schatting)", "").replace(" 00:00:00", "").replace("","");
    //date often has January 1st as a falsely accurate date of publication, remove?
    meta.pubdate = new Date(data.date).toISOString().slice(0,data.date.toString().length);

    // Map language if available
    if (typeof data.language === "string" && data.language in languages) {
        meta.language = languages;
    }


    const dmyPubDate = data.date.split('-').reverse().join('-');

    // Determine URL and additional metadata based on collection
    switch (collection) {
        case 'ddd': // Newspapers
            meta.url = `https://resolver.kb.nl/resolve?urn=${data.metadataKey}`;
            meta.work = data.papertitle;
            break;
        case 'dts': // Magazines
           // Set URL to the title page
            meta.url = data.titlePage;
            meta.work = '';

            // Reverse the date format found in data.date
            if (data.title.includes(dmyPubDate)) {
                meta.title = meta.title.replace(dmyPubDate, '').trim();
                if (meta.title.endsWith(',')) {
                    meta.title = meta.title.slice(0, -1);
                }

            }
            else{
                // Check if issue number exists and add it to title
                if (typeof data.issuenumber !== "undefined") {
                    if (data.issuenumber.startsWith("nr.") || data.issuenumber.startsWith("no.")) {
                        meta.title += ` ${data.issuenumber}`;
                    } else {
                        meta.title += ` nr. ${data.issuenumber}`;
                    }
                }
                if (data.volumeNumber) {
                    meta.title += ` vol. ${data.volumeNumber}`;
                }
                if (typeof data.volumeNumber !== "undefined") {
                    meta.title += ` jrg ${data.volumeNumber}`;
                } else if (typeof data.volumeYear !== "undefined") {
                    meta.title += ` jrg ${data.volumeYear}`;
                }

            }

            break;
        case 'boeken': // Books
            meta.url = data.identifier;
            if (data.subtitle){
                meta.title += `: ${data.subtitle}`;
            }
            meta.publisher = data.publisher || '';
            meta.location = data.publisherSpatial || '';
            break;
        default:
            break;
    }

    // Build references
    const citeArticle = (collection === 'boeken') ? buildCiteBookTemplate(meta) : buildCiteWebTemplate(meta);

    // Insert references into the page
    meta.url = document.querySelector(".js-share-input-i3").value;
    meta.page = meta.url.replace(/.+?0*(\d+)$/,'$1');
    meta.title = (meta.page === "1" ? "Voorpagina " : "") + data.title + (meta.page !== "1" ? ' p. '+ meta.page : "");
    let citePage = '';
    switch (collection) {
        case 'ddd': // Newspapers
            citePage = buildCiteWebTemplate(meta);
            break;
        case 'dts': // Magazines
            // Set volume information
            meta.volume = data.volumeNumber || '';
            meta.issue = data.issuenumber || data.sequenceNumber|| '';
            // Set year (deprecated?)
            meta.year = data.volumeYear || '';


            meta.magazine = data.title;
            if (data.subtitle) {
                meta.magazine += `: ${data.subtitle}`;
            }

            // Reverse the date format found in data.date
            if (data.title.includes(dmyPubDate)) {
                // If an alternative title exists without numbers, use it
                if (Array.isArray(data.alternative) && data.alternative.length > 0) {
                    data.alternative = data.alternative.filter(title => title !== data.title && title !== data.subtitle);


                    // Check if only one alternative title is left
                    if (data.alternative.length == 1){
                        meta.magazine = data.alternative;
                    } else {
                        // Sort alternative titles by the number of digits they contain
                        data.alternative.sort((a, b) => {
                            const numDigitsA = (a.match(/\d/g) || ).length;
                            const numDigitsB = (b.match(/\d/g) || ).length;
                            return numDigitsA - numDigitsB;
                        });

                        // Select the alternative title with the fewest figures
                        meta.magazine = data.alternative || '';
                    }

                    if (data.subtitle) {
                        meta.magazine += `: ${data.subtitle}`;
                    }
                } else {
                    // If no alternative title, remove the date from the current title
                    // more cleanup could be done like removing meta like issue/volume/etc
                    meta.magazine = meta.title.replace(dmyPubDate, '').trim();
                    if (meta.magazine.endsWith(',')) {
                        meta.magazine = meta.magazine.slice(0, -1);
                    }
                }
                // Placeholder title. Ideally, the user replaces this with the headline
                meta.title = (meta.page === "1" ? "Voorpagina " : "") + meta.magazine + (meta.page !== "1" ? ' p. '+ meta.page : "");
                if (!meta.volume){
                    meta.volume = data.title.includes("jrg ") ? data.title.match(/\bjrg (\d+)\b/i) : null;
                }
                if(!meta.issue){
                    meta.issue = data.title.includes("no. ") ? data.title.match(/\b(:?volg)?no\.? (\d+)\b/i) : null;
                }
            }



            citePage = buildCiteMagazineTemplate(meta);
            break;
        case 'boeken': // Books
            citePage = buildCiteBookTemplate(meta);
            break;
    }

    insertHtml("Ref pagina", citePage);
    insertHtml(`Ref ${label}`, citeArticle);
}

function splitAndFormatNames(names) {
    let result = '';
    var one_author = false;
    // Ensure meta.contributors is an array
    names = names.filter(Boolean)
    // Filter out null or undefined values
    names.forEach((name, index) => {
        if (name.includes(',')) {
            const  = name.split(", ");
            result += `|first${index + 1}= ${htmlEntities(firstName)} |last${index + 1}=${htmlEntities(lastName)} `;
        } else if (!one_author) {
            result += `|author= ${htmlEntities(name)}`;
            one_author = true;
        }
    });
    return result;
}




function buildCiteBookTemplate(meta) {
    let contributorFields = splitAndFormatNames()]);

    return `&lt;ref&#62;{{Cite book
| title = ${htmlEntities(meta.title)}
${contributorFields}
| date = ${meta.pubdate || ''}
| publisher = ${meta.publisher || ''}
| location = ${meta.location || ''}
| pages = ${meta.page || ''}
| language = ${meta.language || ''}
| url = ${meta.url}
| access-date = ${today}
}}&#60;/ref&#62;`;
}

function buildCiteMagazineTemplate(meta) {
    let contributorFields = splitAndFormatNames()]);
    //const year_field = meta.year ? `| year = ${meta.year}` : ''; //deprecated, remove?
    return `&lt;ref&#62;{{Cite magazine
| title = ${htmlEntities(meta.title)}
${contributorFields}
| date = ${meta.pubdate || ''}
| magazine = ${htmlEntities(meta.magazine) || ''}
| issue = ${meta.issue || ''}
| volume = ${meta.volume || ''}
| pages = ${meta.page || ''}
| language = ${meta.language || ''}
| url = ${meta.url}
| access-date = ${today}
}}&#60;/ref&#62;`;
}


// Build reference text
function buildCiteWebTemplate(meta) {

    return `&lt;ref&#62;{{Cite web
| title = ${htmlEntities(meta.title)}
| date = ${meta.pubdate}
| url = ${meta.url}
| work = ${htmlEntities(meta.work)}
| via = Delpher
| accessdate  = ${today}
}}&#60;/ref&#62;`;
}

// Convert characters to HTML entities
function htmlEntities(str) {
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

// Insert labels into the page
function insertHtml(label, wikitext) {
    // For mobile share
    const htmlRefPage1 = $('<div/>').html(generateMobileHtml(label, wikitext));
    $(htmlRefPage1).insertBefore($($('dl')));

    // For normal share
    const htmlRefPage2 = $('<div/>').html(generateDesktopHtml(label, wikitext));
    $(htmlRefPage2).insertBefore($($('div.object-view-menu__share-links-details')));
}

// Generate HTML for normal share label
function generateDesktopHtml(label, input) {
    const identifier = Math.random().toString(36).substring(7);
    return `<div class="object-view-menu__share-links-details hidden-input" style="display: flex;">
				<label for="${identifier}" class="object-view-menu__share-links-details-label">${label}</label>
				<div id="${identifier}" class="object-view-menu__share-links-details-link">
					<input class="object-view-menu__share-links-details-input input-field" type="text" onclick="this.setSelectionRange(0, this.value.length)"  onchange="$(this).val(\'${input}\')" value="${input}" readonly="">
				</div>
			</div>`;
}

// Generate HTML for mobile share label
function generateMobileHtml(label, input) {
    return `<label  class="metadata__details-label">${label}</label>
    		<div  class="metadata__details-input-wrapper">
			<input type="text" class="js-share-input-i5 persistent-id input-field metadata__details-input" onclick="this.setSelectionRange(0, this.value.length)"  onchange="$(this).val(\'${input}\')" value="${input}" readonly="">
		</div>`;
}
//</nowiki></noinclude>