gpt4 book ai didi

google-chrome-extension - 如何触发点击按钮

转载 作者:行者123 更新时间:2023-12-02 08:14:22 24 4
gpt4 key购买 nike

我有 this page .我需要使用 AngularJS 触发点击此页面上的立即购买按钮。

我已经尝试了这些方法来点击内容脚本(myscript.js)中的“立即购买”,但不起作用:

   angular.element($('ul form button:contains("BUY NOW")').get(0)).triggerHandler('click');

$('ul form button:contains("BUY NOW")').get(0).click();

$('ul form button:contains("BUY NOW")').get(0).dispatchEvent(new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': true
}));

manifest.json 看起来像这样:

"content_scripts": [
{
"run_at": "document_end",
"all_frames": false,
"matches": ["*://www.flipkart.com/*"],
"css": [ "jqueryui/jquery-ui.css", "js/slidenavi/sidenavi-right.css","main.css", "js/bootstrap-switch-master/dist/css/bootstrap3/bootstrap-switch.min.css"],
"js": ["jquery-2.1.4.min.js", "jqueryui/jquery-ui.min.js","js/angular.min.js", "js/jquery.cookie.js", "jqueryui/jquery-ui.min.js","js/slidenavi/SideNavi.js", "client_server_common.js", "user-selections.js",
"jquery.countdown.min.js", "js/bootstrap-switch-master/dist/js/bootstrap-switch.min.js", "js/cryptojs/rollups/md5.js", "common.js",
"myscript.js"
]
}
],

有什么办法让它发挥作用?

最佳答案

试试这个代码;它通过在按钮中心触发的快速连续的 mousedown、mouseup 和 click 事件来模拟鼠标左键单击元素:

var simulateMouseEvent = function(element, eventName, coordX, coordY) {
element.dispatchEvent(new MouseEvent(eventName, {
view: window,
bubbles: true,
cancelable: true,
clientX: coordX,
clientY: coordY,
button: 0
}));
};

var theButton = document.querySelector('ul form button');

var box = theButton.getBoundingClientRect(),
coordX = box.left + (box.right - box.left) / 2,
coordY = box.top + (box.bottom - box.top) / 2;

simulateMouseEvent (theButton, "mousedown", coordX, coordY);
simulateMouseEvent (theButton, "mouseup", coordX, coordY);
simulateMouseEvent (theButton, "click", coordX, coordY);

关于google-chrome-extension - 如何触发点击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43291181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com