Optimize function parameters

This commit is contained in:
Mimi 2020-02-09 21:01:56 +08:00
parent 549fa28950
commit 5b1e6c7d7a
2 changed files with 16 additions and 5 deletions

View File

@ -31,7 +31,11 @@ if (screen.width >= 768) {
loadExternalResource(live2d_path + "live2d.min.js", "js"),
loadExternalResource(live2d_path + "waifu-tips.js", "js")
]).then(() => {
initWidget(live2d_path + "waifu-tips.json", "https://live2d.fghrsh.net/api");
initWidget({
waifuPath: live2d_path + "waifu-tips.json",
apiPath: "https://live2d.fghrsh.net/api",
cdnPath: "https://cdn.jsdelivr.net/gh/fghrsh/live2d_api"
});
});
}
// initWidget 第一个参数为 waifu-tips.json 的路径,第二个参数为 API 地址

View File

@ -3,7 +3,8 @@
* https://github.com/stevenjoezhang/live2d-widget
*/
function loadWidget(waifuPath, apiPath) {
function loadWidget(config) {
let { waifuPath, apiPath } = config;
localStorage.removeItem("waifu-display");
sessionStorage.removeItem("waifu-text");
document.body.insertAdjacentHTML("beforeend", `<div id="waifu">
@ -220,7 +221,13 @@ function loadWidget(waifuPath, apiPath) {
}
}
function initWidget(waifuPath = "/waifu-tips.json", apiPath = "") {
function initWidget(config, apiPath = "") {
if (typeof config === "string") {
config = {
waifuPath: config,
apiPath
};
}
document.body.insertAdjacentHTML("beforeend", `<div id="waifu-toggle">
<span>看板娘</span>
</div>`);
@ -228,7 +235,7 @@ function initWidget(waifuPath = "/waifu-tips.json", apiPath = "") {
toggle.addEventListener("click", () => {
toggle.classList.remove("waifu-toggle-active");
if (toggle.getAttribute("first-time")) {
loadWidget(waifuPath, apiPath);
loadWidget(config);
toggle.removeAttribute("first-time");
} else {
localStorage.removeItem("waifu-display");
@ -244,6 +251,6 @@ function initWidget(waifuPath = "/waifu-tips.json", apiPath = "") {
toggle.classList.add("waifu-toggle-active");
}, 0);
} else {
loadWidget(waifuPath, apiPath);
loadWidget(config);
}
}