I write Greasemonkey scripts to do all sorts of automation and screen scraping for my job. I've got a list of items/accounts to work on and I iterated through them pulling data from various web tools.
I trap the document ready event and start my processing from there, and when I'm done I'll load the next page in sequence either with a window.href
or just clicking a link to the next page.
Every now and then when the page loads Greasemonkey doesn't run. There's no errors in the consoles and if I just refresh the page it works. It's like the @include
directives are failing. I'd say the URL was changing but it's not, and a page refresh starts the script just fine (albeit with me having to stop what I'm doing and hit F5).
Anyone seen this before? Is there anything I can do about it (short of switching to Selenium)?
// ==UserScript==
// @name testG
// @version 1
// @grant none
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==
var mySearches = localStorage.getItem('mySearches');
var myResults = localStorage.getItem('myResults');
if (mySearches){
mySearches = JSON.parse(mySearches);
}else{
mySearches = ["one", "two", "three","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39"];
localStorage.setItem('mySearches', JSON.stringify(mySearches));
}
if (myResults){
myResults = JSON.parse(myResults);
}else{
myResults = [];
}
$(document).ready( function() {
var search = $("input[title='Search']");
console.log("mySearches is " + JSON.stringify(mySearches));
var mySearch = mySearches.shift();
myResults.push(mySearch);
localStorage.setItem('myResults', JSON.stringify(myResults));
search.val( mySearch );
if ( mySearches.length < 1){
localStorage.removeItem('mySearches');
alert (myResults);
}else{
myResults.push(mySearch);
localStorage.setItem('mySearches', JSON.stringify(mySearches));
window.location.replace("https://www.google.com");
}
} );//END DOCUMENT READY