0

I’m building a browser action chrome extension where the user can click a button and as a result a new tab will be created with the linked-requested url.
In this new tab / webpage I’m filling in some info and automatically (using jQuery) click a button or A tag. As result in the same tab we navigate to a new page.
In this new page I would like to execute other file of code. I can do it if I add this file of code in the manifest using the {content_scripts}. Is there a way to do it dynamically?
In a debug mode I’m able to do it dynamically – and therefore I’m sure that the problem is the asynchronous between the first page click and the adding of the file of code to the 2nd page
please help

  • Can you post some code samples for what you've done so far? – QFDev Jun 13 '13 at 19:46
  • function fill_in(tabId){ chrome.tabs.executeScript(tabId, {file: "content_script.js"}, function(results){ chrome.tabs.executeScript(tabId, {file: "c_s1.js" }); }); chrome.tabs.create({ url: http://...}, function(tab){ fill_in(tab.id) }); After creating the tab and going to the webpage A content_script.js feels some info on A and click a button and move to page B. On page B I want to execute c_s1.js – user1069009 Jun 13 '13 at 20:26

1 Answers1

0

You can do this by passing messages between the content script and the background page and injecting script. See here for details:

http://developer.chrome.com/extensions/messaging.html

Your background page can essentially act as an operator between page 1 and page 2. When a button or anchor tag is clicked on page one, instead of, or in addition to, going directly to page 2, send a message to your background page, which then injects script directly into page 2 like this:

 chrome.browserAction.onClicked.addListener(function(tab) {
      chrome.tabs.executeScript(null, {code: <your code here>});
 });

See the "Programmatic Injection" section of this page:

https://developer.chrome.com/extensions/content_scripts.html

Jude Osborn
  • 1,788
  • 16
  • 24
  • The 1st command will fill in a form on a page and submitting it
    `chrome.tabs.executeScript(tabId, {file: "content_script.js"});` the 2nd command is `chrome.tabs.executeScript(tabId, {file: "c_s1.js"});` and it **should run on the 2nd page** (the one that is open as a result of the submitting of the first page) In debug mode all OK seen that it goes slow, without the debug, the 2nd command runs before the 2nd page is loaded. The problem here is a timing or synchronization or, I need a way to execute script each time that the tab’s page is changed and fully loaded.
    – user1069009 Jun 14 '13 at 01:03
  • Are you injecting code into page 2 that runs at window.onload? – Jude Osborn Jun 15 '13 at 23:23
  • I would like to create fully automated system to fill-in forms. One page form is working fine but, in case the 1st page is calling to 2nd page (all in the same tab) I need to be able to inject code into this 2nd page AFTER it has been fully loaded. This code will recognize the page and will fill in the necessary info. In debug mode all is ok but not in real production. – user1069009 Jun 17 '13 at 13:49