[英]How To Trigger Button Click On A Web That Uses History API HTML5 Using JavaScript?
I'm developing a Chrome Extension that automates click.我正在开发一个自动点击的 Chrome 扩展程序。 My extension worked on Multi-Page Application.
我的扩展适用于多页应用程序。 It can click the buttons on the website.
它可以点击网站上的按钮。 But I'm having a problem with this specific website which is a Single Page Application.
但是我遇到了这个特定网站的问题,它是一个单页应用程序。
My extension can't trigger button click using this JavaScript我的扩展程序无法使用此 JavaScript 触发按钮点击
function clickButton(){
var submit = document.getElementsByClassName(".class");
submit.click();
}
My Question is, how to trigger button click on a single page application?我的问题是,如何在单页应用程序上触发按钮单击? Because it seems like using .click() isn't working
因为似乎使用 .click() 不起作用
Further information更多信息
The way my extension works is that I click on a search button, it shows me result.我的扩展程序的工作方式是单击搜索按钮,它会显示结果。 If there is a result, I do specific things.
如果有结果,我会做具体的事情。 But if result is empty, I go back to previous page (search page) and then hit the search button again.
但如果结果为空,我会返回上一页(搜索页面),然后再次点击搜索按钮。 And my extension can't click the search button nor back button.
而且我的扩展程序无法单击搜索按钮或后退按钮。 My assumption is, when I click the button using .click(), the web doesn't request that specific HTML page to show.
我的假设是,当我使用 .click() 单击按钮时,网络不会请求显示该特定 HTML 页面。 Am I wrong?
我错了吗?
Another Info About This Site关于本网站的另一个信息
So this web is a single page application.所以这个网站是一个单页应用程序。 After I login to this site, it brings me to https://sample.com/page1 -which is a home.
我登录到这个网站后,它把我带到了https://sample.com/page1 - 这是一个家。 Then I go to the search page (the URL is still the same but it shows a different layout, which is search page).
然后我转到搜索页面(URL 仍然相同,但显示了不同的布局,即搜索页面)。 On the search page, I input the search parameters, then hit the search button (the URL is still the same but it shows the different layout, which is search results).
在搜索页面,我输入搜索参数,然后点击搜索按钮(URL仍然相同,但显示不同的布局,即搜索结果)。 So I guess the way these web works is every time I click on a certain page, it requests page layout using history.pushState -i guess.
所以我猜这些网络的工作方式是每次我点击某个页面时,它都会使用 history.pushState 请求页面布局 - 我猜。 So I'm wondering if there's a way to manipulate this history.pushState?
所以我想知道是否有办法操纵这个history.pushState? For example, instead of clicking the back button, can I just go straight to the desired page?
例如,我可以直接转到所需页面而不是单击后退按钮吗?
document.getElementsByClassName(".class")
returns an array, so array.onclick() will not work. document.getElementsByClassName(".class")
返回一个数组,因此 array.onclick() 将不起作用。
To trigger html event, not only click:要触发html事件,不仅点击:
function trigger(elm, type) {
if (elm) {
var evt = document.createEvent("HTMLEvents");
evt.initEvent(type, true, true);
elm.dispatchEvent(evt);
}
}
In your case, I think you want:在你的情况下,我认为你想要:
trigger(document.querySelector('.class'), 'click')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.