0

我想使用 jquery 以编程方式输入单词或短语,然后从我的控制台触发 Google 搜索框自动完成建议。

这是我到目前为止所尝试的:

// Inject JQuery into page from console
var script = document.createElement("script");
script.src = "https://code.jquery.com/jquery-2.1.4.min.js";
document.body.appendChild(script);

// Add the word DOG into the search box
//** This part works **
$('input[name="q"]').val("dog");

// Attempt #1 to simulate clicking on the box
// ** This did appear to add a focus look to the box but it did not trigger the autocomplete suggestions listing
$('input[name="q"]').trigger("click");

// Attempt #2 to simulate clicking on the box
// ** This did not work.
$('input[name="q"]').trigger("mouseup");

如果我以编程方式完成注入 jquery 的第一部分,然后输入单词 dog,它就可以完美地工作。我什至可以手动点击搜索框,即使我没有手动输入,我也会看到所有关于 dog 的搜索建议。问题是我似乎无法通过 jquery 弄清楚如何以编程方式模拟导致它在我手动单击时显示自动建议的正确事件。

4

1 回答 1

1

特别感谢 sideroxylon 的参考。对于任何对此感兴趣的人来说,我是如何让它工作的。

// 从控制台将 JQuery 注入页面 var script = document.createElement("script"); script.src = "https://code.jquery.com/jquery-2.1.4.min.js"; document.body.appendChild(脚本);

// 将单词 DOG 添加到搜索框中 //** 这部分有效 ** let searchbar = $('input[name="q"]');

searchbar.val('cat');

搜索栏.click();

于 2021-05-18T18:49:37.073 回答