在 Tampermonkey 中使用 javascript 进行页面搜索和文本框自动填充
Posted
技术标签:
【中文标题】在 Tampermonkey 中使用 javascript 进行页面搜索和文本框自动填充【英文标题】:Page search and textbox autofill with javascript in Tampermonkey 【发布时间】:2015-12-21 14:50:57 【问题描述】:我正在尝试在 Grease/Tampermonkey 中编写一个脚本,该脚本将在网页中搜索问题,然后在文本框中填写正确答案。
这是我目前想出的:
//There are 12 options in each array
var questions = ["fourth letter", "grass", "6x4", "NFL team", "OSU Buckeys", "35 + 15", "Yellow + Blue", "LeBron James", "Lassie", "2x9", "9x10"];
var answers = ["s", "green", "nut", 24, "Bengals", "gray", 50, "green", 23, "dog", 18, 90];
var answer = 0;
var position = 0;
var found = false;
//When the window loads, the function will trigger the While loop, which will run as long as found is false.
//The While loop triggers the For loop, which runs until the If statement has found the question.
//The For loop will run a number of times equal to the number of variables in the questions array.
//The if statement searches the webpage for each consecutive variable in the questions array until it finds a match.
//When the if statement finds a match, it notes which variable in the questions array matched, and sets found to true which should trigger the end of the while loop.
window.onload = function ()
while (found)
for (var i = 0;i<=questions.length;i++)
if (document.body.innerhtml.toString().indexOf(questions[i])) > -1
position = i;
found = true;
else
console.log ("No answer was found");
;
;
;
;
//Once the While loop has completed, this fills the textbox on the webpage with the answer
document.getElementById("humanverify\[input\]").innerHTML = answers[position];
Tampermonkey 表示代码正在运行。但是,文本框没有填充。我是 javascript 新手,上周一直在拼凑一些代码和知识。
【问题讨论】:
【参考方案1】:found 最初为 false,因此 while 循环永远不会执行。
试试这个
var questions = ["fourth letter", "grass", "6x4", "NFL team", "OSU Buckeys", "35 + 15", "Yellow + Blue", "LeBron James", "Lassie", "2x9", "9x10"];
var answers = ["s", "green", "nut", 24, "Bengals", "gray", 50, "green", 23, "dog", 18, 90];
var answer = 0;
var position = 0;
var found = false;
window.onload = function ()
for (var i = 0;i<=questions.length;i++)
if (document.body.innerHTML.toString().indexOf(questions[i]) > -1)
position = i;
found = true;
break;
if(found)
document.getElementById("humanverify\[input\]").innerHTML = answers[position];
else
console.log ("No answer was found");
【讨论】:
【参考方案2】:我整天都在摆弄它,并想出了一个满足我所有需求的实用结果。结果如下:
var answers = ["s", "green", "nut", 24, "bengals", "gray", 50, "green", 23, "dog", 18, 90];
var questions = ['The fourth letter in the word "Reds" is?','What color is grass?','A buckeye is a what?',"What's 6x4?",'The NFL team in Cincinnati is called the?','The OSU Buckeys are Scarlet and?','35 + 15 is?','Yellow + Blue is?','LeBron James wears #?','Lassie was a what?','2x9 is what?',"What's 9x10?"];
var question = document.getElementsByClassName('description')[0].innerText;
var found = 0;
var answer = 0;
//browses questions variable for match to question variable, then assigns the correlating index to found, and assigns the answer to the answer variable, as pulled from the answers variable
for (var find = 0; find <= questions.length; find++)
if (question === questions[find])
found = find;
answer = answers[found];
//checks the assigned radio, fills the textbox with the answer, and clicks the Vote button
var submit = function ()
document.getElementsByName('polloptionid')[3].checked = true;
document.getElementById("humanverify").value = answer;
document.getElementsByName("vote")[0].click();
submit();
//allows for a slight delay so that the page can load before cycling through script again
var reload = function ()
location.reload();
setTimeout(reload, 3000);
【讨论】:
以上是关于在 Tampermonkey 中使用 javascript 进行页面搜索和文本框自动填充的主要内容,如果未能解决你的问题,请参考以下文章
使用 Tampermonkey 在 Google Chrome 中缓存浏览器版本
在 Tampermonkey 中模拟 mousedown、click、mouseup 序列?