我正在尝试创建一个Chrome扩展程序,它会模仿选项卡上的按钮单击。我知道按钮属于“玩家玩”类......这里是类和子元素的结构。
div class: player-play
table
tbody
tr
td
em
button (here's want I want to *click*)
按钮有一个ID,但它不是静态的...但是,这是我的“player-play”类中唯一的按钮。我试图使用此代码
chrome.tabs.sendMessage(tabid, {action: "document.getElementsByClassName(\"player-play\").click();"}, function(response){});
但这不起作用。
正如旁注,我已经尝试调试页面来获取页面中的元素,但调试器看到“文档”作为我的调试页面,而不是我正在调试的选项卡...任何想法如何指定“文档”应该是什么?
那么有什么方法可以根据类型和我已知的类使用click()吗?
仅供全面参考,我正在尝试创建一个键盘快捷键扩展程序,允许我播放/暂停我的Synology AudioStation Web应用程序(前提是它在我的某个标签页中打开)。我使用this扩展名作为参考(因为我说我是Javascript的菜鸟,文档似乎是分散的)
完整代码:
var currentRdioTabID = 0;
//update current tab
chrome.extension.onMessage.addListener(function(request, sender, sendResponse){
});
//to respond to new key press system
chrome.commands.onCommand.addListener(function(command){
chrome.tabs.query({
url : "[url here]"
},
function(tabs){
if(tabs.length == 0){
console.log('no tabs found.');
}else if(tabs.length >= 1){
thetab = tabs[0];
//if(tabs.length == 1){
thetab = tabs[0];
console.log('found one tab');
/*}else{
//console.log('found mo tabs');
for(var i=0; i<tabs.length; i++){
chrome.tabs.sendMessage(tabs[i].id, {masterCheck:true}, function(response){
//console.log(response);
if(response.masterCheck == 'true'){
thetab = tabs[i];
}
});
}
}*/
switch(command){
case "prev":
//console.log('playpause');
//chrome.tabs.sendMessage(thetab.id, {action:"R.player.playPause();"}, function(response){});
console.log('play-pause');
chrome.tabs.sendRequest(thetab.id, {action: "document.getElementsByClassName(\"player-play\");"}, function(response){});
//chrome.tabs.sendMessage(thetab.id, {action: "document.getElementsByClassName(\"player-play\").click();"}, function(response){});
chrome.tabs.executeScript(thetab.id, {code: "document.getElementsByClassName(\"player-play\").click();"}, function(response){});
var t = document.getElementsByClassName('player-play');
chrome.tabs.sendMessage(thetab.id, {action: "document.getElementsByClassName(\"player-play\");"}, function(response){rr});
break;
// case "next":
// chrome.tabs.sendMessage(thetab.id, {action:"R.player.next();"}, function(response){});
// break;
// case "prev":
// chrome.tabs.sendMessage(thetab.id, {action:"R.player.previous();"}, function(response){});
// break;
case "focus":
console.log('changing tabs')
chrome.tabs.update(thetab.id, {active:true});
break;
}
}
}
);
});