1
2

Amazonの商品情報をクリップボードにコピーするブックマークレット

Last updated at Posted at 2024-05-23

メルカリで出品する時にいちいち商品情報を入力するのが面倒なので、開いているAmazonのページから商品情報を取得してクリップボードにコピーするブックマークレットを作りました。

Amazonのページを開いた状態で、ブックマークレットをクリックすると、クリップボードに保存されます。
そのままメルカリなどにペーストしてください。
Apple製品同士だとMacからiPhoneにコピペできるから便利ですよね。

取得できる情報

  • 商品名
  • 現在価格
  • 商品概要

など

ブックマークレット

CodePenのページからリンクをブックマークバーにドラッグ&ドロップすれば簡単です。

See the Pen Amazonの商品情報をクリップボードにコピーするブックマークレット by Usakoro (@Int314) on CodePen.

コード

javascript: (function () {
  const getTextContent = (selector, parent = document) => {
    const element = parent.querySelector(selector);
    return element ? element.innerText.trim() : '';
  };

  const getPrice = () => {
    const selectors = [
      '#tmm-grid-swatch-HARDCOVER .slot-price',
      '#tmm-grid-swatch-PAPERBACK .slot-price',
      '#tmm-grid-swatch-OTHER .slot-price',
      '#corePriceDisplay_desktop_feature_div .a-price-whole',
      '#corePrice_desktop .a-price > .a-offscreen',
    ];
    for (const selector of selectors) {
      const price = getTextContent(selector);
      if (price) return price;
    }
    return '';
  };

  const getAuthors = (withHash = false) => {
    return Array.from(document.querySelectorAll('#bylineInfo > span > a'))
      .map(author => withHash ? `#${author.innerText.replace(/\s+/g, '')}` : author.innerText.trim())
      .join(withHash ? ' ' : ' / ');
  };

  const getProductOverview = () => {
    return Array.from(document.querySelectorAll('#productOverview_feature_div tr'))
      .map(row => {
        const header = row.querySelector('td.a-span3')?.innerText.trim() || '';
        const value = row.querySelector('td.a-span9')?.innerText.trim() || '';
        if (header && value) {
          return `${header}:${value}`;
        }
      })
      .join('\n');
  };

  const output = [
    // `URL:${window.location.href}`,
    `「${getTextContent('#productTitle')}」`,
    `Amazon価格:${getPrice()}`,
    `${getAuthors()}\n`,
    `${getProductOverview()}\n`,
    `${getTextContent('#drengr_desktopTabbedDescriptionOverviewContent_feature_div')}\n`,
    `${getTextContent('#drengr_DesktopTabbedDescriptionOverviewContent_feature_div')}\n`,
    `${getTextContent('#bookDescription_feature_div')}\n`,
    `${getTextContent('#feature-bullets ul')}\n`,
    `${getAuthors(true)}\n`,
  ].filter(line => line).join('\n').replace(/\n{2,}/g, '\n\n');

  navigator.clipboard.writeText(output).then(() => {
    alert('コピーしました!');
  }).catch(err => {
    console.error('コピーに失敗しました:', err);
  });
})();
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2