1// Copyright (c) 2012 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
5var lastTabId = 0;
6var tab_clicks = {};
7
8chrome.tabs.onSelectionChanged.addListener(function(tabId) {
9  lastTabId = tabId;
10  chrome.pageAction.show(lastTabId);
11});
12
13chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
14  lastTabId = tabs[0].id;
15  chrome.pageAction.show(lastTabId);
16});
17
18// Called when the user clicks on the page action.
19chrome.pageAction.onClicked.addListener(function(tab) {
20  var clicks = tab_clicks[tab.id] || 0;
21  chrome.pageAction.setIcon({path: "icon" + (clicks + 1) + ".png",
22                             tabId: tab.id});
23  if (clicks % 2) {
24    chrome.pageAction.show(tab.id);
25  } else {
26    chrome.pageAction.hide(tab.id);
27    setTimeout(function() { chrome.pageAction.show(tab.id); }, 200);
28  }
29  chrome.pageAction.setTitle({title: "click:" + clicks, tabId: tab.id});
30
31  // We only have 2 icons, but cycle through 3 icons to test the
32  // out-of-bounds index bug.
33  clicks++;
34  if (clicks > 3)
35    clicks = 0;
36  tab_clicks[tab.id] = clicks;
37});
38
39var i = 0;
40window.setInterval(function() {
41  var clicks = tab_clicks[lastTabId] || 0;
42
43  // Don't animate while in "click" mode.
44  if (clicks > 0) return;
45
46  // Don't do anything if we don't have a tab yet.
47  if (lastTabId == 0) return;
48
49  i++;
50  chrome.pageAction.setIcon({imageData: draw(i*2, i*4), tabId: lastTabId});
51}, 50);
52
53function draw(starty, startx) {
54  var canvas = document.getElementById('canvas');
55  var context = canvas.getContext('2d');
56  context.clearRect(0, 0, canvas.width, canvas.height);
57  context.fillStyle = "rgba(0,200,0,255)";
58  context.fillRect(startx % 19, starty % 19, 8, 8);
59  context.fillStyle = "rgba(0,0,200,255)";
60  context.fillRect((startx + 5) % 19, (starty + 5) % 19, 8, 8);
61  context.fillStyle = "rgba(200,0,0,255)";
62  context.fillRect((startx + 10) % 19, (starty + 10) % 19, 8, 8);
63  return context.getImageData(0, 0, 19, 19);
64}
65