- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / examples / extensions / mappy / background.js
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Global accessor that the popup uses.
6 var addresses = {};
7 var selectedAddress = null;
8 var selectedId = null;
9
10 function updateAddress(tabId) {
11   chrome.tabs.sendRequest(tabId, {}, function(address) {
12     addresses[tabId] = address;
13     if (!address) {
14       chrome.pageAction.hide(tabId);
15     } else {
16       chrome.pageAction.show(tabId);
17       if (selectedId == tabId) {
18         updateSelected(tabId);
19       }
20     }
21   });
22 }
23
24 function updateSelected(tabId) {
25   selectedAddress = addresses[tabId];
26   if (selectedAddress)
27     chrome.pageAction.setTitle({tabId:tabId, title:selectedAddress});
28 }
29
30 chrome.tabs.onUpdated.addListener(function(tabId, change, tab) {
31   if (change.status == "complete") {
32     updateAddress(tabId);
33   }
34 });
35
36 chrome.tabs.onSelectionChanged.addListener(function(tabId, info) {
37   selectedId = tabId;
38   updateSelected(tabId);
39 });
40
41 // Ensure the current selected tab is set up.
42 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
43   updateAddress(tabs[0].id);
44 });