Reputation: 38
I have this code that alert if found a word, how can I make this work with multi words?
window.addEventListener('load', function() {
setTimeout(function(){
console.log("Page loaded.");
var LookFor = "HELLO"; // Text to find
var content = document.body.textContent || document.body.innerText;
var hasText = content.indexOf(LookFor) !== -1;
if(hasText) {
console.log("Text fond!.");
alert(Text fond)
} else {
console.log("Text not fond, reloading.");
location.reload();
}
},3000);
});
I mean something like:
var LookFor = "HELLO","World","Fox","Number";
I try many way but don't work, anyone can help me?
Upvotes: 0
Views: 84
Reputation: 277
You will need to create an array and do forEach like this:
window.addEventListener('load', function() {
setTimeout(function(){
console.log("Page loaded.");
var lookFor = ["HELLO","World","Fox","Number"];
let foundChange = false;
lookFor.forEach(word => {
var content = document.body.textContent || document.body.innerText;
var hasText = content.indexOf(word) !== -1;
if(hasText) {
foundChange = true;
console.log("Text fond!.");
// This will alert only found words
alert("Text found: " + word);
}
});
// Only reload if no change was found
if(!foundChange) {
console.log("Text not fond, reloading.");
location.reload();
}
},3000);
});
Upvotes: 1