const CompareResult = { less: -1, equal: 0, greater: 1, incomparable: NaN }; // Based on http://jsfiddle.net/ripper234/Xv9WL/28/ class Version { constructor(versionString) { this.parts = versionString.split('.').map(it => parseInt(it)); this.versionString = versionString; } compareTo(other) { for (let i = 0; i < this.parts.length; ++i) { if (other.parts.length === i) { return CompareResult.greater; } if (this.parts[i] === other.parts[i]) { continue; } if (this.parts[i] > other.parts[i]) { return CompareResult.greater; } return CompareResult.less; } if (this.parts.length !== other.parts.length) { return CompareResult.less; } return CompareResult.equal; } greaterThan(other) { return this.compareTo(other) === CompareResult.greater; } lessThan(other) { return this.compareTo(other) === CompareResult.less; } equals(other) { return this.compareTo(other) === CompareResult.equal; } } async function checkNewVersion() { const latestVersion = new Version(resources.data.latestVersion ? resources.data.latestVersion.text : await Ajax.getText(Resource.root + "version.txt")); const currentVersion = new Version(settings.currentVersion); const hasNewVersion = latestVersion.greaterThan(currentVersion); if (hasNewVersion) { const message = `新版本${latestVersion.versionString}已发布. 安装查看`; const toast = Toast.info(message, "检查更新"); $("#new-version-link").on("click", () => toast && toast.dismiss()); } return hasNewVersion; } export default { widget: { content: /*html*/` `, condition: checkNewVersion, success: () => { document.querySelector("#new-version-update").addEventListener("click", e => { if (e.target.nodeName.toLowerCase() !== "a") { document.querySelector("#new-version-update a").click(); } }); document.querySelector("#new-version-info").addEventListener("click", e => { if (e.target.nodeName.toLowerCase() !== "a") { document.querySelector("#new-version-info a").click(); } }); }, }, };