forked from ao5357/development_favicon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
158 lines (145 loc) · 4.5 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
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
/**
* Listener for the "patternmatch" CustomEvent.
*
* Event is dispatched only if a match is found
* in the "tabupdate" cycle.
*/
document.addEventListener("patternmatch", function(evt) {
"use strict";
// Define scoped pseudo-globals.
var ext = evt.detail.ext,
tabId = evt.detail.tabId,
favIconUrl = evt.detail.favIconUrl,
holder = document.createElement("img");
holder.addEventListener("load", imageLoadCallback);
/**
* Build out the canvas only after the favicon loads.
*/
function imageLoadCallback(evt) {
// Transpose the icon into a canvas.
var canvas = document.createElement("canvas");
canvas.width = holder.width;
canvas.height = holder.height;
var context = canvas.getContext("2d");
context.drawImage(holder, 0, 0);
// Draw the effect based on the user dropdown.
context.fillStyle = ext.bgcolor;
switch (ext.orientation) {
case 'right':
context.fillRect(Math.floor(canvas.width * .75), 0, Math.floor(canvas.width / 4), canvas.height);
break;
case 'bottom':
context.fillRect(0, Math.floor(canvas.height * .75), canvas.width, Math.floor(canvas.height / 4));
break;
case 'left':
context.fillRect(0, 0, Math.floor(canvas.width / 4), canvas.height);
break;
case 'cover':
context.globalAlpha = 0.5;
context.fillRect(0, 0, canvas.width, canvas.height);
break;
case 'replace':
context.globalCompositeOperation = "source-in";
context.fillRect(0, 0, canvas.width, canvas.height);
break;
case 'background':
context.globalCompositeOperation = "destination-over";
context.fillRect(0, 0, canvas.width, canvas.height);
break;
case 'xor-top':
context.globalCompositeOperation = "xor";
context.fillRect(0, 0, canvas.width, Math.floor(canvas.height / 4));
break;
default:
context.fillRect(0, 0, canvas.width, Math.floor(canvas.height / 4));
break;
}
// Pass the icon to the content script.
chrome.tabs.sendMessage(tabId, {
"favIconUrl": canvas.toDataURL()
});
}
// Trigger the image load event.
holder.src = favIconUrl;
}, false);
/**
* If the native chrome event returns complete, this
* event gets fired separately.
*/
document.addEventListener("tabupdate", function(evt) {
"use strict";
// Define locals.
var tabId = evt.detail.tabId;
var ext = {
"matches": false,
"orientation": "top",
"bgcolor": "#ff0000"
};
// Trigger the patternmatch event.
function dispatchPatternmatch(evt) {
// Pass the match to the next async handler.
var patternmatch = new CustomEvent('patternmatch', evt);
document.dispatchEvent(patternmatch);
}
/**
* Determine whether to do anything after tab is retrieved.
*/
function tabsGetCallback(tab) {
/**
* Grab the settings from chrome storage.
*/
chrome.storage.sync.get('rows', function(items){
if (items.rows.length) {
for (var i = 0; i < items.rows.length; i++) {
if (tab.url.match(items.rows[i].pattern)) {
ext.matches = true;
ext.orientation = items.rows[i].orientation;
ext.bgcolor = items.rows[i].bgcolor;
break;
}
}
}
if (ext.matches) {
var favIconUrl = tab.favIconUrl;
if(!favIconUrl) {
// Set the default favicon to the default favicon.
chrome.tabs.sendMessage(tabId, {'null': 'null'}, function(response){
if (response && response.favIconUrl) {
favIconUrl = response.favIconUrl;
}
dispatchPatternmatch({
'detail': {
'ext': ext,
'favIconUrl': favIconUrl,
'tabId': tabId
}
});
});
}
else {
dispatchPatternmatch({
'detail': {
'ext': ext,
'favIconUrl': favIconUrl,
'tabId': tabId
}
});
}
}
});
}
// Trigger the callback, based on the tabId sent to this handler.
chrome.tabs.get(tabId, tabsGetCallback);
}, false);
/**
* This listener kicks everything off, and conditionally
* dispatches tasks to other listeners if the tab update
* is complete.
*/
chrome.tabs.onUpdated.addListener(function(tabId, props) {
"use strict";
if (props.status === "complete") {
var tabupdate = new CustomEvent('tabupdate', {'detail':{'tabId': tabId}});
document.dispatchEvent(tabupdate);
}
});