[英]Greasemonkey get google search autocomplete results
I'm trying to have the autocomplete suggestions from Google search (eg when we press 'a' and we get a list of the suggested results), and achieve that through the script (This is actually a script for another website, but I put Google since this is publicly accessible).我正在尝试从 Google 搜索中获得自动完成建议(例如,当我们按“a”并获得建议结果的列表时),并通过脚本实现这一点(这实际上是另一个网站的脚本,但我把谷歌,因为这是公开的)。
The problem is that the script completes the input with random letters, but we get no suggestions.问题是脚本用随机字母完成输入,但我们没有得到任何建议。 Here's my script: (You can use it on Greasemonkey, and check the result on google.com)
这是我的脚本:(您可以在 Greasemonkey 上使用它,并在 google.com 上查看结果)
// ==UserScript==
// @name Google Test
// @version 1
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @match https://*.google.com/*
// @grant none
// ==/UserScript==
console.log('Google script started');
function performSearch() {
const customKeyCode = Math.floor((Math.random() * 10) + 60);
// I use .gLFyf.gsfi for the selector, you can inspect your google.com in case class is different in your case
$('input.gLFyf.gsfi').each(function(i) {
$(this).attr("autocomplete","on");
$(this).blur();
$(this).focus();
$(this).trigger($.Event("keydown", {keyCode: customKeyCode}));
$(this).val(String.fromCharCode(customKeyCode));
$(this).trigger($.Event("keypress", {keyCode: customKeyCode}));
$(this).trigger($.Event("keyup", {keyCode: customKeyCode}));
$(this).keypress();
});
console.log('filled ');
}
// initial calls
setInterval(() => {
performSearch();
}, 2000);
If anyone could provide info why this is not triggering the autocomplete on Google, I would be more than grateful:)如果有人能提供信息为什么这不会触发 Google 上的自动完成功能,我将不胜感激:)
Finally I need to add a click to the parent element.最后,我需要向父元素添加点击。 Here's the final script in case someone will need a similar solution in the future:
这是最终脚本,以防将来有人需要类似的解决方案:
// ==UserScript==
// @name Google Test
// @version 1
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @match https://*.google.com/*
// @grant none
// ==/UserScript==
console.log('Google script started');
function performSearch() {
const customKeyCode = Math.floor((Math.random() * 10) + 60);
// I use .gLFyf.gsfi for the selector, you can inspect your google.com in case class is different in your case
$('input.gLFyf.gsfi').each(function(i) {
$(this).attr("autocomplete","on");
$(this).blur();
$(this).focus();
$(this).trigger($.Event("keydown", {keyCode: customKeyCode}));
$(this).val(String.fromCharCode(customKeyCode));
$(this).trigger($.Event("keypress", {keyCode: customKeyCode}));
$(this).trigger($.Event("keyup", {keyCode: customKeyCode}));
$(this).parent().trigger('click');
});
console.log('filled ');
}
// initial calls
setInterval(() => {
performSearch();
}, 2000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.