mirror of
https://github.com/RhenCloud/Cloud-Home.git
synced 2026-01-22 17:39:07 +08:00
chore: 格式化代码
This commit is contained in:
@@ -1,100 +1,102 @@
|
||||
import { defineNuxtPlugin } from "#app";
|
||||
import siteConfig from "~/config/siteConfig";
|
||||
|
||||
type NeteaseMiniPlayerGlobal = {
|
||||
init?: () => void;
|
||||
};
|
||||
|
||||
type NeteaseWindow = Window & {
|
||||
NeteaseMiniPlayer?: NeteaseMiniPlayerGlobal;
|
||||
__NETEASE_MUSIC_CONFIG__?: unknown;
|
||||
};
|
||||
|
||||
export default defineNuxtPlugin(() => {
|
||||
if (import.meta.server) return;
|
||||
|
||||
// 检查配置是否启用了音乐播放器
|
||||
if (!siteConfig.music?.enable) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 在本地开发环境禁用网易音乐播放器,避免网络超时
|
||||
// if (
|
||||
// typeof window !== "undefined" &&
|
||||
// (window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1")
|
||||
// ) {
|
||||
// console.log("Netease Music Player disabled on localhost");
|
||||
// return;
|
||||
// }
|
||||
|
||||
const cssHref = "/css/netease-mini-player-v2.css";
|
||||
const scriptSrc = "/js/netease-mini-player-v2.js";
|
||||
|
||||
const ensureStyle = () => {
|
||||
if (document.querySelector(`link[href="${cssHref}"]`)) return;
|
||||
const link = document.createElement("link");
|
||||
link.rel = "stylesheet";
|
||||
link.href = cssHref;
|
||||
link.onerror = () => {
|
||||
console.warn("Failed to load Netease music player styles");
|
||||
};
|
||||
document.head.appendChild(link);
|
||||
};
|
||||
|
||||
const ensureScript = () =>
|
||||
new Promise<void>((resolve) => {
|
||||
// 检查全局对象是否已存在,表示脚本已加载
|
||||
const anyWin = window as NeteaseWindow;
|
||||
if (anyWin.NeteaseMiniPlayer) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const existing = document.querySelector(`script[src="${scriptSrc}"]`) as HTMLScriptElement | null;
|
||||
if (existing) {
|
||||
// 脚本已存在但未加载,等待它加载
|
||||
existing.addEventListener("load", () => resolve(), { once: true });
|
||||
existing.addEventListener("error", () => resolve(), { once: true });
|
||||
return;
|
||||
}
|
||||
|
||||
// 脚本不存在,创建并加载
|
||||
const script = document.createElement("script");
|
||||
script.src = scriptSrc;
|
||||
script.async = true;
|
||||
script.onload = () => resolve();
|
||||
script.onerror = () => {
|
||||
console.warn("Failed to load Netease music player script");
|
||||
resolve();
|
||||
};
|
||||
document.body.appendChild(script);
|
||||
});
|
||||
|
||||
const initPlayer = () => {
|
||||
const anyWin = window as NeteaseWindow;
|
||||
|
||||
// 将 siteConfig 的音乐配置传递给全局 window 对象
|
||||
if (!anyWin.__NETEASE_MUSIC_CONFIG__) {
|
||||
anyWin.__NETEASE_MUSIC_CONFIG__ = siteConfig.music;
|
||||
}
|
||||
|
||||
if (anyWin.NeteaseMiniPlayer?.init) {
|
||||
try {
|
||||
anyWin.NeteaseMiniPlayer.init();
|
||||
} catch (error) {
|
||||
console.warn("Failed to initialize Netease music player:", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 使用超时机制防止永久挂起
|
||||
const timeout = setTimeout(() => {
|
||||
console.warn("Netease music player initialization timeout");
|
||||
}, 15000);
|
||||
|
||||
ensureStyle();
|
||||
ensureScript().then(() => {
|
||||
clearTimeout(timeout);
|
||||
initPlayer();
|
||||
});
|
||||
});
|
||||
import { defineNuxtPlugin } from "#app";
|
||||
import siteConfig from "~/config/siteConfig";
|
||||
|
||||
type NeteaseMiniPlayerGlobal = {
|
||||
init?: () => void;
|
||||
};
|
||||
|
||||
type NeteaseWindow = Window & {
|
||||
NeteaseMiniPlayer?: NeteaseMiniPlayerGlobal;
|
||||
__NETEASE_MUSIC_CONFIG__?: unknown;
|
||||
};
|
||||
|
||||
export default defineNuxtPlugin(() => {
|
||||
if (import.meta.server) return;
|
||||
|
||||
// 检查配置是否启用了音乐播放器
|
||||
if (!siteConfig.music?.enable) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 在本地开发环境禁用网易音乐播放器,避免网络超时
|
||||
// if (
|
||||
// typeof window !== "undefined" &&
|
||||
// (window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1")
|
||||
// ) {
|
||||
// console.log("Netease Music Player disabled on localhost");
|
||||
// return;
|
||||
// }
|
||||
|
||||
const cssHref = "/css/netease-mini-player-v2.css";
|
||||
const scriptSrc = "/js/netease-mini-player-v2.js";
|
||||
|
||||
const ensureStyle = () => {
|
||||
if (document.querySelector(`link[href="${cssHref}"]`)) return;
|
||||
const link = document.createElement("link");
|
||||
link.rel = "stylesheet";
|
||||
link.href = cssHref;
|
||||
link.onerror = () => {
|
||||
console.warn("Failed to load Netease music player styles");
|
||||
};
|
||||
document.head.appendChild(link);
|
||||
};
|
||||
|
||||
const ensureScript = () =>
|
||||
new Promise<void>((resolve) => {
|
||||
// 检查全局对象是否已存在,表示脚本已加载
|
||||
const anyWin = window as NeteaseWindow;
|
||||
if (anyWin.NeteaseMiniPlayer) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
const existing = document.querySelector(
|
||||
`script[src="${scriptSrc}"]`
|
||||
) as HTMLScriptElement | null;
|
||||
if (existing) {
|
||||
// 脚本已存在但未加载,等待它加载
|
||||
existing.addEventListener("load", () => resolve(), { once: true });
|
||||
existing.addEventListener("error", () => resolve(), { once: true });
|
||||
return;
|
||||
}
|
||||
|
||||
// 脚本不存在,创建并加载
|
||||
const script = document.createElement("script");
|
||||
script.src = scriptSrc;
|
||||
script.async = true;
|
||||
script.onload = () => resolve();
|
||||
script.onerror = () => {
|
||||
console.warn("Failed to load Netease music player script");
|
||||
resolve();
|
||||
};
|
||||
document.body.appendChild(script);
|
||||
});
|
||||
|
||||
const initPlayer = () => {
|
||||
const anyWin = window as NeteaseWindow;
|
||||
|
||||
// 将 siteConfig 的音乐配置传递给全局 window 对象
|
||||
if (!anyWin.__NETEASE_MUSIC_CONFIG__) {
|
||||
anyWin.__NETEASE_MUSIC_CONFIG__ = siteConfig.music;
|
||||
}
|
||||
|
||||
if (anyWin.NeteaseMiniPlayer?.init) {
|
||||
try {
|
||||
anyWin.NeteaseMiniPlayer.init();
|
||||
} catch (error) {
|
||||
console.warn("Failed to initialize Netease music player:", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 使用超时机制防止永久挂起
|
||||
const timeout = setTimeout(() => {
|
||||
console.warn("Netease music player initialization timeout");
|
||||
}, 15000);
|
||||
|
||||
ensureStyle();
|
||||
ensureScript().then(() => {
|
||||
clearTimeout(timeout);
|
||||
initPlayer();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,29 +1,29 @@
|
||||
import { defineNuxtPlugin } from "#app";
|
||||
import { VueUmamiPlugin } from "@jaseeey/vue-umami-plugin";
|
||||
import type { Router } from "vue-router";
|
||||
import siteConfig from "~/config/siteConfig";
|
||||
|
||||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
if (!import.meta.client) return;
|
||||
if (!siteConfig.umami?.enable) return;
|
||||
|
||||
// 跳过在 localhost 环境下加载 Umami
|
||||
if (
|
||||
typeof window !== "undefined" &&
|
||||
(window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1")
|
||||
) {
|
||||
console.log("Umami plugin skipped on localhost");
|
||||
return;
|
||||
}
|
||||
|
||||
const router = nuxtApp.$router as Router | undefined;
|
||||
if (!router) return;
|
||||
|
||||
nuxtApp.vueApp.use(
|
||||
VueUmamiPlugin({
|
||||
websiteID: siteConfig.umami.websiteId,
|
||||
scriptSrc: siteConfig.umami.url,
|
||||
router,
|
||||
})
|
||||
);
|
||||
});
|
||||
import { defineNuxtPlugin } from "#app";
|
||||
import { VueUmamiPlugin } from "@jaseeey/vue-umami-plugin";
|
||||
import type { Router } from "vue-router";
|
||||
import siteConfig from "~/config/siteConfig";
|
||||
|
||||
export default defineNuxtPlugin((nuxtApp) => {
|
||||
if (!import.meta.client) return;
|
||||
if (!siteConfig.umami?.enable) return;
|
||||
|
||||
// 跳过在 localhost 环境下加载 Umami
|
||||
if (
|
||||
typeof window !== "undefined" &&
|
||||
(window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1")
|
||||
) {
|
||||
console.log("Umami plugin skipped on localhost");
|
||||
return;
|
||||
}
|
||||
|
||||
const router = nuxtApp.$router as Router | undefined;
|
||||
if (!router) return;
|
||||
|
||||
nuxtApp.vueApp.use(
|
||||
VueUmamiPlugin({
|
||||
websiteID: siteConfig.umami.websiteId,
|
||||
scriptSrc: siteConfig.umami.url,
|
||||
router,
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user