-1

I am building a Record/Playback chrome extension where the user can click a browser action to start the recording of his actions. This will insert a Content Script into the current active tab and start sending the actions to the background script. The challenging part is when the user clicks a link which opens a new window/tab and a script, the background.js keeps a track of new tabs created using chrome.tabs.onCreated.addListener and inserts the content script into the newly created tab if its Opener Tab ID, openerTabId is same as the ID of the starting tab.

The whole methods seems a bit clunky to me. Is this the best way?

2 Answers2

1

Programmatic injection discussed that inserting code into a page programmatically is useful when your JavaScript or CSS code shouldn't be injected into every single page that matches the pattern — for example, if you want a script to run only when the user clicks a browser action's icon.

To insert code into a page, your extension must have cross-origin permissions for the page. It also must be able to use the chrome.tabs module. Once you have permissions set up, you can inject JavaScript into a page by calling tabs.executeScript. And as stated too, instead of inserting code directly, it's usually done by putting the code in a file like this:

chrome.tabs.executeScript(null, {file: "content_script.js"});

Other than that, this SO post - Chrome extension: create tab then inject content script into it might help. :)

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22
1

I think you should add content_script.js to and check that tab is new tab with

if(history.length == 1){
    // TODO: something
}
hong4rc
  • 3,999
  • 4
  • 21
  • 40