-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
192 lines (179 loc) · 4.8 KB
/
index.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
if (window.localStorage && localStorage.hct && Date.now() - localStorage.hct < 60*60*24) {
console.log("localdata");
var ch = JSON.parse(localStorage.ctohex);
}
else {
var ch;
var chx = new XMLHttpRequest();
chx.open("GET","ctohex.json",true);
chx.send();
chx.onreadystatechange=function(){
if(chx.readyState==4&&chx.status==200){
ch = JSON.parse(chx.responseText);
localStorage.ctohex = chx.responseText;
localStorage.hct = Date.now();
}
}
}
function removeCshow(k = 1) {
for(k;document.getElementsByClassName("cShow").length > k;k++){
document.getElementsByClassName("cShow")[k].style.display = "none";
}
// document.body.removeChild(document.body.lastChild);
}
function hexC(evt) {
evt = (evt) ? evt : ((window.event) ? window.event : "")
keyCode = evt.keyCode ? evt.keyCode : (evt.which ? evt.which : evt.charCode);
if (keyCode == 13) {
hexApply()
}
}
function hexApply(){
let civ = document.getElementById("cInv");
let cv = civ.value,chex,hexn="";
cv = cv.replace("色","");
if(cv.indexOf("#") == 0){
hexn = Object.keys(ch).filter(key => ch[key] === cv);
if(hexn.length > 0) hexn = hexn.join("<br>");
else hexn = "";
chex = cv;
}
else if(cv == "r" || cv == "random" || cv == "随机"){
hexn = randomP(ch)[0];
chex = ch[hexn];
}
else if(chex = cv.match(/r([2-8])/)){
hexn = randomP(ch,chex[1]);
for(let j in hexn){
csDiv(j,ch[hexn[j]],hexn[j]);
}
removeCshow(chex[1]);
return;
}
else if(cv.indexOf("*") == 0){
wildHex();
return;
}
else if(jcv = ch[cv.toLowerCase()]){
hexn = cv;
chex = jcv;
}
else{
wildHex();
return;
}
csDiv(0,chex,hexn);
removeCshow();
civ.placeholder = chex;
}
function wildHex() {
let civ = document.getElementById("cInv");
let cv = civ.value;
if(cv.indexOf("*") == 0) cv = cv.substr(1)
let nc = {},i=1;
for(let c in ch){
if(c.match(cv)) {
nc[c] = ch[c];
i += 1;
if(i>8) break;
}
}
if (Object.keys(nc).length == 0) {
removeCshow();
let rc = randomP(ch)[0];
csDiv(0, ch[rc], rc);
cTip("无匹配,显示随机颜色",1);
return;
}
else{
let j=0;
for(let fc in nc){
csDiv(j,nc[fc],fc);
j += 1;
}
removeCshow(j);
}
}
function csDiv(num,hex,hexn) {
let csh = document.getElementsByClassName("cShow")[num];
if(csh){
csh.getElementsByTagName('input')[0].value = hex;
csh.getElementsByTagName("span")[0].innerHTML = hexn;
csh.style.backgroundColor = hex;
csh.style.display = "block";
}
else{
csh = document.createElement("div");
csh.style.display = "block";
csh.className = "cShow";
csh.setAttribute("onclick", "copyHexc(" + num + ")");
csh.style.backgroundColor = hex;
let cinput = document.createElement("input");
cinput.type = "text";
cinput.className = "chex";
cinput.value = hex;
csh.appendChild(cinput);
let cspan = document.createElement("span");
cspan.className = "hexName";
cspan.innerHTML = hexn;
cspan.setAttribute("onclick", "event.stopPropagation();copyHexc(0,this.innerHTML)")
csh.appendChild(cspan);
document.body.appendChild(csh);
}
}
function copyHexc(i,str=false) {
let chex = document.getElementsByClassName("cShow")[i].getElementsByTagName("input")[0],ostr="";
if (str){
ostr = chex.value;
chex.value = str;
}
else if (chex.value == "") return;
chex.select();
if(document.execCommand("copy")) {
cTip("复制内容:" + chex.value);
if(str) {chex.value = ostr}
}
else console.log("复制失败");
chex.blur();
}
function randomP(obj,i=1) {
// obj 随机选取
var keys = Object.keys(obj),tmpv = [];
for(i; i>0; i--){
tmpv.push(keys[keys.length * Math.random() << 0]);
}
return tmpv;
}
function fadeOut(el){
el.style.opacity = 1;
(function fade() {
if ((el.style.opacity -= .05) < 0) {
el.style.display = "none";
} else {
requestAnimationFrame(fade);
}
})();
};
function fadeIn(el, display){
el.style.opacity = 0;
el.style.display = display || "block";
(function fade() {
var val = parseFloat(el.style.opacity);
if (!((val += .05) > 1)) {
el.style.opacity = val;
requestAnimationFrame(fade);
}
})();
};
function cTip(str,p = 0) {
let ct = document.getElementById('ctip')
if(ct == undefined){
ct = document.createElement('div');
ct.id = "ctip";
}
ct.innerHTML = str;
if(p!=0) {ct.style.top = "20px";ct.style.bottom = "unset";}
document.body.appendChild(ct);
fadeIn(ct);
setTimeout("fadeOut(document.getElementById('ctip'))",3000)
}