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).
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)
// ==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:)
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.