// Extract Shoptet project ID from admin page DOM. Exposed both as a
// callable function (for direct execution in page context) and as a
// source string (for injection into MAIN world via
// chrome.scripting.executeScript or webContents.executeJavaScript).
(function (root) {
  function extractShoptetIdFromDom() {
    var spans = document.querySelectorAll('.headerNavigation__item--user span');
    for (var i = 0; i < spans.length; i++) {
      var match = spans[i].textContent.match(/ID:\s*(\d+)/);
      if (match) return match[1];
    }
    var all = document.querySelectorAll('header span, header a span');
    for (var j = 0; j < all.length; j++) {
      var m = all[j].textContent.match(/ID:\s*(\d+)/);
      if (m) return m[1];
    }
    return null;
  }

  var EXTRACT_SHOPTET_ID_SRC = '(' + extractShoptetIdFromDom.toString() + ')()';

  var api = {
    extractShoptetIdFromDom: extractShoptetIdFromDom,
    EXTRACT_SHOPTET_ID_SRC: EXTRACT_SHOPTET_ID_SRC
  };
  if (typeof module !== 'undefined' && module.exports) module.exports = api;
  else { root.NP = root.NP || {}; root.NP.shoptetShopId = api; }
})(typeof self !== 'undefined' ? self : this);
