![](/img/trans.png)
[英]Clicking a button with Javascript on an official website (chrome extension)
[英]Clicking Buttons on a Website with a chrome extension
我正在尝试制作一个谷歌浏览器扩展程序,允许用户按下Chrome扩展菜单中的按钮,这会导致选中的网页上的按钮被点击。我目前的问题是我的injector.js中的语法和代码。 它说意外的令牌; 和意外的令牌[。 我将不胜感激任何帮助。 这是我的manifest.json:
{
"manifest_version":2,
"name": "Button Click",
"description": "Be able to press buttons",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab",
"storage"
]
}
这是我的HTML
<!DOCTYPE html>
<html>
<head>
<title>Fill</title>
</script><script src="popup.js"></script>
</head>
<body>
<h2 id="htwo">Button presser</h2>
<button id="press">Press Button</button>
</body>
</html>
和我的javascript:
window.onload=function(){
if(document.getElementById("press")){
document.getElementById("press").addEventListener('click',function(){
document.getElementById("htwo").innerHTML="yay";
chrome.tabs.executeScript({
file:"injector.js"
});
});
}
}
injector.js:
function pressButton(){
var buttons=document.getElementsByClassName("button"),
button[0].click();
}
pressButton();
顺便说一句,我试图点击的按钮是一个输入按钮,我检查元素,我得到:“(输入类型=”提交“名称=”提交“值=”添加到购物车“class =”按钮“) “由于某种原因不会显示这个按钮可以在这里找到: http : //www.supremenewyork.com/shop/accessories/p840x2jsc/yks6zay73
注意:我知道还有其他人询问有关同一主题的问题,但他们使用的方法对我不起作用。 谢谢你的协助!
现在它有效。 安装并检查出来。 要查看扩展如何工作,您应该在此站点(StackOverflow)并提供所有这些文件和icon.png
的manifest.json
{
"manifest_version": 2,
"name": "Button Click",
"description": "Be able to press buttons",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": ["tabs", "<all_urls>"]
}
popup.html
<!doctype html>
<html>
<head><title>Fill</title></head>
<body>
<h2 id="htwo">Button presser</h2>
<button id="press">Go to activity tab</button>
<script src="popup.js"></script>
</body>
</html>
popup.js
function injectTheScript() {
// Gets all tabs that have the specified properties, or all tabs if no properties are specified (in our case we choose current active tab)
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
// Injects JavaScript code into a page
chrome.tabs.executeScript(tabs[0].id, {file: "utilities.js"});
});
}
// adding listener to your button in popup window
document.getElementById('press').addEventListener('click', injectTheScript);
utilities.js
/**
* Gets the desired element on the client page and clicks on it
*/
function goToActivityTab() {
var activityTab = document.getElementsByClassName("my-profile")[0];
activityTab.click();
}
goToActivityTab();
有关更多信息,请使用这些链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.