因为Net总是抽风,所以搭建了镜像Net,但是却没有合适、快捷的方法跳转到可用的Net,所以使用 html + Javascript 来实现了这个功能。

教程

以下是可用于 TypeScript 的代码

function getStatus(url: string){
    return new Promise((resolve, reject) => {
        let link = document.createElement('link');
        link.rel="stylesheet";
        link.type="text/css"
        link.href = url;

        link.onload = function(){
            resolve(true);
        }
        link.onerror = function(){
            resolve(false);
        }

        document.body.appendChild(link);
    })
}

但是我们需要的是 html + Javascript,所以改成下面的代码:

let link = document.createElement('link');
link.rel="stylesheet";
link.type="text/css"
link.href = "https://example.com";
link.onload = function(){
    //地址可用时执行的代码
}
link.onerror = function(){
    //地址不可用时执行的代码
}
document.body.appendChild(link);

上面的代码,其中的https://example.com就是我们要检测的地址,通过请求css的方式完成。

与 html 结合

对于链接的跳转,可以直接在 script 部分加入跳转代码:

window.location.href="https://example.com"

例如:

<script type="text/javascript">
    let link = document.createElement('link');
    link.rel="stylesheet";
    link.type="text/css"
    link.href = "https://example.com";
    link.onload = function(){
        window.location.href="https://example.com" //判断成功跳转地址
    }
    link.onerror = function(){
        window.location.href="https://not.example.com"
    }
    document.body.appendChild(link);
</script>

当然,如果要显示指定元素的话,可以采取下面的办法:

<h1 align="center" id="text">正在检测地址,请稍候...</h1>
<script type="text/javascript">
    let link = document.createElement('link');
    link.rel="stylesheet";
    link.type="text/css"
    link.href = "https://example.com";
    link.onload = function(){
        document.getElementById("text").innerHTML = "检测成功,该地址可用"; //替换h1文本
    }
    link.onerror = function(){
        document.getElementById("text").innerHTML = "检测失败,该地址不可用";
    }
    document.body.appendChild(link);
</script>