nermolov
nermolov

Reputation: 43

Chrome Extension run without popup or any visual elements

I am trying to write a chrome plugin for the first time and though I have basic javascript knowledge I do not know how to start my script.

My basic problem is that I do not want to use a popup or any other menu to start my script but I cannot figure out how to do this. My intention is that the extension runs commands on startup and then exits.

I am trying to fin out how I can start my Chrome extension can be started without doing it through any visual elements.

Here is what I have:

manifest.json

{
  "manifest_version":2,
  "name":"Audiotool control",
  "version":"1",
  "content_scripts":[
    {
      "matches":[
        "http://www.audiotool.com/*"
      ],
      "js":[
        "jquery.js",
        "content_script.js"
      ]
    }
  ],
  "offline_enabled":true,
  "permissions":[
    "tabs",
    "http://www.audiotool.com/*"
  ]
}

main.js

chrome.tabs.query({url: "http://www.audiotool.com/*", }, function(tabs) {
  chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
});

content_script.js (not really important but just in case)

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?);
    if (request.greeting == "hello")
      alert("message received")
      sendResponse({farewell: "function called"});
  });

//var e = $.Event("keydown", { keyCode: 32}); 
//$("body").trigger(e);

To reiterate, I am trying to fin out how I can start my Chrome extension can be started without doing it through any visual elements.

Upvotes: 1

Views: 2682

Answers (1)

Daniel Herr
Daniel Herr

Reputation: 20458

Well, main.js never runs because you have not specified it in the manifest. You probably want something like this in your manifest:

"background": {
  "scripts": ["main.js"],
  "persistent": false
},

After that, you would only fire main.js when the extension started up. You need to add a listener and perform operations inside it. Such as:

chrome.tabs.onUpdated.addListener(function() {
  //do stuff
});

Upvotes: 1

Related Questions