Welcome guest, is this your first visit? Create Account now to join.
Results 1 to 5 of 5

This is a discussion on How do I get the URL of the current tab? (popup extension) within the Plugins Development section, part of the Chrome Plugins category: Hi guys, I'm trying to submit a form with the current web page I am on. I really don't know ...


  1. #1
    sois is offline Junior Member
    Join Date
    Mar 2011
    Posts
    3

    Thumbs up How do I get the URL of the current tab? (popup extension)

    Hi guys, I'm trying to submit a form with the current web page I am on. I really don't know how to get the current URL to pass along to the form. This is driving me crazy!

    Current code is very simple:

    Code:
    <script>
    function SubmitForm() {
      document.getElementById('url').value = WHAT GOES HERE?
      document.forms['myform'].submit();
    }
    </script>
    
    <body onload="SubmitForm()">
    <form id="myform" action="PageSubmittingTo" method="post">
    <input type="text" name="url" id="url">
    <input type="submit" name="s">
    </form>
    </body>
    Last edited by sois; 03-08-2011 at 10:25 PM.

  2. #2
    sois is offline Junior Member
    Join Date
    Mar 2011
    Posts
    3

    Default

    Code:
    <script>
    function SubmitForm() {
      chrome.tabs.getSelected(null, function(tab) {
        document.getElementById('url').value = tab.url;
      });
      document.forms['myform'].submit();
    }
    </script>
    I figured it out, you have to assign the value inside of the callback. I don't know what a callback is but I"m glad I figured it out! Hope this helps someone.

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

    Default

    Im not the smartest cookie, but a callback is....
    A function that gets called after your request is finished.
    In this case your asked for the selected tab, when chrome figures out which one it is it calls your callback.
    One thing about callbacks....you never know when its going to get called. In your code you seem to be assuming the callback will happen before the line document.forms['myform'].submit()....you prolly shouldnt be doing that and instead include that line in your callback.

  4. #4
    sois is offline Junior Member
    Join Date
    Mar 2011
    Posts
    3

    Default

    Code:
    <script>
    function SubmitForm() {
      chrome.tabs.getSelected(null, function(tab) {
        document.getElementById('url').value = tab.url;
        document.forms['myform'].submit();
      });
    }
    </script>
    Thanks! That actually makes more sense and works great!
    I was getting some weird side effect by not having it within the callback.

    The request/callback terminology is tripping me up, but hopefully with baby steps of progress like this, I will learn. To me it still looks like a function inside of a parameter area of another function and that makes zero sense to my brain. Thank you PAEz!

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

    Default

    I was getting some weird side effect by not having it within the callback.
    This side effect is whats called a race condition.

    Maybe it would help you visualize it better if you wrote it like this.....
    Code:
    <script>
    function callbackForSubmitForm(tab) {
        document.getElementById('url').value = tab.url;
        document.forms['myform'].submit();
      }
    
    function SubmitForm() {
      chrome.tabs.getSelected(null, callbackForSubmitForm);
    }
    </script>
    ...thats perfectly valid code and might help with visualizing whats going on.
    Notice that in the chrome.tabs.getSelected the callbackForSubmitForm doesnt have the () , thats because were referencing that function not executing it (hope I got that right, I should, Waha's giving me two reminders on it so far ).
    The way the function is being used for the callback in your code is called an anonymous function.

    Well I hope that didnt just confuse more

Similar Threads

  1. Edit Current URL
    By AlphaWolf in forum Plugins Development
    Replies: 1
    Last Post: 11-16-2010, 10:05 PM
  2. How to make extension popup sticky
    By TheDev in forum Plugins Development
    Replies: 2
    Last Post: 06-11-2010, 04:49 PM
  3. Current Dev Version : 5.0.375.28
    By sulasno in forum Chrome Troubleshooting
    Replies: 1
    Last Post: 05-01-2010, 04:56 PM
  4. Open current page in firefox
    By bartvdc in forum Chrome Plugins
    Replies: 10
    Last Post: 11-03-2009, 02:38 PM
  5. Getting current url?
    By GA-B1-G5 in forum Plugins Troubleshooting
    Replies: 2
    Last Post: 11-01-2009, 09:30 PM

Posting Permissions

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