forked from Strav/Change-Colors
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground_page.html
244 lines (203 loc) · 6.48 KB
/
background_page.html
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<script>
var CssToInject;
var IsOverrideAll;
var IsPageOverriden;
var IsDomainOverriden;
var OverridenDomains = new Array();
var OverridenPages = new Array();
var OverrideAll;
var CurrentTabId;
function loadOption(optionName){
return JSON.parse(localStorage[optionName]);
}
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
switch(request.shortCut){
case "overridePage":
if(IsPageOverriden)
RemovePageOverride();
else
ApplyPageOverride();
break;
case "overrideDomain":
if(IsDomainOverriden)
RemoveDomainOverride();
else
ApplyDomainOverride();
break;
case "overrideAll":
if(IsOverrideAll)
RemoveOverrideAll();
else
ApplyOverrideAll();
break;
}
}
);
function buildCssToInject(){
var backgroundColor = '#' + loadOption("background_color");
var textColor = '#' + loadOption("text_color");
var linksColor = '#' + loadOption("links_color");
var visitedLinksColor = '#' + loadOption("visited_links_color");
var fontName = loadOption("OverrideFontName");
var fontSize = loadOption("FontSize");
var defaultBrowserFont = loadOption("DefaultBrowserFont");
var defaultBrowserColor = loadOption("DefaultBrowserColor");
if(defaultBrowserColor != true){
var css = 'html > body, html > body * {' +
'background-color: '+ backgroundColor +' !important;' +
'color: ' + textColor + ' !important;' +
'text-shadow: 0 !important;' +
'-webkit-text-fill-color: none !important;}' +
'html > body, html > body *:not([onclick]):not(:link):not(:visited) {' +
'background-image: none !important;}' +
'html > body a:link, html > body a:link *,' +
'html > body a:link:hover, html > body a:link:hover *,' +
'html > body a:link:active, html > body a:link:active * {' +
'color: ' + linksColor + ' !important;}' +
'html > body a:visited, html > body a:visited *,' +
'html > body a:visited:hover, html > body a:visited:hover *,' +
'html > body a:visited:active, html > body a:visited:active * {' +
'color:' + visitedLinksColor + ' !important;}';
}
if(defaultBrowserFont != true){
css += 'html > body, html > body * {' +
'font-family: '+ fontName +' !important;'+
'font-size: '+ fontSize +'pt }';
}
if ( !loadOption("ShowImage") ) {
css += 'img { display:none; }';
}
CssToInject = css;
}
function callInjectCss(){
chrome.tabs.sendRequest(CurrentTabId, {action: "injectCss", css: CssToInject} , null);
}
function callRemoveCss(){
chrome.tabs.sendRequest(CurrentTabId, {action: "removeCss"}, null);
}
function extractDomain(url){
return url.match(/:\/\/(.+?)\//)[1];
}
function checkIfDomainOverriden(domain){
IsDomainOverriden = OverridenDomains.indexOf(domain) != -1;
}
function checkIfPageOverriden(path){
IsPageOverriden = OverridenPages.indexOf(path) != -1;
}
function checkIfOverrideAll(){
return IsOverrideAll;
}
function RemovePageOverride(){
chrome.tabs.get(CurrentTabId, function (tab){
OverridenPages.splice(OverridenPages.indexOf(tab.url),1);
localStorage["OverridenPages"] = JSON.stringify(OverridenPages);
});
IsPageOverriden = false;
callRemoveCss();
}
function RemoveDomainOverride(){
chrome.tabs.get(CurrentTabId, function (tab){
domain = extractDomain(tab.url);
OverridenDomains.splice(OverridenDomains.indexOf(domain),1);
localStorage["OverridenDomains"] = JSON.stringify(OverridenPages);
});
IsDomainOverriden = false;
callRemoveCss();
}
function RemoveOverrideAll(){
localStorage["OverrideAll"] = JSON.stringify(false);
IsOverrideAll = false;
callRemoveCss();
}
function ApplyPageOverride(){
chrome.tabs.get(CurrentTabId, function (tab){
OverridenPages.push(tab.url)
localStorage["OverridenPages"] = JSON.stringify(OverridenPages);
});
IsPageOverriden = true;
callInjectCss();
}
function ApplyDomainOverride(){
chrome.tabs.get(CurrentTabId, function (tab){
OverridenDomains.push(extractDomain(tab.url))
localStorage["OverridenDomains"] = JSON.stringify(OverridenDomains);
});
IsDomainOverriden = true;
callInjectCss();
}
function ApplyOverrideAll(){
localStorage["OverrideAll"] = JSON.stringify(true);
IsOverrideAll = true;
callInjectCss();
}
function IsStyleOverriden(url){
var domain = extractDomain(url);
checkIfOverrideAll();
checkIfPageOverriden(url);
checkIfDomainOverriden(domain);
return IsDomainOverriden || IsPageOverriden || IsOverrideAll;
}
function displayDefault(tabId)
{
chrome.pageAction.setIcon({tabId: tabId, path: "icons/colors_icons.png"});
}
function displayOverriden(tabId)
{
chrome.pageAction.setIcon({tabId: tabId, path: "icons/colors_icons.png"});
}
function updatePageAction(tabId){
CurrentTabId = tabId
chrome.tabs.get(tabId, function (tab){
if(IsStyleOverriden(tab.url)){
callInjectCss();
displayOverriden(tab.id);
}
else
displayDefault(tab.id);
chrome.pageAction.show(tabId);
});
}
function saveOption(optionName, optionValue){
localStorage[optionName] = JSON.stringify(optionValue);
}
function setDefaults()
{
saveOption("OverridenDomains", new Array());
saveOption("OverridenPages", new Array());
saveOption("OverrideAll", false);
saveOption("IsNotFirstStart", true);
saveOption("DefaultBrowserFont", true);
saveOption("DefaultBrowserColor", false);
saveOption("text_color", "E8E8E8");
saveOption("background_color", "080808");
saveOption("links_color", "2E79DB");
saveOption("visited_links_color", "9B51DB");
saveOption("FontSize", "11");
saveOption("OverrideFontName", "Arial");
}
function getOverrides()
{
OverridenDomains = JSON.parse(localStorage["OverridenDomains"]);
OverridenPages = JSON.parse(localStorage["OverridenPages"]);
IsOverrideAll = JSON.parse(localStorage["OverrideAll"]);
}
if(localStorage["IsNotFirstStart"] == undefined){
setDefaults();
getOverrides();
}
else
{
getOverrides();
}
buildCssToInject();
chrome.tabs.onUpdated.addListener(updatePageAction);
chrome.tabs.onSelectionChanged.addListener(updatePageAction);
</script>
</head>
<body>
</body>
</html>