mic_none

User:Chew/Scripts/PasteArchiveInfo.js Source: en.wikipedia.org/wiki/User:Chew/Scripts/PasteArchiveInfo.js

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);
});