[英]Chrome extension inject js
I want to create a new chrome extension but it don't work. 我想创建一个新的chrome扩展程序,但是它不起作用。 I want to inject a js file into web page (all web page,not only one.If i push the chrome icon on google the script must execute,if i push the icon on facebook it must execute ect.) 我想将js文件注入到网页中(所有网页,不仅是一个网页。如果我按google上的chrome图标,则脚本必须执行;如果我按facebook上的图标,则必须执行ect。)
this is background.js 这是background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript({
null,{file: "backgrounds.js"} });
});
this is backgrounds.js 这是backgrounds.js
document.body.innerHTML="display div elem with style and id";
this is manifest.json 这是manifest.json
{
"name": "MyExt",
"description": "an extension,what else?",
"version": "1.0",
"permissions": [
"activeTab"
],
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["background.js"]
}
],
"browser_action": {
"default_title": "myExt"
},
"manifest_version": 2
}
what i wrong? 我错了吗? I'm on windows 8.1 Update 1 with chrome last version 我在Windows 8.1 Update 1中使用chrome最新版本
Your manifest is wrong: you should set background.js
as your background script: 您的清单错误:您应该将background.js
设置为背景脚本:
"background" : { "scripts" : [ "background.js" ] },
and remove the "content_scripts"
section. 并删除"content_scripts"
部分。
The "activeTab"
permission means that you don't need to specify host permissions to inject in the current tab upon browser action click, so no other permissions are needed. "activeTab"
权限意味着您无需指定主机权限即可在单击浏览器操作时注入当前选项卡,因此不需要其他权限。
The tabId
argument is optional, you can just drop it instead of passing null
. tabId
参数是可选的,您可以删除它而不是传递null
。 And your invocation is wrong (you're wrapping two arguments in a single object). 而且您的调用是错误的(将两个参数包装在一个对象中)。 Here's the correct way: 这是正确的方法:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript({file: "backgrounds.js"});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.