我正在尝试在Grease/Tampermonkey中编写一个脚本,它将搜索网页中的问题,然后用正确的答案填充文本框。在Tampermonkey中使用javascript进行页面搜索和文本框自动填充
这是我想出迄今:
//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的新手,并且在上周一直在拼凑一些代码和知识。