anirudh
anirudh

Reputation: 433

Running Script after Injecting from Chrome Extension

I'm trying to inject a script into the webpage when a button in the extension option menu is clicked. Injection -

document.getElementById("button_").addEventListener("click", function(){
  alert("Injecting") ;
  chrome.tabs.executeScript({
          file: 'script.js'
        });
});

However the function that I'm trying to run in script.js , foo() is not running. I'm trying to run it via-

window.onload= function(){
  foo() ;
}

However this does not work . How do I get foo() to run?

Note- It has to run as an injected script

Upvotes: 0

Views: 436

Answers (2)

anirudh
anirudh

Reputation: 433

Sending message from a background script to a content script, then to a injected script gives an excellent guide on dealing with content scripts and communicating with them.

Upvotes: 0

Balsa Lazarevic
Balsa Lazarevic

Reputation: 83

Instead of using the listener and running "chrome.tabs.executeScript" on click, use the "content_scripts" property in manifest file and inside the script create a listener that will run your function.

Upvotes: 1

Related Questions