Is it possible to determine a tab's opener within a Google Chrome extension?

烂漫一生 提交于 2019-12-18 13:28:26

问题


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.


回答1:


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




回答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.




回答3:


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.




回答4:


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
     }); 


来源:https://stackoverflow.com/questions/3124543/is-it-possible-to-determine-a-tabs-opener-within-a-google-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!