use*_*206 12 javascript google-chrome-extension
我正在尝试将消息从后台页面发送到内容脚本,然后将该内容脚本中的消息发送到注入的脚本.我试过这个,但它没有用.
这是我的代码的样子.
的manifest.json
{
"manifest_version": 2,
"name": "NAME",
"description": ":D",
"version": "0.0",
"permissions": [
"tabs","<all_urls>"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content_script.js"]
}
],
"web_accessible_resources": [
"injected.js"
],
"background":{
"scripts":["background.js"]
}
}
Run Code Online (Sandbox Code Playgroud)
background.js
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response){});
});
Run Code Online (Sandbox Code Playgroud)
content_script.js
var s = document.createElement('script');
s.src = chrome.extension.getURL('injected.js');
s.onload = function(){
this.parentNode.removeChild(this);
};
(document.head||document.documentElement).appendChild(s);
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
document.dispatchEvent(new CustomEvent('Buffer2Remote', {todo: "LOL"}));
});
Run Code Online (Sandbox Code Playgroud)
injected.js
document.addEventListener('Buffer2Remote', function(e){
alert(e.todo);
});
Run Code Online (Sandbox Code Playgroud)
消息发送在第一部分background - > content_script中不起作用.我的代码有什么问题吗?
Xan*_*Xan 44
由于内容脚本的注入方式,您的脚本不起作用.
当您(重新)加载扩展程序时,与某些人的预期相反,Chrome 不会将内容脚本注入到与清单中的模式匹配的现有选项卡中.只有在加载扩展后,任何导航都会检查URL以进行匹配,并将注入代码.
那么,时间表:
chrome://extensions/页面,你无论如何都不能注入)1 - 如果您重新加载扩展程序,也会发生这种情况.如果注入了内容脚本,它将继续处理其事件/不会被卸载,但无法再与扩展进行通信.(详情见最后的附录)
解决方案1:您可以先向选项卡询问您是否已准备好发送消息,并在静默时以编程方式注入脚本.考虑:
// Background
function ensureSendMessage(tabId, message, callback){
chrome.tabs.sendMessage(tabId, {ping: true}, function(response){
if(response && response.pong) { // Content script ready
chrome.tabs.sendMessage(tabId, message, callback);
} else { // No listener on the other end
chrome.tabs.executeScript(tabId, {file: "content_script.js"}, function(){
if(chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
throw Error("Unable to inject script into tab " + tabId);
}
// OK, now it's injected and ready
chrome.tabs.sendMessage(tabId, message, callback);
});
}
});
}
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
ensureSendMessage(tabs[0].id, {greeting: "hello"});
});
Run Code Online (Sandbox Code Playgroud)
和
// Content script
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if(request.ping) { sendResponse({pong: true}); return; }
/* Content script action */
});
Run Code Online (Sandbox Code Playgroud)
解决方案2:始终注入脚本,但要确保它只执行一次.
// Background
function ensureSendMessage(tabId, message, callback){
chrome.tabs.executeScript(tabId, {file: "content_script.js"}, function(){
if(chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
throw Error("Unable to inject script into tab " + tabId);
}
// OK, now it's injected and ready
chrome.tabs.sendMessage(tabId, message, callback);
});
}
Run Code Online (Sandbox Code Playgroud)
和
// Content script
var injected;
if(!injected){
injected = true;
/* your toplevel code */
}
Run Code Online (Sandbox Code Playgroud)
这更简单,但在扩展重新加载方面存在复杂性.重新加载扩展后,旧脚本仍然存在1,但它不再是"你的"上下文 - 因此injected将是未定义的.注意可能两次执行脚本的副作用.
解决方案3:在初始化时不加选择地注入内容脚本.如果可以安全地运行相同的内容脚本两次,或者在页面完全加载后运行它,则这样做是安全的.
chrome.tabs.query({}, function(tabs) {
for(var i in tabs) {
// Filter by url if needed; that would require "tabs" permission
// Note that injection will simply fail for tabs that you don't have permissions for
chrome.tabs.executeScript(tabs[i].id, {file: "content_script.js"}, function() {
// Now you can use normal messaging
});
}
});
Run Code Online (Sandbox Code Playgroud)
我还怀疑你希望它在一些动作上运行,而不是在扩展加载上运行.例如,您可以使用浏览器操作并将代码包装在chrome.browserAction.onClicked侦听器中.
当重新加载扩展程序时,人们会希望Chrome清理所有内容脚本.但显然事实并非如此; 内容脚本的侦听器未被禁用.但是,任何具有父扩展的消息都将失败.这应该被视为一个错误,并且可能在某些时候被修复.我要打电话给这个州"孤儿"
这在以下两种情况中都不是问题:
但是,如果情况并非如此,那么您就会遇到一个问题:内容脚本可能正在执行某些操作,但是会失败或干扰另一个非孤立的实例.
解决方法是:
代码,内容脚本:
function heartbeat(success, failure) {
chrome.runtime.sendMessage({heartbeat: true}, function(reply){
if(chrome.runtime.lastError){
failure();
} else {
success();
}
});
}
function handler() {
heartbeat(
function(){ // hearbeat success
/* Do stuff */
},
function(){ // hearbeat failure
someEvent.removeListener(handler);
console.log("Goodbye, cruel world!");
}
);
}
someEvent.addListener(handler);
Run Code Online (Sandbox Code Playgroud)
背景脚本:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if(request.heartbeat) { sendResponse(request); return; }
/* ... */
});
Run Code Online (Sandbox Code Playgroud)