Results 1 to 4 of 4

This is a discussion on Reliably grabbing Omnibox URL of current tab after tab switch within the Plugins Development section, part of the Chrome Plugins category: Hello, I'm writing a plugin to shorten URLs. In fact, I've already written and published it. But, it has one ...


  1. #1
    commorancy is offline Junior Member
    Join Date
    May 2010
    Posts
    3

    Question Reliably grabbing Omnibox URL of current tab after tab switch

    Hello,

    I'm writing a plugin to shorten URLs. In fact, I've already written and published it. But, it has one bug that I'm fighting and need some help with this issue.

    The plugin is working great except for one issue with tabs and the current URL in the Omnibox. Let me explain how the plugin works.

    I have an icon sitting in the Omnibox (Page action) that acts on specific URL types (hence the need to hide and show it at specific times) rather than using a Browser action.

    So, the plugin works when acting on an individual tab. But, when switching tabs, I use chrome.tabs.getSelected() to determine the currently selected tab so I can grab the focused tab.url. For some reason, Chrome seems to either cache or not immediately return the current tab's Omnibox URL after a tab switch. So, after switching tabs and I click my icon once, I get the previous tab's URL. If I close my tool and click the icon a second time, it then pulls in the currently focused and correct tab's URL.

    This appears to be some kind of synchronization problem when switching between tabs.

    For example, I'm using:

    chrome.pageAction.onClicked.addListener(function(reply) {
    chrome.tabs.getSelected(null, function(tab) {
    updateTabData(tab);
    })
    chrome.windows.create({url: "chrome-extension://heaacngjhlnimlemljdooglmlemmhkhh/hotlinkmanager.html?" + urlbar1, width: 600, height: 600});
    })


    And updateTabData simply fills the global variable urlbar1 with contents from tab.url for use throughout the app. The variable urlbar1 should always be filled with the current tab's Omnibox URL and that's the problem. The code works fine when using one tab and also when you refresh or update to a new page in any tab. It's only a problem when you click to a new tab and want the existing URL without refreshing that tab.

    I've tried everything I can think of to get this synchronization issue solved when moving between tabs. I'm fairly new to Chrome's API, so there may be a lot of nuances I'm missing with this stuff.

    Anyway, anyone have any ideas on how I can reliably pull the correct tab.url from the currently focused tab after a switch, but without a page refresh?

    Thanks.

    --
    Brian

  2. #2
    PAEz's Avatar
    PAEz is offline Moderator
    Join Date
    Aug 2009
    Location
    Australia
    Posts
    656

    Default

    Your updating urlbar1 in an chrome.tabs.onUpdated.addListener but this only gets fired when a tabs url changes, not when the selected tab changes. You dont need to do this, when chrome.pageAction.onClicked.addListener is fired it gives you the url of the tab that the page action is connected to in your case that would have been reply.url.
    Heres what the code should look like.....
    Code:
      function checkForValidUrl(tabId, changeInfo, tab) {
        // If the letter 'g' is found in the tab's URL...
          urltestval = tab.url.indexOf('chrome-extension');
          //alert("test value: " + urltestval);
        if (tab.url.indexOf('http://') == 0 && tab.url.indexOf('chrome-extension') == -1) {
          // ... show the page action.
          chrome.pageAction.show(tabId);
      ///    urlbar1 = tab.url;
          // alert(tab.url);
        }
        else
          chrome.pageAction.hide(tabId);
      }
    
      // Listen for any changes to the URL of any tab.
      chrome.tabs.onUpdated.addListener(checkForValidUrl);
    
      chrome.pageAction.onClicked.addListener(function(tab) {
    chrome.windows.create({url: chrome.extension.getURL("hotlinkmanager.html?") + tab.url, width: 600, height: 600});
    })
    Also, why not change the hotlinkmanager page so they dont have to click the button (I really am that lazy). Just add shortenURL(); to the function setUrl so it looks like this....
    Code:
    function setUrl() {
        var boom_url = document.getElementById("boom_url");
        boom_url.value = main_url;
        // document.getElementById("boom_send_email").disabled = true;
        shortenURL();
      }
    Last edited by PAEz; 05-01-2010 at 07:31 AM.

  3. #3
    commorancy is offline Junior Member
    Join Date
    May 2010
    Posts
    3

    Default

    I thought I had tried this code in one my many debug sessions and couldn't get it to work. Perhaps I had a paren out of place or some other typo. What you say makes sense, though.

    As far as clicking 'Make Hotlink' button, I do this intentionally. It's intentional because we store the short URL to long URL linkage in our database and would prefer to avoid extraneous entries to the database. So, the button serves as a secondary confirmation by the user to go ahead and create the hotlink. It also allows the user to bring up the panel and use it for other hotlinks instead of the one in the Omnibox. If it's automatic, then it will create one immediately on the way in which may be discarded if that's not the one the user wanted. Hence, unnecessary pollution.

    I plan to implement options in 1.1, so I will take your idea as a suggestion for when I implement options. Then, you can turn on the option to automatically create hotlinks. I'll leave them off on the default installation, though.

    We haven't decided on a database pruning policy yet, but if the table gets big enough, we may have to start that process. The button serves to help keep the pollution down.

    I would like to point out that by bringing up the panel before creating the short link, this also allows the user to edit the long URL prior to creating the short URL. Some long URLs may need editing to eliminate unnecessary session or identifying information (i.e., email addresses) and still function properly. It may not happen that often, but those concerned about security or accidentally revealing an email address or other personal info in the long URL could find this feature useful. This also means that when storing the linkage in the database, we do not do modify the long URL in any way. It's up to the user to make that choice in the panel.

    Thanks.

    --
    Brian
    Last edited by commorancy; 05-01-2010 at 02:00 PM.

  4. #4
    commorancy is offline Junior Member
    Join Date
    May 2010
    Posts
    3

    Default

    Paez,

    Thanks for the code snippet. It's working perfectly. I am beginning work on version 1.1 with options and a few other features and will hopefully have it done soon.

    Thanks for your help.

    --
    Brian

Similar Threads

  1. Switch between install crx themes
    By SifJar in forum Chrome Talk
    Replies: 5
    Last Post: 02-19-2011, 04:52 PM
  2. Single button to close current tab
    By stjamespark in forum Chrome Plugins
    Replies: 6
    Last Post: 02-14-2010, 12:27 PM
  3. Open current page in firefox
    By bartvdc in forum Chrome Plugins
    Replies: 10
    Last Post: 11-03-2009, 01:38 PM
  4. Getting current url?
    By GA-B1-G5 in forum Plugins Troubleshooting
    Replies: 2
    Last Post: 11-01-2009, 08:30 PM
  5. Check current version of plugins
    By sulasno in forum Plugins Troubleshooting
    Replies: 0
    Last Post: 10-04-2009, 03:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •