Javascript Chrome.tabs.executeScript-未定义选项卡
我正在尝试编写一个chrome扩展,它有一个名为“btn3”的按钮。当我点击chrome扩展(popup.html)中的那个按钮时,它会点击网页上的一个按钮。网页上的按钮具有以下id:“常规辅助按钮发送消息” 2个问题:Javascript Chrome.tabs.executeScript-未定义选项卡,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我正在尝试编写一个chrome扩展,它有一个名为“btn3”的按钮。当我点击chrome扩展(popup.html)中的那个按钮时,它会点击网页上的一个按钮。网页上的按钮具有以下id:“常规辅助按钮发送消息” 2个问题: 我在下面的代码中得到chrome.tabs.executeScript的错误“tabs未定义”。我怎样才能解决这个问题 我在content_scripts.js中写错了什么吗 谢谢 chrome扩展窗口中按钮的脚本 document.addEventListener('DOMC
document.addEventListener('DOMContentLoaded', function (){
document.getElementById('btn3').addEventListener('click', sendInMail)
});
sendInMail = function(){
chrome.tabs.executeScript(tabs[0], {file: "content_script.js"});
}
content\u scripts.js
alert("content_script is working!");
function clickSendInMailButton() {
var SendInMailButton = document.getElementsByClassName("regular-secondary-button send-message"),
SendInMailButton.click();
}
clickSendInMailButton();
Manifest.json
{
"manifest_version": 2,
"name": "LinkedIn Assistant",
"description": "This extension makes a LSS AE successful.",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content_script.js"]
}
],
"chrome_url_overrides" : {
"newtab": "newtab.html"
},
"background": {
"scripts": ["bg.js"]
},
"permissions": [
"tabs", "<all_urls>"
]
}
{
“清单版本”:2,
“姓名”:“LinkedIn助手”,
“说明”:“此扩展使LSS AE成功。”,
“版本”:“1.0”,
“浏览器操作”:{
“默认图标”:“icon.png”,
“默认弹出窗口”:“popup.html”
},
“内容脚本”:[
{
“匹配项”:[“http://*/*”,“https://*/*”],
“js”:[“content_script.js”]
}
],
“chrome\u url\u覆盖”:{
“newtab”:“newtab.html”
},
“背景”:{
“脚本”:[“bg.js”]
},
“权限”:[
“制表符”
]
}
试试这个:
sendInMail = function(){
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
chrome.tabs.executeScript(tabs[0], {file: "content_script.js"});
});
}
“未定义选项卡”的错误
制表符[0]-您需要找到它。您的脚本在popup.html的上下文中执行。我希望popup.html包含
<head>
...
<script src="popup.js"></script>
</head>
或者,您也可以不使用选项卡id进行尝试,
chrome.tabs.executeScript({ file: 'content_script.js', allFrames: false });
因为在本例中,脚本将在
当您在Manifest.json中指定“content\u scripts”:
脚本将在创建选项卡和时执行。我不确定,但可能在加载DOM之前。但是,您不应该在Manifest.json中指定“content\u scripts”:
,因为
此外,您必须确保活动选项卡包含您正在查找的按钮。对某些对象使用
console.dir
,尤其是函数中的参数console.log
查看一些文本<代码>调试器用于愉快的调试。您只需删除参数“tabs[0]”
整数(可选)选项卡ID要在其中运行脚本的选项卡的ID;默认为当前窗口的活动选项卡
不起作用。现在我得到以下信息:extensions::uncaught_exception_handler:8响应制表符时出错。query:错误:调用表单制表符。executeScript(对象,对象)与定义制表符不匹配。executeScript(可选整数制表符、对象详细信息、可选函数回调)。。。。。。在Object.callback(chrome-extension://bdndljnkfidkacoflhfhpgilegpfgdja/sendInMail.js:10:17)在HTMLButtonElement.sendInMail(chrome-extension://bdndljnkfidkacoflhfhpgilegpfgdja/sendInMail.js:6:15)除了在清单中添加选项卡权限外,从后台调用此函数,还请确保按照本SO帖子中的建议在Chrome中重新加载扩展的权限。获取权限是误导性的。该示例使用了一个弹出窗口并引用了
选项卡
,但实际上它不是现成的。谢谢你。
chrome.tabs.executeScript({ file: 'content_script.js', allFrames: false });