3

I am making an chrome extension that shares url to one website. Now I need a code that can get current URL navigated in browser. When I click on icon I want to open new tab (http://www.thatsite.com/sharer.php?u= + current URL).

I have two files:

manifest.json

{
    "name": "Share on that site",
    "version": "1",
    "browser_action": {
        "default_icon": "icon.png"
    },
    "background": {
        "scripts": ["background.js"]
    },
    "permissions": ["tabs"],
    "manifest_version": 2
}

and background.js

chrome.browserAction.onClicked.addListener(function(activeTab){
    var newURL = "http://www.leegly.com/sharer.php?u=" + <current URL here>;
    chrome.tabs.create({ url: newURL });
});
Rob W
  • 341,306
  • 83
  • 791
  • 678
user93146
  • 33
  • 1
  • 3
  • I have edited your post to make it conciser. During this edit, I have also removed a `});` from your manifest file, assuming that it's a copy-paste mistake. The presence of these characters result in an invalid manifest file, which prevents your extension from loading. If your original manifest file *did* contain this error, just copy-paste the current manifest from your edited question (and use "activeTab" instead of "tabs" as suggested by my answer below). – Rob W Jun 29 '13 at 10:53

2 Answers2

3

When the chrome.browserAction.onClicked event is dispatched, the first argument holds information about the current tab.

To get the URL of the current tab, first request the activeTab permission in the manifest file (the tabs permission is unnecessary, you can omit it). Then, getting the URL is as simple as reading tab.url:

chrome.browserAction.onClicked.addListener(function(tab) {
    var url_encoded_url = encodeURIComponent(tab.url);
    var newURL = "http://www.leegly.com/sharer.php?u=" + url_encoded_url;
    chrome.tabs.create({ url: newURL });
});

Note that I've used encodeURIComponent. Without this, your code will fail if the current URL contains an ampersand (&).

Rob W
  • 341,306
  • 83
  • 791
  • 678
0

You can use following function

chrome.tabs.getSelected(null,function(tab) {
    var taburl = tab.url;
});
Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66
  • `chrome.tabs.getSelected` has been deprecated since Chrome 16. – Rob W Jun 29 '13 at 10:43
  • @Rob W unnecessary downvote I think. Version is nowhere to be seen in OP and this is currently working in my browser. – Shashwat Kumar Jun 29 '13 at 10:54
  • 1
    @ShashwatKumar Chrome Canary is already at version 30. It's generally bad practice to use deprecated methods, even when it seems to be functional at the moment. Deprecation is the first step towards removal, so the function could break at any time in the future. – Rob W Jun 29 '13 at 10:56
  • I don't have control over his PC. It is working on mine. He may be making some other mistake. I didn't post this reply unknowingly. Its working so I did. – Shashwat Kumar Jun 29 '13 at 11:01
  • If you edit your answer to use `chrome.tabs.query`, I will retract the downvote. – Rob W Jun 29 '13 at 11:01
  • I have never used chrome.tabs.query. I don't know whether it works or not. – Shashwat Kumar Jun 29 '13 at 11:04