Chrome Extension: Capture URL of the newest tab opened

5,168 views
Skip to first unread message

Gabriel Barreras

unread,
Jul 15, 2014, 2:39:32 PM7/15/14
to chromium-...@chromium.org

My purpose is to capture the URL of the newest tab opened using a content script. I want to capture the URL of the new tab only if it was opened from a webpage which matches the pattern specified in the manifest.

My first though was to use this code in content_script.js, so it would be executed every time current page matches the pattern:

chrome.tabs.onCreated.addListener(function(tab) {
    /* tabs.onUpdated */
    chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){ 
        status = changeInfo.status;
    });
    while (status != 'complete'){
        chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){ 
            status = changeInfo.status;
        });
    }
    /* At this point, url tab is complete so I can capture it*/
});

The problem is content_script.js cannot access chrome.tabs object. Then I though that code should be onbackground.js. However, background.js does not know when the current webpage matches the pattern.

Any idea of doing this? Thank you!

PD: I have already published this question in StackOverflow without getting any solution.

Tom Coleman

unread,
Jul 15, 2014, 3:07:54 PM7/15/14
to Gabriel Barreras, chromium-...@chromium.org
For this reason I often send messages between background and content scripts.
The challenge I faced was having a context menu appear ( which is done in the background page ) and do stuff in the content script.

I would set aside an hour or two and add very simple code in the content and background scripts, then open the debuggers for each, and watch messages go back and forth.  After playing with it, hopefully you will have some ideas on how to talk back and forth.  It seems like the content script decides when to run, sends a request to the backend, the backend looks up some stuff ( state, config, prefs ), then sends back a response to the content script.

background script:

chrome.tabs.getSelected(null, function(tab) {

      chrome.tabs.sendMessage( tab.id, text, function( response ) {

content script:

      chrome.extension.sendRequest(
          {
              url: document.documentURI,
              command: 'onPageLoaded',
          }, function(response) {
            // now that the data has come back, ask backend for state
          });



Another option is to have the background script listen for network traffic

chrome.webRequest.onSendHeaders.addListener(
                  onSendHeaders_callback,
                  opt_filter_domains,
                  opt_extraInfoSpec);

there may be a challenge with figuring out which tab is having the traffic.  maybe you can send an event / message to each tab, asking it if it needs stuff.




--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/11035fb8-c581-47d2-adae-9b831cd87bd8%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Muzaffer Tolga Ozses

unread,
Jul 15, 2014, 3:49:11 PM7/15/14
to Gabriel Barreras, Chromium-extensions
Use messaging API.


--

Gabriel Barreras

unread,
Jul 19, 2014, 2:31:20 PM7/19/14
to chromium-...@chromium.org, gbarr...@gmail.com
I managed to do it in a similar way.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

Muzaffer Tolga Ozses

unread,
Jul 20, 2014, 2:36:41 AM7/20/14
to Gabriel Barreras, Chromium-extensions

Well? Care to share with the rest of the class?

To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
Message has been deleted

Gabriel Barreras

unread,
Jul 20, 2014, 8:24:51 AM7/20/14
to chromium-...@chromium.org, gbarr...@gmail.com
Actually what I tried to do is to detect when a new tab was opened using right-click.
So, from content_script.js I listen for node.oncontextmenu() events. There I called chrome.runtime.sendMessage() to send the message to the background.js and then chrome.extension.onMessage.addListener() to wait for the background.js warning of "new tab opened".

Meanwhile, background.js, which have started running since the extension was enabled, was listening for the content_script.js message by calling chrome.runtime.onMessage.addListener(). Then it will listen for a new tab to open, and it will tell the content_script.js when that happens:

    chrome.tabs.onCreated.addListener(function(tab) {
     chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
     chrome.tabs.sendMessage(tabs[0].id, {action: 'whatever'}, 
     function(response) {});  
     }); 
     });

I did not post the actual code cause it's too messy, and you would get confused.
I had a few typos, but groups.google did not allow me to edit so I removed and posted the message again, sorry. 

Gabriel Barreras

unread,
Aug 2, 2014, 8:03:33 PM8/2/14
to chromium-...@chromium.org
Hey dudes, did you understand my solution? Has it satiesfied you?
Reply all
Reply to author
Forward
0 new messages