Newer
Older
2023-ryuei / system / extractor / script.js
document.getElementById("spreadsheetUrlInput").addEventListener("blur", function() {
  var url = this.value;
  if(SpreadsheetUrl(url)) {
      document.getElementById("spreadsheetIdDisplay").value = extractSpreadsheetId(url);
      document.getElementById("spreadsheetWarning").textContent = '';
  } else {
      document.getElementById("spreadsheetWarning").textContent = '正しいスプレッドシートのURLを入力してください。';
  }
});

document.getElementById("driveUrlInput").addEventListener("blur", function() {
  var url = this.value;
  if(DriveUrl(url)) {
      document.getElementById("driveUrlDisplay").value = convertDriveUrlToImageTag(url);
      document.getElementById("driveWarning").textContent = '';
  } else {
      document.getElementById("driveWarning").textContent = '正しいドライブの共有リンクを入力してください。';
  }
});

function SpreadsheetUrl(url) {
  return /\/spreadsheets\/d\/[a-zA-Z0-9-_]+/.test(url);
}

function DriveUrl(url) {
  return /\/file\/d\/[a-zA-Z0-9-_]+/.test(url);
}

function extractSpreadsheetId(url) {
  var matches = /\/spreadsheets\/d\/([a-zA-Z0-9-_]+)/.exec(url);
  return matches ? matches[1] : '';
}

function convertDriveUrlToImageTag(url) {
  var fileId = extractFileId(url);
  return "https://lh3.google.com/u/o/d/" + fileId;
}

function extractFileId(url) {
  var matches = /\/file\/d\/([a-zA-Z0-9-_]+)/.exec(url);
  return matches ? matches[1] : '';
}

document.getElementById("copySpreadsheetId").addEventListener("click", function() {
  copyToClipboard("spreadsheetIdDisplay");
});

document.getElementById("copyDriveUrl").addEventListener("click", function() {
  copyToClipboard("driveUrlDisplay");
});

function copyToClipboard(elementId) {
  var copyText = document.getElementById(elementId).value;
  navigator.clipboard.writeText(copyText)
  .then(() => {
      console.log('Text copied to clipboard');
  })
  .catch(err => {
      console.error('Error in copying text: ', err);
  });
}