patternjavascriptMinor
Greasemonkey script to refresh page until target string is found
Viewed 0 times
scripttargetuntilrefreshgreasemonkeyfoundpagestring
Problem
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
// @grant metadata
// ==/UserScript==
var strHTML = document.body.innerHTML;
var yourTime = new Date();
var count = 1;
if(strHTML.indexOf("Text to find here") < 0){
alert(yourTime);
} 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]);
window.setTimeout(function(){window.location.reload() ;},300000) ;
}
return;Solution
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
Now, the
Now, I may be mistaken, but after some testing, it seems that
Where did that
This is unnecessary and is confusing. This should be removed.
The
Now, this is what your code looks like:
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
// @grant metadata
// ==/UserScript==
var FREQUENCY = 300000;
var text = document.body.innerHTMl || document.body.textContent;
var yourTime = new Date();
if (text.indexOf("Text to find here") < 0) {
alert(yourTime);
} else {
var lastCheck = document.createElement("div");
lastCheck.innerHTML = "Last Check: " + yourTime.toString();
var parent = document.getElementsByClassName("content")[0];
parent.insertBefore(lastCheck, parent.children[1]);
window.setTimeout(window.location.reload, FREQUENCY);
}Code Snippets
var text = document.body.innerText || document.body.textContent;window.setTimeout(function(){window.location.reload() ;},300000) ;// ==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
// @grant metadata
// ==/UserScript==
var FREQUENCY = 300000;
var text = document.body.innerHTMl || document.body.textContent;
var yourTime = new Date();
if (text.indexOf("Text to find here") < 0) {
alert(yourTime);
} else {
var lastCheck = document.createElement("div");
lastCheck.innerHTML = "Last Check: " + yourTime.toString();
var parent = document.getElementsByClassName("content")[0];
parent.insertBefore(lastCheck, parent.children[1]);
window.setTimeout(window.location.reload, FREQUENCY);
}Context
StackExchange Code Review Q#104864, answer score: 2
Revisions (0)
No revisions yet.