简体   繁体   中英

Chrome extension inject js

I want to create a new chrome extension but it don't work. 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.)

this is background.js

chrome.browserAction.onClicked.addListener(function(tab) {

chrome.tabs.executeScript({

    null,{file: "backgrounds.js"} });
});

this is backgrounds.js

document.body.innerHTML="display div elem with style and id";

this is 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

Your manifest is wrong: you should set background.js as your background script:

"background" : { "scripts" : [ "background.js" ] },

and remove the "content_scripts" section.


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.


The tabId argument is optional, you can just drop it instead of passing 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"});
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM