DigiKey: Export cart (possibly to BOM)

Want to save your cart or import it to BOM? This will make a tab-delimited version of your cart.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        DigiKey: Export cart (possibly to BOM)
// @namespace   giferrari.net
// @description Want to save your cart or import it to BOM? This will make a tab-delimited version of your cart.
// @include     http://www.digikey.com/classic/Ordering/AddPart.aspx*
// @version     1
// @grant       none
// ==/UserScript==

function exportCartAsText() {
  /*
  BOM format: (one per line):
  Quantity delimiter Part Number delimiter Customer Reference (optional) where delimiter is a comma or tab.
  Example: 5,P4525-ND,ABC123
  */
  var lines = [];
  $('#ctl00_ctl00_mainContentPlaceHolder_mainContentPlaceHolder_ordOrderDetails tr.detail').each(function(i, tr) {
    var datums = $(tr).find('td');
    var quantity = $(datums[1]).find('input').val();
    var pn = $(datums[3]).text();
    var cref = $(datums[5]).find('input').val().replace(/,/g, ';'); // Text import tool doesn't support commas, bahhh
    lines.push(quantity + '\t' + pn + '\t' + cref);
  });
  
  return lines.join('\n');
}

$('#btnFinishOrder').before(
  $('<input>', { class: 'button', value: 'Script: Export as text' }).click(function() {
    $(this).remove();
    var cartText = exportCartAsText();
    var $content = $('<div>').append(
      $('<p>Exported cart (you can paste this into the <a target="_blank" href="https://www.digikey.com/Classic/Registereduser/TextFileImport.aspx?bom=y&ppp=10">BOM import tool</a>):</p>').css('font-weight', 'bold'),
      $('<pre>').text(cartText)
    ).
    css('background', '#AAA').
    css('padding', '0.5em 1em').
    slideDown();

    $('#pnlAddManually').before($content)
  })
);