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.Revision as of 23:21, 17 October 2024 by Chew(talk | contribs)(initial archive helper script)
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
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){constpastedInput=event.target;// Ensure the paste is happening in an input elementif(pastedInput.tagName.toLowerCase()!=='input'){return;}// Get the pasted text from the clipboardletpastedText=(event.clipboardData||window.clipboardData).getData('text');// Ensure the pasted text is a "web.archive.org" linkif(!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 URLconstnewUrl=pastedText.replace(/https:\/\/web\.archive\.org\/web\/\d{14}\//,'');// Extract the 14-digit timestamp from the Web Archive URLconsttimestampMatch=pastedText.match(/https:\/\/web\.archive\.org\/web\/(\d{14})\//);letdateISO="";if(timestampMatch){consttimestamp=timestampMatch[1];// Get the timestamp (14 digits)constyear=timestamp.substring(0,4);constmonth=timestamp.substring(4,6);constday=timestamp.substring(6,8);// Create the YYYY-MM-DD date stringdateISO=`${year}-${month}-${day}`;}// Traverse up to find the label, handling missing elements gracefullytry{constpastedBox=pastedInput.parentElement.parentElement.parentElement.children[0].children[0];// Ensure 'pastedBox' exists and is a label with the correct textif(!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 constantconstformInputs=pastedInput.parentElement.parentElement.parentElement.parentElement;try{// Check if the URL input is empty before pastingconsturlInput=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 dateconstdateInput=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);}});