views:

831

answers:

2

Basically on my window (when you click the icon) it should open and show the URL of the tab and next to it I want it to say "Save", it will save it to the localStorage, and to be displayed below into the saved links area.

Like this:

alt text

Something like bookmarks :)

A: 

to get the current url, you need to get the current tab and then extract all the paramenters.

for getting the current tab use, chrome.tabs.getSelected(). Then, to fetch the parameters from the tab object, refer tabs api

your code snippet should look like this,

chrome.tabs.getSelected(function(tab) {
  tab = tab.id;
  tabUrl = tab.url;

  //rest of the save functionality.
});

you'll also need to declare the "tabs" permission in your extension's manifest to use the tabs API. For example

{
  "name": "My extension",
  ...
  "permissions": [
    "tabs"
  ],
  ...
}
phoenix24
+4  A: 

Hi Jamie,

If you want to do something like that, you easily do that with the Chrome extensions API. The areas to look for are:

Now the first step is to create your popup.html file and remember that it is transient, that is, it only lives when you click on the browser action, then dies if it exits (closes). What I am trying to say, if you have a lot of computation and you want it to happen in the background and happen even if the popup is closed, move everything to the background page. And in your popup, you can easily access the background page by using chrome.extension.getBackgroundPage()

Within your popup.html, you would need to get the URL of the current tab, to do so, the Tab API has a "getSelected" function that allows you to get the Tab object for the selected Tab.

So something like this:

<html>
<head>
  <script>
    window.addEventListener("load", windowLoaded, false);
    function windowLoaded() {
      chrome.tabs.getSelected(null, function(tab) {
        document.getElementById('currentLink').innerHTML = tab.url;
      });
    }
  </script>
</head>
<body>
<p id="currentLink">Loading ...</p>
<hr />
<ul id="savedLinks"></ul>
</body>
</html>

Now that will allow you to show the Url in the popup for the current page as a browser action. Your next step is to use simple HTML5 features such as localStorage, or Webdatabase (in my opinion that will be better). To store the saved pages into, then you can render them on the savedLinks page same as I have done for the currentLink.

Good Luck!

Mohamed Mansour
Very helpful code to start with, I'll do the storage, thanks :)
Jamie
Take a look at HTML5 webdatabase (it is better for your scenario)
Mohamed Mansour