-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathindex.js
209 lines (209 loc) · 8.88 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* @file 包含初始化看板娘小部件的函数。
* @module index
*/
import Model from './model.js';
import showMessage from './message.js';
import randomSelection from './utils.js';
import tools from './tools.js';
/**
* 加载看板娘小部件。
* @param {Config} config - 看板娘配置。
*/
function loadWidget(config) {
var model = new Model(config);
localStorage.removeItem('waifu-display');
sessionStorage.removeItem('waifu-text');
document.body.insertAdjacentHTML('beforeend', "<div id=\"waifu\">\n <div id=\"waifu-tips\"></div>\n <canvas id=\"live2d\" width=\"800\" height=\"800\"></canvas>\n <div id=\"waifu-tool\"></div>\n </div>");
// https://stackoverflow.com/questions/24148403/trigger-css-transition-on-appended-element
setTimeout(function () {
document.getElementById('waifu').style.bottom = '0';
}, 0);
(function registerTools() {
tools['switch-model'].callback = function () { return model.loadOtherModel(); };
tools['switch-texture'].callback = function () { return model.loadRandModel(); };
if (!Array.isArray(config.tools)) {
config.tools = Object.keys(tools);
}
for (var _i = 0, _a = config.tools; _i < _a.length; _i++) {
var tool = _a[_i];
if (tools[tool]) {
var _b = tools[tool], icon = _b.icon, callback = _b.callback;
document
.getElementById('waifu-tool')
.insertAdjacentHTML('beforeend', "<span id=\"waifu-tool-".concat(tool, "\">").concat(icon, "</span>"));
document
.getElementById("waifu-tool-".concat(tool))
.addEventListener('click', callback);
}
}
})();
/**
* 根据时间显示欢迎消息。
* @param {Time} time - 时间消息配置。
* @returns {string} 欢迎消息。
*/
function welcomeMessage(time) {
if (location.pathname === '/') {
// 如果是主页
for (var _i = 0, time_1 = time; _i < time_1.length; _i++) {
var _a = time_1[_i], hour = _a.hour, text_1 = _a.text;
var now = new Date(), after = hour.split('-')[0], before = hour.split('-')[1] || after;
if (Number(after) <= now.getHours() &&
now.getHours() <= Number(before)) {
return text_1;
}
}
}
var text = "\u6B22\u8FCE\u9605\u8BFB<span>\u300C".concat(document.title.split(' - ')[0], "\u300D</span>");
var from;
if (document.referrer !== '') {
var referrer = new URL(document.referrer), domain = referrer.hostname.split('.')[1];
var domains = {
baidu: '百度',
so: '360搜索',
google: '谷歌搜索',
};
if (location.hostname === referrer.hostname)
return text;
if (domain in domains)
from = domains[domain];
else
from = referrer.hostname;
return "Hello\uFF01\u6765\u81EA <span>".concat(from, "</span> \u7684\u670B\u53CB<br>").concat(text);
}
return text;
}
/**
* 注册事件监听器。
* @param {Result} result - 结果配置。
*/
function registerEventListener(result) {
// Detect user activity and display messages when idle
var userAction = false;
var userActionTimer;
var messageArray = result.message.default;
var lastHoverElement;
window.addEventListener('mousemove', function () { return (userAction = true); });
window.addEventListener('keydown', function () { return (userAction = true); });
setInterval(function () {
if (userAction) {
userAction = false;
clearInterval(userActionTimer);
userActionTimer = null;
}
else if (!userActionTimer) {
userActionTimer = setInterval(function () {
showMessage(randomSelection(messageArray), 6000, 9);
}, 20000);
}
}, 1000);
showMessage(welcomeMessage(result.time), 7000, 11);
window.addEventListener('mouseover', function (event) {
var _a;
// eslint-disable-next-line prefer-const
for (var _i = 0, _b = result.mouseover; _i < _b.length; _i++) {
var _c = _b[_i], selector = _c.selector, text = _c.text;
if (!((_a = event.target) === null || _a === void 0 ? void 0 : _a.closest(selector)))
continue;
if (lastHoverElement === selector)
return;
lastHoverElement = selector;
text = randomSelection(text);
text = text.replace('{text}', event.target.innerText);
showMessage(text, 4000, 8);
return;
}
});
window.addEventListener('click', function (event) {
var _a;
// eslint-disable-next-line prefer-const
for (var _i = 0, _b = result.click; _i < _b.length; _i++) {
var _c = _b[_i], selector = _c.selector, text = _c.text;
if (!((_a = event.target) === null || _a === void 0 ? void 0 : _a.closest(selector)))
continue;
text = randomSelection(text);
text = text.replace('{text}', event.target.innerText);
showMessage(text, 4000, 8);
return;
}
});
result.seasons.forEach(function (_a) {
var date = _a.date, text = _a.text;
var now = new Date(), after = date.split('-')[0], before = date.split('-')[1] || after;
if (Number(after.split('/')[0]) <= now.getMonth() + 1 &&
now.getMonth() + 1 <= Number(before.split('/')[0]) &&
Number(after.split('/')[1]) <= now.getDate() &&
now.getDate() <= Number(before.split('/')[1])) {
text = randomSelection(text);
text = text.replace('{year}', String(now.getFullYear()));
messageArray.push(text);
}
});
var devtools = function () { };
console.log('%c', devtools);
devtools.toString = function () {
showMessage(result.message.console, 6000, 9);
};
window.addEventListener('copy', function () {
showMessage(result.message.copy, 6000, 9);
});
window.addEventListener('visibilitychange', function () {
if (!document.hidden)
showMessage(result.message.visibilitychange, 6000, 9);
});
}
(function initModel() {
var modelId = Number(localStorage.getItem('modelId'));
var modelTexturesId = Number(localStorage.getItem('modelTexturesId'));
if (modelId === null) {
// 首次访问加载 指定模型 的 指定材质
modelId = 1; // 模型 ID
modelTexturesId = 53; // 材质 ID
}
void model.loadModel(modelId, modelTexturesId, '');
fetch(config.waifuPath)
.then(function (response) { return response.json(); })
.then(registerEventListener);
})();
}
/**
* 初始化看板娘小部件。
* @param {string | Config} config - 看板娘配置或配置路径。
* @param {string} [apiPath] - API 路径,如果 config 是字符串。
*/
function initWidget(config, apiPath) {
if (typeof config === 'string') {
config = {
waifuPath: config,
apiPath: apiPath,
};
}
document.body.insertAdjacentHTML('beforeend', "<div id=\"waifu-toggle\">\n <span>\u770B\u677F\u5A18</span>\n </div>");
var toggle = document.getElementById('waifu-toggle');
toggle === null || toggle === void 0 ? void 0 : toggle.addEventListener('click', function () {
toggle.classList.remove('waifu-toggle-active');
if (toggle === null || toggle === void 0 ? void 0 : toggle.getAttribute('first-time')) {
loadWidget(config);
toggle === null || toggle === void 0 ? void 0 : toggle.removeAttribute('first-time');
}
else {
localStorage.removeItem('waifu-display');
document.getElementById('waifu').style.display = '';
setTimeout(function () {
document.getElementById('waifu').style.bottom = '0';
}, 0);
}
});
if (localStorage.getItem('waifu-display') &&
Date.now() - Number(localStorage.getItem('waifu-display')) <= 86400000) {
toggle === null || toggle === void 0 ? void 0 : toggle.setAttribute('first-time', 'true');
setTimeout(function () {
toggle === null || toggle === void 0 ? void 0 : toggle.classList.add('waifu-toggle-active');
}, 0);
}
else {
loadWidget(config);
}
}
export default initWidget;