-
Notifications
You must be signed in to change notification settings - Fork 29
/
background.js
80 lines (72 loc) · 2.9 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
var TITLE_ON = 'Code Cola(on)',
TITLE_OFF = 'Code Cola(off)';
var tabStatus = {};
var toggleTabStatus = function(id){
if(!tabStatus[id]){
initTabStatus(id);
}
tabStatus[id].active = !tabStatus[id].active;
syncTabStatus(id);
},
syncTabStatus = function(id){
if(!tabStatus[id]){
initTabStatus(id);
}
if(!tabStatus[id].active){
chrome.browserAction.setTitle({"tabId": id, "title": TITLE_OFF});
chrome.browserAction.setIcon({"tabId": id, "path": "cc-off.png"});
}else{
chrome.browserAction.setTitle({"tabId": id, "title": TITLE_ON});
chrome.browserAction.setIcon({"tabId": id, "path": "cc-on.png"});
}
},
initTabStatus = function(id){
tabStatus[id] = {};
};
chrome.tabs.onUpdated.addListener(function(tabId, info, tab){
if (info.status === 'complete') {
initTabStatus(tabId);
}
});
chrome.tabs.onActivated.addListener(function(tab){
syncTabStatus(tab.tabId);
});
//chrome.tabs.onRemoved.addListener(function(tabId, removeInfo){
// toggleTabStatus(tabId);
//});
chrome.browserAction.onClicked.addListener(function(tab) {
var id = tab.id;
if(tab.url.indexOf("https://chrome.google.com") == 0 || tab.url.indexOf("chrome://") == 0 || tab.url.indexOf("googleusercontent.com") == 0){
alert(chrome.i18n.getMessage("error_google"));
return;
}else if(tab.url.indexOf("file:///") == 0){
alert(chrome.i18n.getMessage("error_local"));
return;
}
chrome.browserAction.getTitle({
tabId: id
}, function(title){
chrome.tabs.sendMessage(id, 'browserAction');
if(title === TITLE_OFF && !tabStatus[id].on){
chrome.tabs.insertCSS(id, {file: "codecola.css"});
chrome.tabs.insertCSS(id, {file: "code-cola-widget/src/color/codecola-color.css"});
chrome.tabs.insertCSS(id, {file: "code-cola-widget/src/degree/codecola-degree.css"});
chrome.tabs.insertCSS(id, {file: "code-cola-widget/src/gradient/codecola-gradient.css"});
chrome.tabs.executeScript(id, {file: "yui3.js"});
chrome.tabs.executeScript(id, {file: "plugin.js"});
chrome.tabs.executeScript(id, {file: "code-cola-widget/src/color/codecola-color.js"});
chrome.tabs.executeScript(id, {file: "code-cola-widget/src/degree/codecola-degree.js"});
chrome.tabs.executeScript(id, {file: "code-cola-widget/src/gradient/codecola-gradient.js"});
chrome.tabs.executeScript(id, {file: "codecola.js"});
tabStatus[id].on = true;
}
toggleTabStatus(id);
});
});
chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
if(request == "getUrls"){
sendResponse({
"action": localStorage["codecola_save_action"]?localStorage["codecola_save_action"]:""
});
}
});