Quiero verificar si el contenido de texto en el carrito de var cambia en esta página: https://www.supremenewyork.com/shop/all
let cart = document.getElementById('items-count').textContent; console.log(cart); cart.addEventListener('change', function() { console.log("cart"); window.location.replace("http://stackoverflow.com"); });me sale este error
TypeError no detectado: cart.addEventListener no es una función en Chrome-extension://ljhadajgdcijdfoopfbkjibfebkilieh/supreme.js:11:6
El script comienza cuando entro en la página.
Ya intenté monitorearlo con un ciclo while como en el siguiente código, pero luego el sitio web se congela y no puedo usarlo más hasta que cambie la var.
let carted = 0 let cart = document.getElementById('items-count').textContent; while (carted < 1 ) { console.log(cart); if (cart.length > 2) { cart ++; window.location.replace("http://stackoverflow.com"); } }Porque está tratando de agregar un detector de eventos al contenido de texto de su textContent , no a su elemento, y no puede hacerlo. solo haz esto
let cart = document.getElementById('items-count'); console.log(cart); cart.addEventListener('change', function() { console.log(`cart content has changed — ${cart.textContent}`); window.location.replace("http://stackoverflow.com"); });Si desea agregar un oyente, no puede hacerlo en textContent .
let cart = document.getElementById('items-count'); //.textContent console.log(cart); cart.addEventListener('change', function() { console.log("cart"); window.location.replace("http://stackoverflow.com"); });Si necesita ese valor, puede almacenarlo en otra variable:
var content = cart.textContentTú podrías
var item = document.getElementById('items-count'); item.addEventListener("change", DoStuff); function DoStuff(){ //do what do you want to do. }