Glubbrer / BagNotification

// ==UserScript==
// @name         BagNotification
// @namespace    none
// @version      1.4
// @description  ready to use notifications for items in bag
// @author       Me
// @license      GPL-2.0-or-later
// @copyright    2020
// @include      http*://*.the-west.*/game.php*
// @include      http*://*.the-west.*.*/game.php*
// @updateURL    https://openuserjs.org/src/scripts/Glubbrer/BagNotification.meta.js
// @downloadURL  https://openuserjs.org/src/scripts/Glubbrer/BagNotification.user.js
// @grant        none
// ==/UserScript==

var listOfReusableItems = [2482000, 2485000, 2484000, 2557000, 2558000, 2665000, 2666000, 50691000, 2698000, 51482000];

async function createOrUpdateStatusField(newStatus, id) {
  if (document.getElementById('bagStatus')) {
    document.getElementById('bagStatus').innerHTML = newStatus;
    document.getElementById('bagStatus').hidden = false;
    document.getElementById('bagStatus').title = id;
  }
  else {
    var bagStatus = document.createElement('div');

    bagStatus.setAttribute('id', 'bagStatus');
    bagStatus.innerHTML = newStatus;
    bagStatus.setAttribute('style', 'position:absolute;z-index:1;text-align:center;color:#fff;font-size:16px;padding: 0px 0px 0px 0px;left:10px;top:350px;background:#222222 no-repeat scroll 0px -7px');
    bagStatus.onclick = function () {
      
      openInventory(document.getElementById('bagStatus').innerHTML)
      document.getElementById('bagStatus').innerHTML = "";
      document.getElementById('bagStatus').hidden = true
      document.getElementById('bagStatus').title = "0";
      initBagScript();
    }
    document.body.appendChild(bagStatus);
  }

}
async function openInventory(string)
{
    var a = Inventory.open();
    await new Promise(r => setTimeout(r, 750));
    a.search(string);
}

async function checkBagItems(itemId) {
  await new Promise(r => setTimeout(r, 5000));
  Bag.updateCooldowns();
  await new Promise(r => setTimeout(r, 5000));
  try {
    var timeToWait = 1000 * 60;
    var next = Bag.getItemByItemId(itemId).divMain[0].childNodes[4].firstChild.textContent;
    if (next.includes("h")) {
      timeToWait += next.split("h")[0] * 1000 * 60 * 60;
      timeToWait += next.split("h")[1].trim().split("m")[0] * 1000 * 60;
    }
    else if (next.includes("m")) {
      timeToWait += next.split("m")[0] * 1000 * 60;
      timeToWait += next.split("s")[1].trim().split("s")[0] * 1000;
    }
    await new Promise(r => setTimeout(r, timeToWait));
    checkBagItems(itemId);
    createOrUpdateStatusField(Bag.getItemByItemId(itemId).obj.name, itemId);
    var noti = new Notification(Bag.getItemByItemId(itemId).obj.name);
  }
  catch (err) {
    if (!err.message.includes("Bag.getItemByItemId(...) is undefined")) {
      createOrUpdateStatusField(Bag.getItemByItemId(itemId).obj.name, itemId);
      var a = new Notification(Bag.getItemByItemId(itemId).obj.name);
    }
  }
}
async function initBagScript() {
  await new Promise(r => setTimeout(r, 5000));
  for (var i = 0; i < listOfReusableItems.length; i++) {
    checkBagItems(listOfReusableItems[i]);
  }
}
initBagScript();