[英]Chrome Extension: chrome.tabs.executeScript not working
I'm writing a chrome extension, and want to execute a content script from my background script. 我正在编写chrome扩展,并希望从我的后台脚本执行内容脚本。 The background script executes, but the content doesn't.
后台脚本执行,但内容不执行。 Here are the relevant chunks of code:
以下是相关的代码块:
manifest.json
: manifest.json
:
"background": {
"scripts": ["js/app/background.js"],
"persistent": true
},
"permissions": [
"identity",
"tabs",
"activeTab",
"*://*/*"
]
background.js
: background.js
:
console.log('background')
chrome.tabs.executeScript(null, {file: "content.js"})
content.js
: content.js
:
console.log('content')
When I inspect element, the console has background
logged on it, but not content
. 当我检查元素时,控制台上有登录
background
,但不是content
。 The regular console also has nothing logged on it. 常规控制台也没有任何记录。 What am I doing wrong?
我究竟做错了什么?
In your background.js
wrap your chrome.tabs.executeScript
in this: 在你的
background.js
包装你的chrome.tabs.executeScript
:
chrome.tabs.onUpdated.addListener(function(tab) {
chrome.tabs.executeScript({
file: '/scripts/runsOnPageLoad.js'
});
});
我无法使用编程注入工作,所以我刚刚在manifest.json的content_scripts
字段中指定了它( https://developer.chrome.com/extensions/content_scripts#registration )
you should add permision "activeTab". 你应该添加permision“activeTab”。 more information https://developer.chrome.com/extensions/content_scripts#pi
更多信息https://developer.chrome.com/extensions/content_scripts#pi
It must be as mentioned below: 必须如下所述:
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
chrome.tabs.executeScript({
code:"alert('Any Javascript code comes here !');"
});
}); });
Note: can execute in background script, Don't forgot to allow necessary permissions in manifest.json 注意:可以在后台脚本中执行,不要忘记在manifest.json中允许必要的权限
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.