0

我编写 Greasemonkey 脚本来为我的工作进行各种自动化和屏幕抓取。我有一个要处理的项目/帐户列表,我通过它们从各种网络工具中提取数据进行了迭代。

我捕获文档就绪事件并从那里开始我的处理,当我完成后,我将按顺序加载下一页,window.href或者点击下一页的链接。

页面加载时,Greasemonkey 时不时地不运行。控制台中没有错误,如果我只是刷新页面,它就可以工作。这就像@include指令失败了。我会说 URL 正在更改,但事实并非如此,并且页面刷新启动脚本就好了(尽管我不得不停止我正在做的事情并按 F5)。

有人见过这个吗?有什么我可以做的(除了切换到 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
4

0 回答 0