Help get this topic noticed by sharing it on Twitter, Facebook, or email.

How do i get ActiveTab (appAPI.tabs.getActive) in IE and Safari?

In the documentation says appAPI.tabs.getActive() only is supported by chrome and firefox.
1 person has
this question
+1
Reply
  • Hello Eneida,

    I am assuming you need the active tab information in the background scope, since in the extension scope you can simply use the appAPI.getTabId method. Hence, you can work around the issue and get the active tab id for all browsers by the background scope requesting the active tab from the extension scope, as follows:

    background.js:
    appAPI.ready(function($) {
    var newTabId=null;
    appAPI.message.addListener(function(msg) {
    if (msg.type === 'getActiveTabId')
    newTabId = msg.tabId;
    });
    appAPI.message.toActiveTab({
    type:'getActiveTabId'
    });
    });


    extension.js:
    appAPI.ready(function($) {
    appAPI.message.addListener(function(msg) {
    if (msg.type === 'getActiveTabId')
    appAPI.message.toBackground({
    type:'getActiveTabId',
    tabId:appAPI.getTabId()
    });
    });
    });
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated indifferent, undecided, unconcerned happy, confident, thankful, excited kidding, amused, unsure, silly

  • Thanks for the answer Shlomo,

    This works in a part, because i get the id in number, and what i want is get the url of active tab (http://someurl), it is possible?

    Thanks
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated indifferent, undecided, unconcerned happy, confident, thankful, excited kidding, amused, unsure, silly

  • Hello Eneida,

    Yes, simply change the tabId for the url, as follows:

    background.js:
    appAPI.ready(function($) {
    var newTabUrl=null;
    appAPI.message.addListener(function(msg) {
    if (msg.type === 'getActiveTabUrl')
    newTabUrl= msg.tabUrl;
    });
    appAPI.message.toActiveTab({
    type:'getActiveTabUrl'
    });
    });


    extension.js:
    appAPI.ready(function($) {
    appAPI.message.addListener(function(msg) {
    if (msg.type === 'getActiveTabUrl')
    appAPI.message.toBackground({
    type:'getActiveTabUrl',
    tabUrl:window.location.href
    });
    });
    });
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned happy, confident, thankful, excited kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • Hi Shlomo,

    I got a question about this workaround for safari.

    It works well, if the tab has finished loading or appAPI.ready block is finished.
    But when I try to get info from active tab while it's loading a heavyweight website the msg listener in extension.js can't answer...

    Do you have an idea for this problem?

    With kind regards
    Andreas
    • view 2 more comments
    • Shlomo (Official Rep) March 26, 2015 12:28
      Try placing the message to the active tab in something like appAPI.tabs.onTabSelectionChanged.
    • Do you mean I should implement the call to active tab in "onTabSelectionChanged" like this:

      appAPI.tabs.onTabSelectionChanged(function(){
      appAPI.message.toActiveTab({ type:'getActiveTabUrl' });
      });

      This makes no difference, because evaluation of the following snippet leads to "ReferenceError: appAPI is not defined"

      appAPI.dom.onDocumentStart.addJS({
      js:
      'console.log("getActiveTabUrl called in extension.js");' +
      'appAPI.message.addListener(function(msg){' +
      'if (msg.type === "getActiveTabUrl")' +
      'appAPI.message.toBackground({' +
      'type:"getActiveTabUrl", ' +
      'tabUrl:window.location.href' +
      '})' +
      '});'

      });
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. indifferent, undecided, unconcerned happy, confident, thankful, excited kidding, amused, unsure, silly sad, anxious, confused, frustrated

  • I've implemented a workaround for this case: background code send message to active tab and if it doesn't respond after 1 second I display a hint to the user.

    background.js

    appAPI.ready(function($) {
    var listenerIntId = null;
    listenerIntId = appAPI.message.addListener(function(msg) {
    if (msg.type=== "getActiveTabUrl"){

    // removes the listener
    appAPI.message.removeListener(listenerIntId);
    listenerIntId = null;

    console("tabUrl: " + msg.tabUrl);
    }
    });

    // check if callback function has been called
    appAPI.setTimeout(function() {
    if(null != listenerIntId){
    // display hint to user, e.g.
    console("website didn't finish loading. Please try again");
    }else{
    // everything worked fine
    }
    }, 1 * 1000);
    });


    extension.js

    appAPI.ready(function($) {
    appAPI.message.addListener(function(msg) {
    if (msg.type === 'getActiveTabUrl')
    appAPI.message.toBackground({
    type:'getActiveTabUrl',
    tabUrl:window.location.href
    });
    });
    });
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated indifferent, undecided, unconcerned happy, confident, thankful, excited kidding, amused, unsure, silly