152 lines
4.8 KiB
JavaScript
152 lines
4.8 KiB
JavaScript
|
var $doc = $(document);
|
|||
|
|
|||
|
(function () {
|
|||
|
var lastStatus = 0;
|
|||
|
var initialized = false;
|
|||
|
|
|||
|
var updateState = function (state) {
|
|||
|
var html = "";
|
|||
|
|
|||
|
switch (state) {
|
|||
|
case 0:
|
|||
|
html = "<span class='label label-primary'>正常运行中...</span>";
|
|||
|
break;
|
|||
|
case 1:
|
|||
|
html = "<span class='label label-success'>正在从服务器更新节点列表...</span>";
|
|||
|
break;
|
|||
|
case 2:
|
|||
|
html = "<span class='label label-primary'>正在对节点测速中...</span>";
|
|||
|
break;
|
|||
|
case 3:
|
|||
|
html = "<span class='label label-primary'>正在筛选节点...</span>";
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
if (html)
|
|||
|
$("#currentStatus").html(html);
|
|||
|
};
|
|||
|
var loadServerList = function (list) {
|
|||
|
var html = ["<tr>"];
|
|||
|
_.each(list, function (s, i) {
|
|||
|
var cls = s.status < 0 ? "danger" : !s.status ? "active" : "success";
|
|||
|
var key = s.ip.replace(/\./g, "_");
|
|||
|
|
|||
|
html.push("<td class='" + cls + "' id='s_" + key + "'>");
|
|||
|
html.push(s.ip);
|
|||
|
html.push("</td><td class='" + cls + "'>");
|
|||
|
if (!s.status) {
|
|||
|
html.push("等待测速...");
|
|||
|
} else {
|
|||
|
html.push(s.status == -1 ? "连接失败" : s.status == -2 ? "速度过慢" : "速度 " + (s.speed / 1000) + " 秒");
|
|||
|
}
|
|||
|
html.push("</td>");
|
|||
|
|
|||
|
if (i % 4 === 3) {
|
|||
|
html.push("</tr><tr>");
|
|||
|
}
|
|||
|
});
|
|||
|
if (!list.length) {
|
|||
|
html.push("<td colspan='8'>当前没有服务器在等待速度测试。</td>");
|
|||
|
}
|
|||
|
html.push("</tr>");
|
|||
|
|
|||
|
$("#serverList tr:gt(0)").remove();
|
|||
|
$("#serverList").append(html.join(""));
|
|||
|
};
|
|||
|
$doc.bind("refreshCurrentServer", function () {
|
|||
|
chrome.runtime.sendMessage({ action: "getCurrentServer" }, function (m) {
|
|||
|
var otn = m["kyfw.12306.cn"];
|
|||
|
//var dynamic = m["dynamic.12306.cn"];
|
|||
|
|
|||
|
if (otn) {
|
|||
|
$("#currentServerOtn").html("正在使用 <code>" + (otn.ip ? otn.ip : "系统默认服务器") + "</code> 延迟 <code>" + (otn.speed > 0 ? otn.speed + "毫秒" : "未知") + "</code>" + (otn.count > 0 ? " 已过慢或失败 <code>" + otn.count + "</code> 次" : ""));
|
|||
|
} else {
|
|||
|
$("#currentServerOtn").html("<em>--状态未知--</em>");
|
|||
|
}
|
|||
|
//if (dynamic) {
|
|||
|
// $("#currentServerDynamic").html("正在使用 <code>" + (dynamic.ip ? dynamic.ip : "系统默认服务器") + "</code> 延迟 <code>" + (dynamic.speed > 0 ? dynamic.speed + "毫秒" : "未知") + "</code>" + (dynamic.count > 0 ? " 已过慢或失败 <code>" + dynamic.count + "</code> 次" : ""));
|
|||
|
//} else {
|
|||
|
// $("#currentServerDynamic").html("<em>--状态未知--</em>");
|
|||
|
//}
|
|||
|
$("#currentServer").html(m.ip ? m.ip : "系统默认服务器");
|
|||
|
$("#currentServerSpeed").html(m.speed > 0 ? (m.speed / 1000) + "毫秒" : "未知");
|
|||
|
});
|
|||
|
});
|
|||
|
$doc.bind("refreshServerStatus", function () {
|
|||
|
chrome.runtime.sendMessage({ action: "getServerStatus" }, function (m) {
|
|||
|
$("#currentServerStorage").html([
|
|||
|
"<span class='label label-primary'>总节点数 ",
|
|||
|
m.count,
|
|||
|
'</span> ',
|
|||
|
"<span class='label label-success'>可用节点数 ",
|
|||
|
m.valid,
|
|||
|
'</span> ',
|
|||
|
"<span class='label label-danger'>超时节点数 ",
|
|||
|
m.timeout,
|
|||
|
'</span> ',
|
|||
|
"<span class='label label-default'>无效节点数 ",
|
|||
|
m.failed,
|
|||
|
'</span> '
|
|||
|
].join(""));
|
|||
|
|
|||
|
if (m.status === 0) {
|
|||
|
var iplist = _.map(_.flatten(m.validList), function (e) { return "<code>" + e.ip + "</code> "; });
|
|||
|
$("#currentServerValidList").html(iplist.length ? iplist.join("") : "<em>暂无节点</em>");
|
|||
|
} else {
|
|||
|
$("#currentServerValidList").html("<em>节点列表正在更新中....</em>");
|
|||
|
}
|
|||
|
if (!initialized) {
|
|||
|
initialized = true;
|
|||
|
if (m.status === 2 || m.status === 3) {
|
|||
|
$doc.trigger("reloadServer");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
updateState(m.status);
|
|||
|
});
|
|||
|
});
|
|||
|
$doc.bind("reloadServer", function () {
|
|||
|
chrome.runtime.sendMessage({ action: "getServerList" }, function (list) {
|
|||
|
loadServerList(list);
|
|||
|
});
|
|||
|
$doc.trigger("refreshServerStatus");
|
|||
|
});
|
|||
|
|
|||
|
message.addAction("serverStateChange", function () {
|
|||
|
updateState(this.state);
|
|||
|
if (this.state === 2 || this.state === 3) {
|
|||
|
$doc.trigger("reloadServer");
|
|||
|
}
|
|||
|
$doc.trigger("refreshServerStatus");
|
|||
|
$doc.trigger("refreshCurrentServer");
|
|||
|
});
|
|||
|
message.addAction("serverTestResult", function () {
|
|||
|
var key = this.ip.replace(/\./g, "_");
|
|||
|
var cls = this.status < 0 ? "danger" : !this.status ? "active" : "success";
|
|||
|
var content = (this.status == -1 ? "连接失败" : this.status == -2 ? "速度过慢" : "速度 " + (this.speed / 1000) + " 秒");
|
|||
|
|
|||
|
$("#s_" + key).removeClass().addClass(cls).next().removeClass().addClass(cls).html(content);
|
|||
|
|
|||
|
$doc.trigger("refreshServerStatus");
|
|||
|
});
|
|||
|
|
|||
|
$doc.trigger("refreshServerStatus");
|
|||
|
$doc.trigger("refreshCurrentServer");
|
|||
|
})();
|
|||
|
|
|||
|
(function () {
|
|||
|
message.sendAction("servervalid", null, function (resp) {
|
|||
|
if (resp.valid === 0) {
|
|||
|
$("div.container").hide();
|
|||
|
$("#notValid").show();
|
|||
|
}
|
|||
|
if (resp.valid === -1) {
|
|||
|
$("div.container").hide();
|
|||
|
$("#inProxy").show();
|
|||
|
}
|
|||
|
if (resp.valid === -2) {
|
|||
|
$("div.container").hide();
|
|||
|
$("#greenMode").show();
|
|||
|
}
|
|||
|
});
|
|||
|
})();
|