简体   繁体   中英

Force autocomplete to populate on javascript click

I am working on Bookmarklet and having an issue with click function in Javascript. On google home page, If I manually click search bar then it populates my recent searches but when I go to Dev console and use click() on the search box element then it doesn't populate my recent searches.

Manual Click

Console Click

It seems that you should emulate mouse events. Perhaps something like this:

javascript:(function() {
    fireMouseEvent(document.querySelector("#lst-ib"));

    function fireMouseEvent(eltTarget) {
        var screenX, screenY, clientX, clientY;
        var event = document.createEvent('MouseEvents');
        event.initMouseEvent('mousedown', true, true, window, 1, screenX, screenY, clientX, clientY, false, false, false, false, 0, null);
        eltTarget.dispatchEvent(event);
    }
})();

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.

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