How to add automatic click feature in chrome extension?

被刻印的时光 ゝ 提交于 2019-12-14 02:23:07

问题


I am creating a chrome extension, and need to click on the first link automatically for some time. Is it possible to add automatic click feature in chrome extension??


回答1:


If you don't care about mouse coordinates, you can use this:

var trigger = document.createEvent("Event");
trigger.initEvent("click", true, true);
element.dispatchEvent(trigger);

"element" should be the DOM node you want to trigger the click on. If you want to also specify a particular X/Y coordinate, you could use this:

var x = 0,
    y = 0;

var trigger = document.createEvent("MouseEvent");
trigger.initMouseEvent("click", true, true, null, 0, x, y, x, y, false, false, false, false, 0, null);
element.dispatchEvent(trigger);

Change x and y to what you want ...or just use jQuery, like npdoty said.




回答2:


jQuery has a nice trigger function for just such a situation (jQuery docs), even a shortened form just for the click event, and that pairs nicely with the :first pseudo-selector.

$('a:first').click();

You can probably do this in pure JavaScript as well, using some combination of element.click() or dispatching events.



来源:https://stackoverflow.com/questions/3615149/how-to-add-automatic-click-feature-in-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!