aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/fiveui/chrome/background.js
blob: 7f74a33b3843f2beecc85ccfb285f2760a0c2d7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Module     : chrome/background.js
 * Copyright  : (c) 2011-2012, Galois, Inc.
 *
 * Maintainer :
 * Stability  : Provisional
 * Portability: Not Portable (Chrome Only)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var fiveui = fiveui || {};

(function() {

fiveui.chrome = fiveui.chrome || {};

/**
 * The primary entry point for the FiveUI Chrome background page.
 */
fiveui.chrome.background = function() {

  /**
   * Inject code and resources into the specified tab's web page.
   *
   * @param {!number} tabid The id of the tab to load scripts into.
   * @param {!Array.<string>} inScripts The list of scripts to load in
   *                                  order.
   * @param {!boolean} inFrames Whether or not to inject into iFrames.
   * @return {void}
   */
  var loadScripts = function(tabid, inScripts, inFrames) {
    var chromeScripts = [dataLoader('fiveui/chrome/chrome-port.js')];
    if(inFrames) {
      chromeScripts.push(dataLoader('fiveui/chrome/chrome-injected-compute.js'));
    } else {
      chromeScripts.push(dataLoader('fiveui/chrome/chrome-injected-ui.js'));
    }
    var scripts = _.flatten([chromeScripts, inScripts]);

    var end = function() {};

    var loop = function() {
      var next = loop;
      if (scripts.length == 1) {
        next = end;
      }

      var script = scripts.shift();

      // console.log('injecting: ' + script);

      if (/css$/.test(script)) {
        chrome.tabs.insertCSS(tabid, { 'file' : script }, next);
      } else {
        chrome.tabs.executeScript(tabid, { 'file' : script }, next);
      }
    };

    loop();
  };

  /**
   * Set the current widget icon.
   *
   * @param {!string} iconPath The local path to the icon to use.
   * @return {void}
   */
  var setIcon = function(iconPath) {
    chrome.tabs.getSelected(null,
      function(tab) {
        chrome.browserAction.setIcon({
          path: iconPath,
          tabId: tab.id
        });
      });
  };

  /**
   * Change the text undearneath the fiveui icon.
   *
   * @param {!fiveui.TabState} tabState The tab state object to update.
   * @return {void}
   */
  var updateIconText = function(tabState) {
    var tabId = tabState.tabId;
    var probs = tabState.problems.length;
    var text = '';
    if (probs > 0) {
      if (probs > 99) {
        text = '99+';
      } else {
        text = probs.toString();
      }
    }
    chrome.browserAction.setBadgeText({ text: text, tabId: tabId });
  };

  var updateWidget = function(tabState) {
    if(null == tabState) {
      setIcon('../images/fiveui-icon-disabled.png');
    } else {
      setIcon('../images/fiveui-icon-enabled.png');
      updateIconText(tabState);
    }
  };

  // launch the generic background script
  var dataLoader = function (path) {
    return "data/"+path;
  };
  var settings = new fiveui.Settings(localStorage);
  background = new fiveui.Background(settings, updateWidget,
                                     loadScripts, dataLoader);

  // notify the generic background about a new content script connection.
  chrome.extension.onConnect.addListener(
    function(chPort) {
      var port = new fiveui.ChromePort(chPort);
      var tabId = chPort.sender.tab.id;
      var url = chPort.sender.tab.url;

      background.connect(tabId, port, url, chPort.name == 'ui');
    });


  chrome.tabs.onCreated.addListener(function(tab) {
    // console.log('in oncreated');
    if (tab.url) {
      background.pageLoad(tab.id, tab.url);
    }
  });
  // check page load events against the generic background
  chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if (changeInfo.status == 'complete') {
      background.pageLoad(tabId, tab.url);
    }
  });

  chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
    background.removeTab(tabId);
  });

  // show the browser widget when the fiveui button is clicked.
  chrome.browserAction.onClicked.addListener(function(tab) {
    background.showUI(tab.id);
  });
};

// Add event listeners once the DOM has fully loaded by listening for the
// `DOMContentLoaded` event on the document, and adding your listeners to
// specific elements when it triggers.
jQuery(fiveui.chrome.background);

})();