Javascript注入将商品添加到购物篮

时间:2018-11-17 17:46:15

标签: javascript html node.js json

我正在尝试运行此脚本,该脚本有效地通过了一个json列表,该列表包含url和要转到链接集的大小,请根据大小将商品添加到购物车中,检查它是否有库存或已添加。我已经知道链接和大小搜索算法可以独立工作,但是它们似乎无法正常工作,我不知道为什么吗?

var foundall = false;
var copItems = [{
  "url": "https://www.supremenewyork.com/mobile/#products/303518/22745",
  "size": "Medium"
}];
for (var i = 0; i < copItems.length; i++) {
  AddToCart(copItems[i], function(carted) {
    console.log(carted);
  });
}

function AddToCart(item, callback) {
  location.href = item.url;
  var counter = 0;
  var waitToAppear = setInterval(function() {
    if (document.querySelector('#cart-update > span')) {
      if (document.querySelector('#cart-update > span').innerHTML == 'remove') {
        return callback("failed");
        clearInterval(waitToAppear);
      } else if (document.querySelector('#cart-update > span').innerHTML == 'sold out') {
        copSelectSize(size, function(data) {
          return callback(data);
          clearInterval(waitToAppear);
        });
      } else if (document.querySelector('#cart-update > span').innerHTML == 'add to basket') {
        copSelectSize(item.size, function(Sized) {
          return callback("failed");
          clearInterval(waitToAppear);
        })
      } else {
        counter += 1;
        if (counter == 5) {
          return callback("failed");
          clearInterval(waitToAppear);
        }
      }
    }
  }, 100);
}

function copSelectSize(size, callback) {
  var counter = 0;
  var checkExist = setInterval(function() {
    if (document.getElementById('size-options').length) {
      var sizes = document.getElementById('size-options').options;
      var size_id;
      for (var i = 0; i < sizes.length; i++) {
        if (sizes[i].innerText == '\(Size)') {
          size_id = i;
          document.getElementById('size-options').selectedIndex = size_id;
          document.getElementById('size-options-link').innerHTML = '\(Size)';
          if (document.querySelector('#cart-update > span').innerHTML != 'remove') {
            document.querySelector('#cart-update > span').click();
            return callback("success");
            clearInterval(checkExist);
          }
          var checkExista = setInterval(function() {
            if (document.querySelector('#cart-update > span').innerHTML == 'remove') {
              checkExista = '';
            }
            clearInterval(checkExista);
          }, 100);
          break;
        }
      }
    }
    counter += 1;
    if (counter == 5) {
      return callback("failed");
      clearInterval(checkExist);
    }
  }, 200);
}

0 个答案:

没有答案