mirror of
https://github.com/SilenceLurker/ST-Amily2-Chat-Optimisation.git
synced 2026-06-06 15:55:50 +00:00
Update index.js
This commit is contained in:
97
index.js
97
index.js
@@ -1,8 +1,40 @@
|
|||||||
|
|
||||||
import { createDrawer } from "./ui/drawer.js";
|
import { createDrawer } from "./ui/drawer.js";
|
||||||
import { registerSlashCommands } from "./core/commands.js";
|
import { registerSlashCommands } from "./core/commands.js";
|
||||||
import { onMessageReceived, onChatChanged } from "./core/events.js";
|
import { onMessageReceived, onChatChanged } from "./core/events.js";
|
||||||
import { eventSource, event_types } from '/script.js';
|
import { eventSource, event_types } from '/script.js';
|
||||||
|
import { checkForUpdates } from './core/api.js';
|
||||||
|
import { setUpdateInfo } from './ui/state.js';
|
||||||
|
import { pluginVersion } from './utils/settings.js';
|
||||||
|
|
||||||
|
|
||||||
|
function compareVersions(v1, v2) {
|
||||||
|
const parts1 = v1.split('.').map(Number);
|
||||||
|
const parts2 = v2.split('.').map(Number);
|
||||||
|
const len = Math.max(parts1.length, parts2.length);
|
||||||
|
|
||||||
|
for (let i = 0; i < len; i++) {
|
||||||
|
const p1 = parts1[i] || 0;
|
||||||
|
const p2 = parts2[i] || 0;
|
||||||
|
if (p1 > p2) return true;
|
||||||
|
if (p1 < p2) return false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleUpdateCheck() {
|
||||||
|
console.log("【Amily2号】帝国已就绪,现派遣外交官,为陛下探查外界新情报...");
|
||||||
|
const updateInfo = await checkForUpdates();
|
||||||
|
|
||||||
|
if (updateInfo && updateInfo.version) {
|
||||||
|
const isNew = compareVersions(updateInfo.version, pluginVersion);
|
||||||
|
if(isNew) {
|
||||||
|
console.log(`【Amily2号-情报部】捷报!发现新版本: ${updateInfo.version}。情报已转交内务府。`);
|
||||||
|
} else {
|
||||||
|
console.log(`【Amily2号-情报部】一切安好,帝国已是最新版本。情报已转交内务府备案。`);
|
||||||
|
}
|
||||||
|
setUpdateInfo(isNew, updateInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function loadPluginStyles() {
|
function loadPluginStyles() {
|
||||||
@@ -20,38 +52,18 @@ function loadPluginStyles() {
|
|||||||
document.head.appendChild(link);
|
document.head.appendChild(link);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
window.addEventListener("error", (event) => {
|
window.addEventListener("error", (event) => {
|
||||||
const stackTrace = event.error?.stack || "";
|
const stackTrace = event.error?.stack || "";
|
||||||
|
|
||||||
if (stackTrace.includes("ST-Amily2-Chat-Optimisation")) {
|
if (stackTrace.includes("ST-Amily2-Chat-Optimisation")) {
|
||||||
console.error("[Amily2-全局卫队] 捕获到严重错误:", event.error);
|
console.error("[Amily2-全局卫队] 捕获到严重错误:", event.error);
|
||||||
toastr.error(
|
toastr.error(`Amily2插件错误: ${event.error?.message || "未知错误"}`, "严重错误", { timeOut: 10000 });
|
||||||
`Amily2插件错误: ${event.error?.message || "未知错误"}`,
|
|
||||||
"严重错误",
|
|
||||||
{ timeOut: 10000 },
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener("error", (event) => {
|
|
||||||
const stackTrace = event.error?.stack || "";
|
|
||||||
|
|
||||||
if (stackTrace.includes("ST-Amily2-Chat-Optimisation")) {
|
|
||||||
console.error("[Amily2-全局错误]", event.error);
|
|
||||||
try {
|
|
||||||
toastr.error(
|
|
||||||
`Amily2插件错误: ${event.error?.message || "未知错误"}`,
|
|
||||||
"严重错误",
|
|
||||||
{ timeOut: 10000 },
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
|
|
||||||
console.error("无法显示错误提示", err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
jQuery(async () => {
|
jQuery(async () => {
|
||||||
|
console.log("[Amily2号-帝国枢密院] 开始执行开国大典...");
|
||||||
|
|
||||||
let attempts = 0;
|
let attempts = 0;
|
||||||
const maxAttempts = 100;
|
const maxAttempts = 100;
|
||||||
@@ -61,34 +73,39 @@ jQuery(async () => {
|
|||||||
const deploymentInterval = setInterval(async () => {
|
const deploymentInterval = setInterval(async () => {
|
||||||
if ($(targetSelector).length > 0) {
|
if ($(targetSelector).length > 0) {
|
||||||
clearInterval(deploymentInterval);
|
clearInterval(deploymentInterval);
|
||||||
console.log(
|
console.log("[Amily2号-帝国枢密院] 目标邻居已确认,开国大典正式开始!");
|
||||||
`[Amily2号] 目标邻居(${targetSelector})已定位,开始建造府邸...`
|
|
||||||
);
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log("[Amily2号-开国大典] 步骤一:为宫殿披上华服...");
|
||||||
|
loadPluginStyles();
|
||||||
|
|
||||||
loadPluginStyles();
|
console.log("[Amily2号-开国大典] 步骤二:皇家仪仗队就位...");
|
||||||
|
await registerSlashCommands();
|
||||||
|
|
||||||
|
console.log("[Amily2号-开国大典] 步骤三:开始召唤府邸...");
|
||||||
|
createDrawer();
|
||||||
|
|
||||||
await registerSlashCommands();
|
console.log("[Amily2号-开国大典] 步骤四:部署帝国哨兵网络...");
|
||||||
|
if (!window.amily2EventsRegistered) {
|
||||||
|
eventSource.on(event_types.MESSAGE_RECEIVED, onMessageReceived);
|
||||||
|
eventSource.on(event_types.IMPERSONATE_READY, onMessageReceived);
|
||||||
|
eventSource.on(event_types.CHAT_CHANGED, onChatChanged);
|
||||||
|
window.amily2EventsRegistered = true;
|
||||||
|
}
|
||||||
|
|
||||||
createDrawer();
|
console.log("【Amily2号】帝国秩序已完美建立。Amily2号的府邸已恭候陛下的莅临。");
|
||||||
|
|
||||||
if (!window.amily2EventsRegistered) {
|
handleUpdateCheck();
|
||||||
eventSource.on(event_types.MESSAGE_RECEIVED, onMessageReceived);
|
|
||||||
eventSource.on(event_types.IMPERSONATE_READY, onMessageReceived);
|
} catch (error) {
|
||||||
eventSource.on(event_types.CHAT_CHANGED, onChatChanged);
|
console.error("!!!【开国大典失败】在执行系列法令时发生严重错误:", error);
|
||||||
window.amily2EventsRegistered = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("【Amily2号】帝国秩序已建立。恭迎陛下检阅!");
|
|
||||||
} else {
|
} else {
|
||||||
attempts++;
|
attempts++;
|
||||||
if (attempts >= maxAttempts) {
|
if (attempts >= maxAttempts) {
|
||||||
clearInterval(deploymentInterval);
|
clearInterval(deploymentInterval);
|
||||||
console.error(
|
console.error(`[Amily2号] 部署失败:等待 ${targetSelector} 超时。`);
|
||||||
`[Amily2号] 部署失败:等待 ${targetSelector} 超时。帝国号角未能吹响。`
|
|
||||||
);
|
|
||||||
toastr.error("Amily2号UI部署失败。", "部署错误");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, checkInterval);
|
}, checkInterval);
|
||||||
|
|||||||
Reference in New Issue
Block a user