-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
180 lines (158 loc) · 4.79 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link id="favicon" rel="icon"></link>
<link rel="manifest" href="manifest.webmanifest">
</head>
<style>
input {
width: 100%;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=text] {
padding: 12px 20px;
}
input[type=color] {
padding: 0px 0px;
height: 41px;
}
</style>
<body>
<script>
function getURLParameter(sParam) {
let sPageURL = window.location.search.substring(1);
let sURLVariables = sPageURL.split('&');
for (let i = 0; i < sURLVariables.length; i++) {
let sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
return "";
}
function initTitleAndColor(title, color) {
document.titleTab = title;
document.colorTab = color;
updateUrl();
updateInputTitle();
updateInputColor();
updateTitle();
updateFavicon();
}
function setTitle(title) {
document.titleTab = title;
updateUrl();
updateInputTitle();
updateTitle();
}
function getTitle() {
return document.titleTab;
}
function updateTitle() {
let title = getTitle()
if (title == "") {
title = '\u200E';
}
document.title = title;
}
function setColor(color) {
document.colorTab = color;
updateUrl();
updateInputColor();
updateFavicon();
}
function getColor() {
return document.colorTab;
}
function updateFavicon() {
let favicon = document.getElementById('favicon');
const faviconSize = 16;
let canvas = document.createElement('canvas');
canvas.height = faviconSize;
canvas.width = faviconSize;
let currentColor = getColor();
let context = canvas.getContext('2d');
context.fillStyle = (currentColor ? "#" + currentColor : "#ffffff");
context.fillRect(0, 0, faviconSize, faviconSize);
favicon.href = canvas.toDataURL('image/png');
}
function updateUrl() {
let currentTitle = getTitle();
let currentColor = getColor();
let url = window.location.pathname;
let params = {};
if (currentTitle && currentTitle != "") {
params["title"] = currentTitle;
}
if (currentColor && currentColor != "") {
params["color"] = currentColor;
}
let str = [];
for(let param in params) {
str.push(param + "=" + params[param]);
}
if (str.length > 0) {
url += '?';
url += str.join("&").toString();
}
window.history.replaceState(null, currentTitle, url);
}
function updateInputTitle() {
let elmtInputTitle = document.getElementById('title');
elmtInputTitle.value = getTitle();
}
function updateInputColor() {
let elmtInputColor = document.getElementById('color');
let currentColor = getColor();
if (currentColor) {
elmtInputColor.value = "#" + getColor();
}
}
window.onload = function() {
let elmtInputTitle = document.getElementById('title');
elmtInputTitle.addEventListener('input', (event) => {
setTitle(event.target.value);
});
let elmtInputColor = document.getElementById('color')
elmtInputColor.addEventListener('input', (event) => {
let color = (event.target.value).replace('#', '');
setColor(color);
});
// Init title from parameter
let paramTitle = decodeURI(getURLParameter('title'));
let paramColor = getURLParameter('color');
initTitleAndColor(paramTitle, paramColor);
}
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/colortab/sw.js', { scope: '/colortab/' })
.then(function(reg) {
if(reg.installing) {
console.log('Service worker installing');
} else if(reg.waiting) {
console.log('Service worker installed');
} else if(reg.active) {
console.log('Service worker active');
}
}).catch(function(error) {
// registration failed
console.log('Registration failed with ' + error);
});
}
</script>
<div>
<label>Title:</label>
<input id="title" type="text"></input>
</div>
<div>
<label>Color:</label>
<input id="color" type="color" value="#ffffff"></input>
</div>
</body>
</html>