User:Chew/Scripts/PasteArchiveInfo.js
(Redirected from User:Chewsterchew/Scripts/PasteArchiveInfo.js)
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. |
![]() | Documentation for this user script can be added at User:Chew/Scripts/PasteArchiveInfo. |
class VisualEditorParameter {
element;
constructor(el) {
this.element = el;
}
get name() {
// get first child, its first child, and the innerText
return this.element.children[0].children[0].textContent ?? '';
}
setValue(val) {
// set value of the input element
const el = this.element.children[2].children[0].children[0];
if (el.value.toString().length === 0) {
el.value = val;
}
}
focus() {
// focus the input element
const el = this.element.children[2].children[0].children[0];
el.focus();
}
}
/**
* Finds a parameter by name in the given parameters array.
* @param {VisualEditorParameter[]} params the list of params
* @param {string} name the name of the parameter to find
* @return {VisualEditorParameter | undefined} the parameter with the given name, or null if not found
*/
function findParam(params, name) {
// find the parameter with the given name
return params.find((param) => param.name === name);
}
document.addEventListener('paste', (event) => {
const pastedInput = event.target;
if (!pastedInput) {
return;
}
// Ensure the paste is happening in an input element
if (!(pastedInput instanceof HTMLInputElement)) {
console.log('Pasted input is not an HTMLInputElement.');
return;
}
// Get the pasted text from the clipboard
const pastedText = event.clipboardData?.getData('text/plain') || '';
let date = '';
let url = '';
// Ensure the pasted text is a "web.archive.org" link
if (pastedText.startsWith('https://web.archive.org/web/')) {
url = pastedText.replace(/https:\/\/web\.archive\.org\/web\/\d{14}\//, '');
const timestampMatch = /https:\/\/web\.archive\.org\/web\/(\d{14})\//.exec(pastedText);
if (timestampMatch) {
const timestamp = timestampMatch[1]; // Get the timestamp (14 digits)
if (!timestamp) {
return;
}
const year = timestamp.slice(0, 4);
const month = timestamp.slice(4, 6);
const day = timestamp.slice(6, 8);
// Create the YYYY-MM-DD date string
date = `${year}-${month}-${day}`;
}
} else if (
pastedText.startsWith('https://archive.today/') ||
pastedText.startsWith('http://archive.today/')
) {
// must match http://archive.today/2013.08.02-201601/url_to_use
url = pastedText.replace(/https?:\/\/archive\.today\/\d{4}\.\d{2}\.\d{2}-\d{6}\//, '');
const timestampMatch = /https?:\/\/archive\.today\/(\d{4}\.\d{2}\.\d{2})-\d{6}\//.exec(
pastedText,
);
if (timestampMatch) {
date = timestampMatch[1].replaceAll('.', '-'); // Get the date (YYYY.MM.DD)
} else {
console.error('Invalid date format in archive.today URL.');
return; // Exit if the date format is invalid
}
} else {
console.error('Pasted text is not a web.archive.org nor archive.today link.');
return; // Exit if it's not a valid Web Archive URL
}
const parameters = Array.from(document.querySelectorAll('.ve-ui-mwParameterPage')).map(
(el) => new VisualEditorParameter(el),
);
// Simplify the repetitive DOM access to a single constant
const urlInput = findParam(parameters, 'URL');
const archiveUrl = findParam(parameters, 'Archive URL');
const archiveDate = findParam(parameters, 'Archive date');
if (urlInput) {
urlInput.setValue(url);
setTimeout(() => {
urlInput.focus();
}, 50);
} else {
console.error('URL parameter not found.');
}
if (archiveDate) {
archiveDate.setValue(date);
setTimeout(() => {
archiveDate.focus();
}, 100);
} else {
console.error('Archive Date parameter not found.');
}
setTimeout(() => {
archiveUrl.focus();
}, 200);
});