Edw*_*hen 8 html javascript google-chrome-extension
我正在尝试制作一个 google chrome 扩展,它允许用户按下 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"
]
}
Run Code Online (Sandbox Code Playgroud)
这是我的 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>
Run Code Online (Sandbox Code Playgroud)
和我的 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"
});
});
}
}
Run Code Online (Sandbox Code Playgroud)
注入器.js:
function pressButton(){
var buttons=document.getElementsByClassName("button"),
button[0].click();
}
pressButton();
Run Code Online (Sandbox Code Playgroud)
顺便说一下,我试图点击的按钮是一个输入按钮,我在其中检查元素,我得到:"(input type="submit" name="commit" value="add to cart" class="button") " 出于某种原因不会显示此按钮可以在这里找到:http : //www.supremenewyork.com/shop/accessories/p840x2jsc/yks6zay73
注意:我知道还有其他人就同一主题提出问题,但他们使用的方法对我不起作用。谢谢你的协助!
现在它起作用了。安装它并检查它。要查看扩展程序的工作原理,您应该访问此站点 (StackOverflow) 并提供所有这些文件和icon.png
清单文件
{
"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>"]
}
Run Code Online (Sandbox Code Playgroud)
弹出窗口.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>
Run Code Online (Sandbox Code Playgroud)
弹出窗口.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);
Run Code Online (Sandbox Code Playgroud)
实用程序.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();
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请使用这些链接