简体   繁体   English

如何模拟 jquery 事件来触发 Google 自动完成?

[英]How do I simulate jquery events to trigger Google Autocomplete?

I want to use jquery to programmatically enter a word or phrase and then trigger the Google search box autocomplete suggestions from my console.我想使用 jquery 以编程方式输入单词或短语,然后从我的控制台触发 Google 搜索框自动完成建议。

Here is what I have tried so far:这是我到目前为止所尝试的:

// 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");

If I go through the first part programmatically of injected jquery and then entering the word dog it works perfectly.如果我 go 通过第一部分以编程方式注入 jquery 然后输入单词 dog 它可以完美运行。 I can even manually click on the search box and I will see all the search suggestions for the word dog even though I did not enter it manually.我什至可以手动点击搜索框,即使我没有手动输入,我也会看到所有关于 dog 的搜索建议。 The issue is I can't seem to figure out via jquery how to programmatically simulate the proper event that results in it displaying the autosuggest as when I click manually.问题是我似乎无法通过 jquery 弄清楚如何以编程方式模拟导致它在我手动单击时显示自动建议的正确事件。

Special thanks to sideroxylon for the reference.特别感谢 sideroxylon 的参考。 For anyone intersted here is how I got it to work.对于任何对此感兴趣的人来说,我是如何让它工作的。

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

// Add the word DOG into the search box //** This part works ** let searchbar = $('input[name="q"]'); // 将单词 DOG 添加到搜索框中 //** 这部分有效 ** let searchbar = $('input[name="q"]');

searchbar.val('cat'); searchbar.val('cat');

searchbar.click();搜索栏.click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM