0

Hello all i want to load the script whether or not user clicks on my extension icon This is my extension it works great but i want it to work without making the user click on the icon to load the scripts ..

Here is the code .

  {
   "name": "Injecta",
    "version": "0.0.1",
     "manifest_version": 2,
      "description": "Injecting stuff",

        "background":
         {
         "scripts": ["jquery.js","background.js"]
              },
            "browser_action": {
             "default_title": "Inject!"
              },
              "permissions": [
              "https://*/*",
               "http://*/*",
                "tabs"
                  ]
                  }

This is my background.js

     chrome.browserAction.onClicked.addListener(function (tab) {
      chrome.tabs.executeScript({
      file: 'jquery.js'
           });
         chrome.tabs.executeScript({
            file: 'inject.js'
             });
                });

i just want the extension to load all the scripts with the page load. currently user has to click on the icon to load the scripts..

Lisa
  • 33
  • 1
  • 2
  • 5

2 Answers2

1

What executeScript does is basically creating a Content Script dynamically. This is called Programmatic Injection.

An alternative method of working with content scripts is specifying them in the manifest. This achieves exactly what you're asking: content scripts are executed automatically when the page is loaded.

"content_scripts" : [
   {
     "js": ["jquery.js", "inject.js"],
     "matches": ["*://*/*"]
   }
],

Adjust the matches parameter to only include match patterns for pages you want it to run on.

Make sure to check out the documentation of run_at parameter if you need to fine-tune when injection happens.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • Possibly, but that should be a new question. It also seems like a pretty basic question - make sure you [do your research first](http://stackoverflow.com/help/how-to-ask). – Xan May 27 '16 at 14:24
-2

if (typeof jQuery === 'undefined') {}

William B
  • 1,411
  • 8
  • 10