[英]Greasemonkey get google search autocomplete results
我正在嘗試從 Google 搜索中獲得自動完成建議(例如,當我們按“a”並獲得建議結果的列表時),並通過腳本實現這一點(這實際上是另一個網站的腳本,但我把谷歌,因為這是公開的)。
問題是腳本用隨機字母完成輸入,但我們沒有得到任何建議。 這是我的腳本:(您可以在 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);
如果有人能提供信息為什么這不會觸發 Google 上的自動完成功能,我將不勝感激:)
最后,我需要向父元素添加點擊。 這是最終腳本,以防將來有人需要類似的解決方案:
// ==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.