vincentma128 / BrickSeek Inline BarCode

// ==UserScript==
// @name         BrickSeek Inline BarCode
// @namespace    https://openuserjs.org/users/vincentma128
// @version      0.3
// @description  try to take over the world!
// @author       vincentma128
// @match        https://brickseek.com/*
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// @grant        none
// @run-at       document-end
// @license MIT
// ==/UserScript==

(function () {
  'use strict';

  let url = location.href;
  let upc;

  if (/brickseek.com\/pricing-info/.test(url)) {
    // == price-info ==
    let strongs = $(".bsapi-product-overview__meta strong");
    let upc_parent = strongs[2].parentElement;
    let raw_text = upc_parent.textContent;
    upc = raw_text.split(":")[1].trim();
    appendButton(upc_parent, upc);
  }
  else if (/brickseek.com\/.+-checker/.test(url)) {
    let strongs = $(".bsapi-product-overview__meta strong");
    let upc_parent = strongs[2].parentElement;
    if (upc_parent.childNodes.length > 3) {
      upc = upc_parent.childNodes[2].nodeValue;
      if (upc) {
        upc = upc.trim();
      }
      appendButton(upc_parent, upc);
    }
  }

  function appendButton(dom, upc) {
    if (isValidUpc(upc)) {
      $(dom).append(createButton());
    }

    $('#inlineViewBarcodeButton').click(function () {
      $('#inlineViewBarcodeButton').replaceWith(createImageTag(upc));
    });
  }

  function createButton() {
    return `<button id="inlineViewBarcodeButton" style="background-color:yellow">View BarCode</button>`;
  }

  function isValidUpc(upc) {
    return /^\d+$/.test(upc);
  }

  function createImageTag(upc) {
    return `<img referrerpolicy="no-referrer" style="width:10rem;" src="http://barcode.live/barcode.php?upc=${upc}"/>`;
  }
})();