我正在尝试制作一个Chrome扩展程序,可以在按Ctrl + Shift + X时识别。我正在使用命令API,我把它放在我的清单中。然后,在我的后台页面中,我收到命令并尝试自动按下页面上的按钮。出于某种原因,它不起作用,我没有收到错误。你们能帮助我吗?谢谢! 我在这个网站上使用我的程序: http://www.supremenewyork.com/shop/accessories/b82ba9vpq/jx7qm1hsc 当我不使用命令时,它完全正常 清单:
{
"manifest_version":2,
"name": "Hotkey",
"description": "Press buttons on keyboard to activate hotkeys.",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"commands":{
"doo":{
"suggested_key":{
"default":"Ctrl+Shift+X",
"mac":"Command+Shift+X"
},
"description":"Fast"
}
},
"permissions": [
"activeTab",
"storage"
]
}
background.js
chrome.commands.onCommand.addListener(function(command){
chrome.tabs.executeScript({
file: "injector.js"
});
});
injector.js
function goToActivityTab() {
var activityTab = document.getElementsByName("commit")[0];
activityTab.click();
sleepFor(100);
var addToCart=document.getElementsByClassName("button checkout")[0];
addToCart.click();
}
goToActivityTab();
function sleepFor( sleepDuration ){
var now = new Date().getTime();
while(new Date().getTime() < now + sleepDuration){ /* do nothing */ }
}