Javascript 从弹出窗口到内容脚本的消息不工作
我有一个chrome扩展,chrome扩展的用例如下: 第一部分Javascript 从弹出窗口到内容脚本的消息不工作,javascript,google-chrome-extension,content-script,Javascript,Google Chrome Extension,Content Script,我有一个chrome扩展,chrome扩展的用例如下: 第一部分 chrome扩展有一个包含2个div元素和一个按钮的弹出页面 单击弹出窗口后,获取当前选项卡的URL,并使用chrome.runtime.sendMessage API将其发送到后台脚本 后台脚本有一个消息侦听器,它使用cookies执行xhr请求,获取与url相关的数据,然后将其发送回popup.js popup.js根据收到的数据编辑2个div元素 这部分工作非常完美,但是第二部分变得有点棘手: 第2部分: 如果我们点击弹出窗
{
"manifest_version": 2,
"name" : "Switcher",
"version": "1.1",
"browser_action":{
"default_popup":"popup.html"
},
"permissions":["tabs","activeTab","cookies","<all_urls>"],
"background": {
"scripts": ["background.js"],
"persistent": false
}
}
background.js
document.addEventListener("DOMContentLoaded", function(event){
var obj = {"active": true, "currentWindow": true}
chrome.tabs.query(obj, function(tab){
chrome.runtime.sendMessage({url: tab[0].url, action: "id_load"},function(response) //Send message to background script with the URL of the tab
{
//Manipulate the popup divs using the response from background script
})
})
var timezone = document.getElementById("change_timezone")
timezone.addEventListener('click',change_timezone_function)
function change_timezone_function()
{
var query_info = {active: true, currentWindow: true}
chrome.tabs.query(query_info, function(tab){
console.log(tab[0].id)
chrome.tabs.sendMessage(tab[0].id,{time: ticket_time},function(response) //Send message to the content script by getting the tab ID
{
console.log(response)
})
})
}
})
chrome.runtime.onMessage.addListener(
function(request,sender,sendResponse)
{
//Do some manipulation, few api calls and send response asynchronously after the api call is successful
return true
})
chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
var date_time = new Date(request.time.toString())
console.log(request.time)
sendResponse({date_time: request.time})
})
timezone.js
document.addEventListener("DOMContentLoaded", function(event){
var obj = {"active": true, "currentWindow": true}
chrome.tabs.query(obj, function(tab){
chrome.runtime.sendMessage({url: tab[0].url, action: "id_load"},function(response) //Send message to background script with the URL of the tab
{
//Manipulate the popup divs using the response from background script
})
})
var timezone = document.getElementById("change_timezone")
timezone.addEventListener('click',change_timezone_function)
function change_timezone_function()
{
var query_info = {active: true, currentWindow: true}
chrome.tabs.query(query_info, function(tab){
console.log(tab[0].id)
chrome.tabs.sendMessage(tab[0].id,{time: ticket_time},function(response) //Send message to the content script by getting the tab ID
{
console.log(response)
})
})
}
})
chrome.runtime.onMessage.addListener(
function(request,sender,sendResponse)
{
//Do some manipulation, few api calls and send response asynchronously after the api call is successful
return true
})
chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
var date_time = new Date(request.time.toString())
console.log(request.time)
sendResponse({date_time: request.time})
})
所有答案都指向一个事实,即popup.js可以使用特定于选项卡的消息与内容脚本交互,但这似乎不起作用,我认为这可能是因为后台的侦听器,但从逻辑上讲,没有理由相信这一点,由于我从弹出窗口发送的消息专门发送到选项卡的内容脚本。要将消息发送到内容脚本,您需要在manifest.json中声明消息或使用chrome.tabs.executeScript进行注入,请参阅。感谢您的回答,但我仍然有一些疑问,一个是我正在进行编程注入,因此,我将尝试文档中给出的方法,除了文档说内容脚本支持消息API之外,那么为什么不发送我的消息呢?此外,由于我正在进行编程注入,我认为activeTab权限应该足够了,不是吗?我也尝试过声明文档中给出的内容脚本和URL模式,但我仍然面临同样的问题。1)我不理解这种混淆。要接收信息,应该有东西接收它。目前,您没有可接收的内容脚本。因此,无法发送该消息。2) 有关更多信息,请参阅文档。3) 编辑manifest.json时,需要在chrome://extensions 翻页并重新加载选项卡。也可能是您的声明不正确,因此我无法帮助您。@wOxxOm添加一个示例文件夹这是在内容脚本中使用一个简单的onmessage侦听器。我已经在清单中声明了它,但面临着同样的错误。我将单独尝试程序注入,并让您知道。声明内容脚本是否在页面加载后立即执行它们?或者他们会等待弹出窗口中的按钮单击之类的操作吗?它会在加载DOMContentLoaded之后运行,所以您可能需要指定。