Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch URL of current Tab in my chrome extension using javascript

I am just getting started with Google Chrome Extension development and my project involves making an extension which when clicked prints the URL of whichever page/tab is currently open.

So if I am on google's home page and I click my extension, I need to get "https://www.google.com/" as my output within the extension.

I need to do this using javascript and am unable to find code which I understand and which does the job. I read about using "window.location" and "document.href" and stuff but won't that give me the url of just my extension and not the current tab?

Please help me get started. Thanks in advance.

like image 222
Darth Coder Avatar asked Aug 26 '13 03:08

Darth Coder


People also ask

How do I find the URL for a Chrome extension?

Setting currentWindow: true allows you to get the current tab in the window where your extension's code is currently executing. For example, this might be useful if your extension creates a new window / popup (changing focus), but still wants to access tab information from the window where the extension was run.

How do I copy the URL address of all open tabs in Chrome?

Click Use Current Pages below the box to load all the URLs from all the tabs into the box. Then, put the cursor in the box, select all (Ctrl + A), and copy (Ctrl + C).

How do I check my current extensions in Chrome?

On your computer, open Chrome . At the top right, click Extensions .


1 Answers

Note you must have the tabs permission set in your manifest file

"permissions": [     "tabs" ], 

http://developer.chrome.com/extensions/tabs.html

or the activeTab permission if initiated by a click on the extension button[Xan]

https://developer.chrome.com/extensions/activeTab


Code:

chrome.tabs.query({currentWindow: true, active: true}, function(tabs){     console.log(tabs[0].url); }); 
like image 87
Musa Avatar answered Sep 28 '22 23:09

Musa