# Greasemonkey script to refresh page until target string is found

I've been working on this user script to hunt for a specific string in an HTML webpage with the goal of finding the time at which it appeared. This feels needlessly complicated and I would like advice on how too condense and upgrade the code to more simply find the time.

// ==UserScript==
// @name           Magma Time Finder
// @namespace      Will.GG
// @description    Refreshes the page until target string is found
// @include        *www.website.com
// @include http://www.website.com
// ==/UserScript==
var strHTML = document.body.innerHTML;
var yourTime = new Date();
var count = 1;
if(strHTML.indexOf("Text to find here") < 0){

} else {

var lastCheck = document.createElement("div");
lastCheck.innerHTML = "Last Check: " + yourTime.toString();
console.log('Iteration: ' + count);
count++;
var parent = document.getElementsByClassName("content")[0];
parent.insertBefore(lastCheck, parent.children[1]);

}
return;


Right now, your code grabs everything HTML related in the document. However, from what I can tell, you only want to search the text on a webpage.

That being said, this code currently could potentially pick up a string that is embedded in the HTML code, but is not actually visible to the user on the screen.

To only grab the text, you can use either the innerText property of the textContent of the body:

var text = document.body.innerText || document.body.textContent;


Now, the text variable will contain all the text in the document, excluding the actual HTML code.

Now, I may be mistaken, but after some testing, it seems that count does not exist after the page resets. Therefore, you can just remove this variable and all interactions with it.

window.setTimeout(function(){window.location.reload() ;},300000) ;


Where did that 300000 come from? You should declare a constant at the top of your code.

return;


This is unnecessary and is confusing. This should be removed.

The console.logs seem to me that they were used for debugging, since they don't seem to be showing any important message to the user, and why would the user be looking in the console anyway?. These can be removed.

Now, this is what your code looks like:

// ==UserScript==
// @name           Magma Time Finder
// @namespace      Will.GG
// @description    Refreshes the page until target string is found
// @include        *www.website.com
// @include http://www.website.com
// ==/UserScript==

var FREQUENCY = 300000;

var text = document.body.innerHTMl || document.body.textContent;
var yourTime = new Date();

if (text.indexOf("Text to find here") < 0) {

• window.setTimeout(window.location.reload, FREQUENCY) will result in an error. On chrome it's: "Uncaught TypeError: Illegal invocation" – gilly3 Sep 18 '15 at 20:46