Bilibili-Evolved/utils/notify-new-version.js
2019-03-21 22:21:49 +08:00

105 lines
3.4 KiB
JavaScript

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}已发布. <a id="new-version-link" class="link" href="${settings.latestVersionLink}">安装</a><a class="link" target="_blank" href="https://github.com/the1812/Bilibili-Evolved/releases">查看</a>`;
const toast = Toast.info(message, "检查更新");
$("#new-version-link").on("click", () => toast && toast.dismiss());
}
return hasNewVersion;
}
export default {
widget:
{
content: /*html*/`
<button
class="gui-settings-flat-button"
id="new-version-update">
<a href="${settings.latestVersionLink}" style="display:none"></a>
<i class="icon-update"></i>
<span>安装更新</span>
</button>
<button
class="gui-settings-flat-button"
id="new-version-info">
<a target="blank" style="display:none" href="https://github.com/the1812/Bilibili-Evolved/releases"></a>
<i class="icon-info"></i>
<span>查看更新</span>
</button>
`,
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();
}
});
},
},
};