模拟鼠标点击链接的chrome扩展

Simulate mouse click on link for chrome extension

本文关键字:chrome 扩展 链接 鼠标 模拟      更新时间:2023-09-26

我正在写chrome扩展,我需要自动点击链接下一页。
我使用jQuery.noConflict();在我的Chrome控制台总是抛出错误- jquery-3.1.0.min.js:2 Uncaught TypeError: target.dispatchEvent is not a function.
这是我的代码:

var dispatchMouseEvent = function (target, var_args) {
var e = document.createEvent("MouseEvents");
e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1));
target.dispatchEvent(e);};
function GoToNextPage() {
var link_nextpage = null;
jQuery(function ($) {
    var pages = $('#results-pagination');
    var next_page = pages.find('.next');
    var link_nextpage = null;
    if (next_page.length) {
        link_nextpage = next_page.find('a');
        dispatchMouseEvent(link_nextpage, 'click', true, true);
    }       
});
}

你能给我解释一下这样做是否可能,或者告诉我做错了什么吗?

如wOxxOm所述,link_nextpage是一个jquery对象,而不是dom元素,dispatchMouseEvent(link_nextpage[0], 'click', true, true);好工作。
那是我愚蠢的错误。