Search code examples
google-chrome-extension

How to detect tabs change URLs or tabs create on Google Chrome Extension?


I have a question about writing Google Chrome Extension. My goal now is to detect that if a tab is created or a URL of a tab has been changed.

Practically, I want to insert a dictionary .js from a link online to any webpage on Chrome, and the script will run as background.html. For example, if you open the browser and go to your homepage, it will run the script to insert dictionary.js into that page. When a new tab is created or a new page is open, it will run the script too. And when people change tab's url, it will run the script too. How do I detect if the tab changes in such situations? Ok, here is my ... code, i guess, to explain that.

chrome.someFunctionThatDetectTheSituationsAbove(function() {
    insertDictionaryScript();//I'd love to have the script of detection, not the insertDictionaryScript();
}

I would appreciate for any idea. Thank you. :P.

[x]


Solution

  • What you are describing is called a content script. You don't need any coding for that, just make a declaration in the manifest file. More about content scripts here.