我只想设置一个简单的Chrome扩展程序,只需在点击扩展程序按钮后单击某个元素即可。
我对此进行了一些研究,我找不到一个简单的答案,说明如何点击,其他人都有非常精细的代码,我无法理解,也不知道是否有必要。我的意思是,每当我搜索“点击”时,我发现一个比我的级别更高级的问题。
(我即将从这个扩展中赚取很多钱,所以请你帮助一个兄弟;)
使用我所看到的,我已将代码拼凑在一起:
popup.js:
var evt = document.createEvent ("HTMLEvents");
evt.initEvent ("click", true, true);
document.getElementById('product-addtocart-button').dispatchEvent (evt);
的manifest.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:
<!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>
答案 0 :(得分:1)
目前尚不清楚您使用document.createEvent()
的原因。该接口已弃用。要创建活动,您应该使用event constructors。但是,对于HTML元素上的通用click
event,您只需使用click()
method而无需实际构建事件。
当您点击id="product-addtocart-button"
按钮时,一个简单,完整的Chrome扩展程序会点击content script点按browser_action
按钮,这将是:
background.js :
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id,{
code: "document.getElementById('product-addtocart-button').click();"
});
});
的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"
}
}