User:Chew/Scripts/PasteArchiveInfo.js

This is an old revision of this page, as edited by Chew (talk | contribs) at 23:21, 17 October 2024 (initial archive helper script). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
document.addEventListener('paste', function(event) {
    const pastedInput = event.target;

    // Ensure the paste is happening in an input element
    if (pastedInput.tagName.toLowerCase() !== 'input') {
        return;
    }

    // Get the pasted text from the clipboard
    let pastedText = (event.clipboardData || window.clipboardData).getData('text');

    // Ensure the pasted text is a "web.archive.org" link
    if (!pastedText.startsWith('https://web.archive.org/web/')) {
        console.error('Pasted text is not a web.archive.org link.');
        return;  // Exit if it's not a valid Web Archive URL
    }

    // Remove "https://web.archive.org/web/{14-digit-timestamp}/" part from the URL
    const newUrl = pastedText.replace(/https:\/\/web\.archive\.org\/web\/\d{14}\//, '');

    // Extract the 14-digit timestamp from the Web Archive URL
    const timestampMatch = pastedText.match(/https:\/\/web\.archive\.org\/web\/(\d{14})\//);
    let dateISO = "";
    
    if (timestampMatch) {
        const timestamp = timestampMatch[1];  // Get the timestamp (14 digits)
        const year = timestamp.substring(0, 4);
        const month = timestamp.substring(4, 6);
        const day = timestamp.substring(6, 8);
        
        // Create the YYYY-MM-DD date string
        dateISO = `${year}-${month}-${day}`;
    }

    // Traverse up to find the label, handling missing elements gracefully
    try {
        const pastedBox = pastedInput.parentElement
            .parentElement.parentElement.children[0].children[0];

        // Ensure 'pastedBox' exists and is a label with the correct text
        if (!pastedBox || pastedBox.tagName.toLowerCase() !== 'label' || pastedBox.innerText !== "Archive URL") {
            console.error('Label check failed or not "Archive URL".');
            return;  // Exit if the label is not "Archive URL" or traversal failed
        }

    } catch (err) {
        console.error('Error during DOM traversal for label check:', err);
        return;  // Exit if there's an error in the traversal process
    }

    // Simplify the repetitive DOM access to a single constant
    const formInputs = pastedInput.parentElement.parentElement.parentElement.parentElement;

    try {
        // Check if the URL input is empty before pasting
        const urlInput = formInputs.children[6].children[2].children[0].children[0];
        if (!urlInput.value) {
            urlInput.value = newUrl;  // Only paste the new URL if the input is empty
        }

        // Check if the date input is empty before pasting the extracted date
        const dateInput = formInputs.children[9].children[2].children[0].children[0];
        if (!dateInput.value) {
            dateInput.value = dateISO;  // Only paste the date if the input is empty
        }
    } catch (err) {
        console.error('Error traversing the DOM or setting value:', err);
    }
});