简体   繁体   中英

If page contains specific text then reload (using javascript)

If the text We are sorry but we made a boo boo appears then

  1. Wait 5 seconds
  2. reload

I would like to do this in JavaScript.

Here is an attempt

(function () {
"use strict";

function walkTheDOM(node, func) {
    if (node && node.nodeType) {
        if (typeof func === "function") {
            func(node);
        }

        node = node.firstChild;
        while (node) {
            walkTheDOM(node, func);
            node = node.nextSibling;
        }
    }
}

function filterElementsByContains(elements, string) {
    var toStringFN = {}.toString,
        text = toStringFN.call(elements),
        result,
        length,
        i,
        element;

    if (text !== "[object NodeList]" && text !== "[object Array]" && !($() instanceof jQuery)) {
        return result;
    }

    result = [];
    if (typeof string === "string") {
        string = new RegExp("^" + string + "$");
    } else if (toStringFN.call(string) !== "[object RegExp]") {
        return result;
    }

    function getText(node) {
        if (node.nodeType === 3) {
            text += node.nodeValue;
        }
    }

    length = elements.length;
    i = 0;
    while (i < length) {
        text = "";
        element = elements[i];
        walkTheDOM(element, getText);
        if (string.test(text)) {
            result.push(element);
        }

        i += 1;
    }

    return result;
}

if(filterElementsByContains([document.getElementsByTagName("table")[0]], /We are sorry but we made a boo boo/).length) {
    location.reload();
}

The above could should, I think, work for the text if it appears in a specific place. I want to make it more general - so that the text could appear anywhere on that page.

Also, I would like to know how to add a pause so that, for example, it waits 5 seconds before reloading.

I guess I would add incorporate something like:

setTimeout(
function() 
{
//location.reload();
}, 5000);

Just do an indexOf on the body's textContent / innerText property

var content = document.body.textContent || document.body.innerText;
var hasText = content.indexOf("We are sorry but we made a boo boo")!==-1;
if(hasText){
   setTimeout(function(){
       window.location = "http://www.example.com";
   },5000);
}

This may work:

var bodyText = document.body.textContent || document.body.innerText;
var msg = "We are sorry but we made a boo boo";

if (bodyText.indexOf(msg) > -1) {
    setTimeout(function() {
        location.reload();
    }, 5000);
}

--sorry for nearly duplicate answer :\\ --

--edit--

Tell me - how can I add a second rule, it's a different way of phrasing the error message: There was an internal error in our system. We logged the problem and will investigate it later.

This will check for both messages:

var bodyText = document.body.textContent || document.body.innerText;
var msg = [
    "We are sorry but we made a boo boo",
    "There was an internal error in our system. We logged the problem and will investigate it later."
];

var flag = false;

for (var i = 0; i < msg.length; i++) {
    if bodyText.indexOf(msg[i]) {
        flag = true;
    }
}

if (flag) {
    setTimeout(function() {
        location.reload();
    }, 5000);   
}

Explanation: All I did was modify msg to be an array of strings rather than a string itself. Then for every msg we want to check, we loop through the values and compare msg against bodyText . If bodyText contains one of the msg 's, we set a flag to true , and then perform an if statement on flag .

  1. If you want to check anywhere in the page... then you have to do just that. Get every DOM element and check if there is your String there... I do not think there is another way.
  2. Yes using setTimeout will do the trick for waiting before reload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM