Chrome Extension: chrome.tabs.executeScript not working

tldr picture tldr · Oct 25, 2014 · Viewed 9.7k times · Source

I'm writing a chrome extension, and want to execute a content script from my background script. The background script executes, but the content doesn't. Here are the relevant chunks of code:

manifest.json:

"background": {
    "scripts": ["js/app/background.js"],
    "persistent": true
},

"permissions": [
    "identity",
    "tabs",
    "activeTab",
    "*://*/*"
]

background.js:

console.log('background')
chrome.tabs.executeScript(null, {file: "content.js"})

content.js:

console.log('content')

When I inspect element, the console has background logged on it, but not content. The regular console also has nothing logged on it. What am I doing wrong?

Answer

Charles Clayton picture Charles Clayton · Apr 22, 2015

In your background.js wrap your chrome.tabs.executeScript in this:

chrome.tabs.onUpdated.addListener(function(tab) {

    chrome.tabs.executeScript({
        file: '/scripts/runsOnPageLoad.js'
    }); 

});