In background script, listen for newly opened tab


In my background script, or anywhere but a content script, I am trying to listen for the active tab, or a newly opened tab.

I have this, but this is apparently incorrect:

<a href="https://i.stack.imgur.com/42h03.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/42h03.png" data-original="https://i.stack.imgur.com/42h03.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

What I am trying to do is to inject a content-script into the active tab. I don't want my content script to run for every tab/window, just for select tabs.

Does anyone know how to inject a content script for certain tabs? I can't figure it out. I assume the best way to inject content scripts is from the background script.


If you want to run a code in already activated tab (<a href="https://developer.chrome.com/extensions/tabs#method-query" rel="nofollow">chrome.tabs.query</a>):

chrome.tabs.query({active: true, currentWindow: true}, function(foundTabs) { const activeTabId = foundTabs[0].id; chrome.tabs.executeScript(activeTabId, { file: 'inject.js' }); })

In case you want to inject a file into every newly activated tab (<a href="https://developer.chrome.com/extensions/tabs#event-onActivated" rel="nofollow">chrome.tabs.onActivated</a>):

chrome.tabs.onActivated.addListener(function(activeInfo) { chrome.tabs.executeScript(activeInfo.tabId, { file: 'inject.js' }); });

Be sure to check if you have injected the file already, in order to prevent multiple injections.

For both cases, permissions should include tabs.


some chrome API we cant access in content script in that case do that code in background.js and do according action from background js. if you want to do some action in content script in that case you need to communicate using send message


I was missing the:


permission in manifest.json. After adding that permission, I now have:

chrome.webNavigation.onDOMContentLoaded.addListener(function (details) { const tabId = details.tabId; chrome.tabs.executeScript(tabId, { file: 'inject.js' }); });

now it works.


  • .NET get process ID of my application in C#
  • Activate Monotouch
  • How to make a npm command, that executes two commands in the console (one after another)?
  • Are there any libraries for Python to simulate keyboard action?
  • python: forcing relative imports to search from script file
  • Python 3.2.2, error(scripts to exe)
  • cell spacing in div table
  • XSLT foreach repeating nodes to flat
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • jQuery: add elements until a particular height is reached
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Combining two different ActiveRecord collections into one
  • Is playing sound in Javascript performance heavy?
  • How to use carriage return with multiple line?
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Test if a set exists before trying to drop it
  • Chrome doesn't support silverlight anymore? How to solve this?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to check if every primary key value is being referenced as foreign key in another table
  • Javascript convert timezone issue
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Display Images one by one with next and previous functionality
  • jquery mobile loadPage not working
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • SQL merge duplicate rows and join values that are different
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How to stop GridView from loading again when I press back button?
  • How can I remove ASP.NET Designer.cs files?
  • sending mail using smtp is too slow
  • Append folder name and increment by 1 using batch script
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal