[英]How to click on a button in Chrome Extension
I am just trying to set up a simple chrome extension that will just click on an element once the extension button is clicked on.我只是想设置一个简单的 chrome 扩展,一旦点击扩展按钮,它就会点击一个元素。
I have researched this a bit, and I can't find a simple answer that says how to click, everyone else has very elaborate code that I can't understand and don't know if it's necessary or not.我对此进行了一些研究,但找不到一个简单的答案来说明如何单击,其他所有人都有非常复杂的代码,我无法理解,也不知道是否有必要。 What I mean is, whenever I search for just "click" I find a question that is much more advanced than my level.我的意思是,每当我搜索“点击”时,我会发现一个比我的水平高得多的问题。
(I am about to make a lot of $ off of this extension, so please will you help a brother out ;) (我即将从这个扩展中赚很多钱,所以请你帮助兄弟;)
Using what I have seen I have scraped together the code:使用我所看到的,我将代码拼凑在一起:
popup.js: popup.js:
var evt = document.createEvent ("HTMLEvents");
evt.initEvent ("click", true, true);
document.getElementById('product-addtocart-button').dispatchEvent (evt);
manifest.Json:清单.json:
{
"manifest_version": 2,
"name": "Shoe BOT",
"description": "This extension shows a Google Image search result for the current page",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab",
"https://shop.adidas.ae/en/"
]
}
popup.html: popup.html:
<!doctype html>
<!--
This page is shown when the extension button is clicked, because the
"browser_action" field in manifest.json contains the "default_popup" key with
value "popup.html".
-->
<html>
<head>
<title>Getting Started Extension's Popup</title>
<style>
body {
}
#status {
}
</style>
<!--
- JavaScript and HTML must be in separate files: see our Content Security
- Policy documentation[1] for details and explanation.
-
- [1]: https://developer.chrome.com/extensions/contentSecurityPolicy
-->
<script src="popup.js"></script>
</head>
<body>
<!--<div id="status"></div>
<img id="image-result" hidden>-->
</body>
</html>
It is not clear why you are using document.createEvent()
. 目前尚不清楚为什么要使用document.createEvent()
。 That interface is deprecated. 该接口已弃用。 To create events, you should be using event constructors . 要创建事件,您应该使用事件构造函数 。 However, for a generic click
event on an HTML element, you can just use the click()
method without having to actually construct the event. 但是,对于HTML元素上的常规click
事件 ,您可以仅使用click()
方法,而不必实际构造该事件。
A simple, complete Chrome extension which injects a content script to click the button with id="product-addtocart-button"
when you click on a browser_action
button would be: 一个简单,完整的Chrome扩展程序可以是:当您单击browser_action
按钮时,会注入内容脚本以单击带有id="product-addtocart-button"
按钮:
background.js : background.js :
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id,{
code: "document.getElementById('product-addtocart-button').click();"
});
});
manifest.json : manifest.json :
{
"description": "Click a button with ID=product-addtocart-button",
"manifest_version": 2,
"name": "click-product-addtocart-button",
"version": "0.1",
"permissions": [
"activeTab"
],
"background": {
"scripts": [
"background.js"
]
},
"browser_action": {
"default_icon": {
"32": "myIcon.png"
},
"default_title": "Click product-addtocart-button"
}
}
With the new manifest version (V3) the page_action
and browser_action
were unified into one single action
:在新的清单版本(V3)中, page_action
和browser_action
被统一为一个action
:
"action": {
"title": { 'This is a popup tip!'}
}
You just avoid declaring any default_popup
and in background.js
you write:您只需避免声明任何default_popup
并在background.js
中编写:
chrome.action.onClicked.addListener(
async function(tab) {
// what you want to do when click the extension button
});
Incredibly easy !非常容易!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.