8

I am looking for a way to determine a given tab's opener (parent tab) within a Google Chrome extension.

I've looked at the documention for Tab but there doesn't really seem to be anything that would yield this information. http://code.google.com/chrome/extensions/tabs.html

I've tried injecting this content script into pages (thinking I could pass the value to my background page):

alert(window.opener);

.. but it just yields null.

The best thing I've come up with so far is to keep track of the currently focused tab, and whenever a new tab is created, just assume that the focused tab is the opener/parent of the new tab. I believe this would de facto identify the parent tab correctly most of the time since background tabs rarely (are allowed to) open new pages. However, it seems kludgey and potentially inaccurate at times -- for example, if another extension opened a new tab, this method may misidentify the new tab's opener.

joelpt
  • 4,675
  • 2
  • 29
  • 28
  • May I ask what kind of extension are you/were you building? I'm looking for a very simple one that would have this exact functionality very simply: Whenever I am in doubt of 'where I come from' or what opened what, I would like to know the parent of the current tab. Can your extension by any chance answer that question for me? Or even if not, maybe you know of a similar extension? – Wizek Jan 09 '16 at 11:31

4 Answers4

7

Update: it is now possible to reliably determine a tab's opener tab within a Chrome extension natively using the newly added webNavigation API, and specifically by hooking the onCreatedNavigationTarget event.

https://code.google.com/chrome/extensions/trunk/webNavigation.html

joelpt
  • 4,675
  • 2
  • 29
  • 28
2

Chrome has added an experimental extension API which can accomplish this -- specifically webNavigation.onBeforeRetarget. http://code.google.com/chrome/extensions/experimental.webNavigation.html

However since this is still experimental (not usable in Chrome stable versions or releasable on the Chrome Web Store) I have ended up using another approach.

Basically:

In content_script.js:

chrome.extension.sendRequest({
    request: {
        op: "pageLoadStarted", 
        url: document.location.href, 
        referrer: document.referrer
    }
}); 

In background.html:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { 
    console.log('onRequest: ' + JSON.stringify(request)); 
    console.log(JSON.stringify(sender)); 
  }); 

This approach allows me to obtain the referrer of a tab, which I can then correlate with an existing tab's url. This isn't always a one-to-one mapping, so I do some additional magic such as preferring the currently selected tab as the opener if its url matches the new tab's referrer.

This really is just a hack to approximate the functionality that would be provided more simply and accurately by webNavigation.onBeforeRetarget or window.opener.

joelpt
  • 4,675
  • 2
  • 29
  • 28
1

Further investigation has revealed that onCreatedNavigationTarget() does not always fire when you think it would to indicate an opener-opened relationship.

An additional hint can sometimes be found in the Tab object returned by chrome.tabs.onCreated/onUpdated in the .openerTabId parameter.

A comprehensive solution will probably have to rely on multiple of the methods described in these answers.

joelpt
  • 4,675
  • 2
  • 29
  • 28
1
port.onMessage.addListener(
     function(msg) {
         var tabid = port.sender.tab.openerTabId;
         console.log("Received message from tab that was opened by tab id : " + tabid);
         // reliably shows the tab id of the tab that opened
         // the tab sending the message
     }); 
Cris Stringfellow
  • 3,714
  • 26
  • 48