forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
64 lines (54 loc) · 1.98 KB
/
background.js
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var lastTabId = 0;
var tab_clicks = {};
chrome.tabs.onSelectionChanged.addListener(function(tabId) {
lastTabId = tabId;
chrome.pageAction.show(lastTabId);
});
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
lastTabId = tabs[0].id;
chrome.pageAction.show(lastTabId);
});
// Called when the user clicks on the page action.
chrome.pageAction.onClicked.addListener(function(tab) {
var clicks = tab_clicks[tab.id] || 0;
chrome.pageAction.setIcon({path: "icon" + (clicks + 1) + ".png",
tabId: tab.id});
if (clicks % 2) {
chrome.pageAction.show(tab.id);
} else {
chrome.pageAction.hide(tab.id);
setTimeout(function() { chrome.pageAction.show(tab.id); }, 200);
}
chrome.pageAction.setTitle({title: "click:" + clicks, tabId: tab.id});
// We only have 2 icons, but cycle through 3 icons to test the
// out-of-bounds index bug.
clicks++;
if (clicks > 3)
clicks = 0;
tab_clicks[tab.id] = clicks;
});
var i = 0;
window.setInterval(function() {
var clicks = tab_clicks[lastTabId] || 0;
// Don't animate while in "click" mode.
if (clicks > 0) return;
// Don't do anything if we don't have a tab yet.
if (lastTabId == 0) return;
i++;
chrome.pageAction.setIcon({imageData: draw(i*2, i*4), tabId: lastTabId});
}, 50);
function draw(starty, startx) {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(0,200,0,255)";
context.fillRect(startx % 19, starty % 19, 8, 8);
context.fillStyle = "rgba(0,0,200,255)";
context.fillRect((startx + 5) % 19, (starty + 5) % 19, 8, 8);
context.fillStyle = "rgba(200,0,0,255)";
context.fillRect((startx + 10) % 19, (starty + 10) % 19, 8, 8);
return context.getImageData(0, 0, 19, 19);
}