Get current chrome tab URL and insert to a form field

5,238 views
Skip to first unread message

Assaf

unread,
Nov 29, 2011, 11:59:05 AM11/29/11
to Chromium-extensions
Hi,
I have this very simple yet handy extension which opens an html popup
with a form in it.
The first field contained by default, the current url as shown in the
browser.
The problem was when the extension window closed, and the user moved
to another chrome tab and click the extension button again, the filed
had the old value of the previous url, and not the current tab url.
(It's a simple html page with some JavaScripts.)

The original code for the default value was initially like this:

<script type="text/javascript" language="JavaScript"><!--
document.write('<input ');
document.write(' type="text" ');
document.write(' name="website" ');
document.write(' size="50" ');
document.write(' value="' + document.URL + '">');
//--></script>
<noscript>
<input name="website" size="50" type="text">
</noscript>

Any help will do :-)

Boris Smus

unread,
Nov 30, 2011, 10:06:06 AM11/30/11
to Assaf, Chromium-extensions
On Tue, Nov 29, 2011 at 8:59 AM, Assaf <as...@quickwin.co.il> wrote:
Hi,
I have this very simple yet handy extension which opens an html popup
with a form in it.

Do you mean browser action popup or new page popup?
 
The first field contained by default, the current url as shown in the
browser.
The problem was when the extension  window closed, and the user moved
to another chrome tab and click the extension button again, the filed
had the old value of the previous url, and not the current tab url.
(It's a simple html page with some JavaScripts.)

If in the context of your extension (eg. in a browser action page), you can get the front-most tab by using the http://code.google.com/chrome/extensions/tabs.html#method-getSelected call and update your <input> whenever you create the browser action popup page.
 
The original code for the default value was initially like this:

<script type="text/javascript" language="JavaScript"><!--
document.write('<input ');
document.write('   type="text" ');
document.write('   name="website" ');
document.write('   size="50" ');
document.write('   value="' + document.URL + '">');
//--></script>
<noscript>
<input name="website" size="50" type="text">
</noscript>

Any help will do :-)

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.




--
Ask your next question on Stack Overflow, using the google-chrome-extension tag.

Assaf

unread,
Nov 30, 2011, 10:22:16 AM11/30/11
to Chromium-extensions
Browser action popup, i think. the pop up that pops under the
extension icon.
I've tried this one (gave also the tab permissions in the json file):

<script>
chrome.tabs.getSelected(null, function(tab) {
var url1 = tab.url;
});
document.write("<input type='text' name='website' size='50'
value="+url1+"'>");
</script>

but it didn't work...

> > To post to this group, send email to chromium-extensi...@chromium.org.


> > To unsubscribe from this group, send email to

> > chromium-extensions+unsubscr...@chromium.org.


> > For more options, visit this group at
> >http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.
>
> --
> Ask your next question on Stack

> Overflow<http://stackoverflow.com/questions/ask>,
> using the google-chrome-extension<http://stackoverflow.com/questions/tagged/google-chrome-extension>tag.

Zachary “Gamer_Z.” Yaro

unread,
Nov 30, 2011, 6:50:15 PM11/30/11
to Assaf, Chromium-extensions
That code will probably not work because, IIRC, chrome.* API calls are asynchronous, so url1 will not necessarily exist when you do the document.write.  Please correct me if I am mistaken.

—Zachary “Gamer_Z.” Yaro



To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

Scott Fujan

unread,
Nov 30, 2011, 6:53:16 PM11/30/11
to Zachary “Gamer_Z.” Yaro, Assaf, Chromium-extensions
You are correct

Assaf

unread,
Dec 1, 2011, 12:50:02 AM12/1/11
to Chromium-extensions
Well, if the user just opened the browswer, than you are correct. but
it's not working even when there are open tabs.
I don't mind if it would give a null value in case of a fresh
browser.the outcome will be empty field which is also fine

Assaf

unread,
Dec 4, 2011, 3:03:47 AM12/4/11
to Chromium-extensions
Anyone?

Ben

unread,
Dec 4, 2011, 2:34:40 PM12/4/11
to Assaf, Chromium-extensions
Because the call is asynchronous, the variable will almost never be
ready when you need it:

<script>
chrome.tabs.getSelected(null, function(tab) {
var url1 = tab.url; // <--- This gets executed in a separate thread
});

/* url1 likely doesn't exist yet, because the above
thread is still being initialized (a "race condition") */


document.write("<input type='text' name='website' size='50' value="+url1
+"'>");
</script>

To solve that, you can create a function which will be called from
inside the callback function, like so:

function doSomething(tabUrl) {document.write("<p>"+tabUrl+"</p>"); }

chrome.tabs.getSelected(null, function(tab) {
doSomething(tab.url);
});

Or write everything within the callback function.

Reply all
Reply to author
Forward
0 new messages