-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab.js
178 lines (156 loc) · 5.03 KB
/
tab.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Main.
*/
document.addEventListener('DOMContentLoaded', init);
chrome.storage.onChanged.addListener(restoreSettings); //Update CSS every time settings are saved.
function init() {
//Restore settings.
restoreSettings();
//Start updating time.
updateTime();
setInterval(updateTime, 1000);
//Retrieve set wallpaper.
chrome.storage.local.get('wallpaper', function(items) {
var background = get('backimage');
if (!items.wallpaper)
background.src = 'images/background.png';
else
background.src = items.wallpaper;
background.style.animation = 'fadein 2s'; //Fade in after image has loaded.
});
//Initialize wallpaper setter.
if (window.File && window.FileList && window.FileReader) {
fileDrag();
}
}
/*
Functions.
*/
function updateTime() {
get('time').textContent = getTime();
}
function getTime() {
var d = new Date();
var hours = d.getHours() > 12 ? d.getHours() - 12 : d.getHours();
hours = hours == 0 ? 12 : hours;
hours = hours < 10 ? '0' + hours : hours;
var minutes = d.getMinutes();
minutes = minutes < 10 ? '0' + minutes : minutes;
var seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds();
return `${hours} ${minutes} ${seconds}`;
}
/*
Restore settings from local storage.
*/
function restoreSettings() {
chrome.storage.local.get({
posY: 'Top', //Top, Middle, Bottom
posX: 'Middle', //Left, Middle, Right
edgeDistance: 100, //px
fontColor: '#FFFFFF',
fontSize: 110, //px
fontOpacity: 1,
backgroundColor: '#000000',
backgroundOpacity: 0.4
}, function(items) {
//Clock background settings.
var background = get('clock');
var backgroundRgb = hexToRgb(items.backgroundColor);
background.style.backgroundColor =
`rgba(${backgroundRgb.r}, ${backgroundRgb.g}, ${backgroundRgb.b}, ${items.backgroundOpacity})`;
//Clock position settings.
background.style.transform = '';
background.style.top = '';
background.style.bottom = '';
background.style.left = '';
background.style.right = '';
switch (items.posY) {
case 'Top':
background.style.top = `${items.edgeDistance}px`;
break;
case 'Middle':
background.style.top = '50%';
background.style.transform = 'translateY(-50%)';
break;
case 'Bottom':
background.style.bottom = `${items.edgeDistance}px`;
break;
};
switch (items.posX) {
case 'Left':
background.style.left = `${items.edgeDistance}px`;
break;
case 'Middle':
background.style.left = '50%';
background.style.transform += 'translateX(-50%)';
break;
case 'Right':
background.style.right = `${items.edgeDistance}px`;
break;
};
//Clock font settings, and adjust margins.
var time = get('time');
var fontRgb = hexToRgb(items.fontColor);
time.style.color =
`rgba(${fontRgb.r}, ${fontRgb.g}, ${fontRgb.b}, ${items.fontOpacity})`;
time.style.fontSize = `${items.fontSize}px`;
time.style.margin =
`-${items.fontSize/15}px ${items.fontSize/10}px -${items.fontSize/15}px ${items.fontSize/10}px`;
});
}
/*
File drag handling.
*/
function fileDrag() {
var filedrag = get('filedrag');
filedrag.addEventListener('dragover', fileDragHover, false);
filedrag.addEventListener('dragleave', fileDragHover, false);
filedrag.addEventListener('drop', fileSelectHandler, false);
}
function fileDragHover(e) {
e.stopPropagation();
e.preventDefault();
e.target.className = (e.type == 'dragover' ? 'hover' : '');
}
function fileSelectHandler(e) {
//Cancel hover styling.
fileDragHover(e);
//Select image and change wallpaper.
var file = e.dataTransfer.files[0];
var imagePatt = /^image\//;
if (!imagePatt.test(file.type)) return;
var reader = new FileReader();
reader.onload = function(e) {
changeBackground(e.target.result);
}
reader.readAsDataURL(file);
}
/*
Crossfade old and new background.
*/
function changeBackground(source) {
var image = get('backimage');
var oldImage = get('backimageold');
oldImage.src = image.src;
oldImage.className = 'replace';
image.src = source;
chrome.storage.local.set({ wallpaper: source });
setTimeout(function() { //Delete old background after transition.
oldImage.src = '';
oldImage.className = '';
}, 2000);
}
function get(id) {
return document.getElementById(id);
}
/*
Converts hexcode to rgb.
*/
function hexToRgb(hex) { //Written by Tim Down.
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}