检测抓包工具
function CheckProxy() {
var enumeration = java.net.NetworkInterface.getNetworkInterfaces();
while (enumeration.hasMoreElements()) {
var name = enumeration.nextElement().getName();
if (name == "tun0") {
return true;
}
}
return false;
}
捕获所有Http异常
(function() {
let request = http.request;
http.request = function() {
try {
return request.apply(http, arguments);
} catch (e) {
console.error(e);
return null;
}
}
})();
判断 当前日期时间 是否到时
if (GetTimeOutResult("2020-04-21 16:46:43", "second", -2)) {
console.warn("yes 到时间了");
} else {
console.warn("no 没到时间");
}
function GetTimeOutResult(oldDateTime, calculationAttribute, num) {
var nowTime = GetFormatDateTime(new Date());
var oldTime = new Date(oldDateTime.replace(/-/g, "/"));
if (calculationAttribute && calculationAttribute.length > 2) {
switch (calculationAttribute.toLowerCase()) {
case "year":
oldTime.setFullYear(oldTime.getFullYear() + num);
break;
case "month":
oldTime.setMonth(oldTime.getMonth() + num);
break;
case "day":
oldTime.setDate(oldTime.getDate() + num);
break;
case "hour":
oldTime.setHours(oldTime.getHours() + num);
break;
case "minute":
oldTime.setMinutes(oldTime.getMinutes() + num);
break;
case "second":
oldTime.setSeconds(oldTime.getSeconds() + num);
break;
default:
break;
}
}
oldTime = GetFormatDateTime(oldTime);
console.warn("当前时间: " + nowTime);
console.warn("目标时间: " + oldTime);
return nowTime > oldTime;
}
function GetFormatDateTime(date) {
var year = date.getFullYear();
var month = DateTimePrefixZero(date.getMonth() + 1);
var day = DateTimePrefixZero(date.getDate());
var hour = DateTimePrefixZero(date.getHours());
var minute = DateTimePrefixZero(date.getMinutes());
var second = DateTimePrefixZero(date.getSeconds());
var millisecond = DateTimePrefixZero(date.getMilliseconds());
return (
year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second
);
function DateTimePrefixZero(s) {
if (s < 10) {
s = "0" + s;
}
return s;
}
}
前缀 补0
function PrefixZero(num, n) {
return (Array(n).join(0) + num).slice(-n);
}
后缀 补0 __(小数点后2位)
function SuffixZero(x) {
var f_x = parseFloat(x);
if (isNaN(f_x)) {
return 0;
}
var f_x = Math.round(x * 100) / 100;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf(".");
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += ".";
}
while (s_x.length <= pos_decimal + 2) {
s_x += "0";
}
return s_x;
}
获取 当前日期时间 yyyy-MM-dd HH:mm:ss
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"H+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
S: this.getMilliseconds(),
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(
RegExp.$1,
(this.getFullYear() + "").substr(4 - RegExp.$1.length)
);
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
);
return fmt;
};
function GetNowTime() {
return new Date().Format("yyyy-MM-dd HH:mm:ss");
}
取随机数
function GetRandom(min, max) {
var num = Math.floor(Math.random() * (max - min) + min);
return num;
}
计时器
var TimerHelper = Timer();
TimerHelper.timerSign(1);
if(TimerHelper.timer(1, 2 * 60))
{
console.log("到时间了")
}
function Timer() {
var timerPool = Array();
var func = {};
func.timerSign = function (id) {
timerPool[id] = new Date().getTime();
};
func.timer = function (id, diff) {
var nowTime = new Date().getTime();
if (nowTime - timerPool[id] >= diff * 1000) {
timerPool[id] = new Date().getTime();
return true;
} else {
return false;
}
};
return func;
}
计算时间差 (用来 显示运行时长)
function TimeDis(startTime) {
var start_time = Date.parse(startTime);
var end_time = Date.parse(new Date());
if (end_time < start_time) {
return false;
} else {
var time_dis = end_time - start_time;
var days = Math.floor(time_dis / (24 * 3600 * 1000));
var leave1 = time_dis % (24 * 3600 * 1000);
var hours = Math.floor(leave1 / (3600 * 1000));
if (days > 0) {
hours = hours + days * 24;
}
var leave2 = leave1 % (3600 * 1000);
var minutes = Math.floor(leave2 / (60 * 1000));
var leave3 = leave2 % (60 * 1000);
var second = leave3 / 1000;
return hours + "时" + minutes + "分" + second + "秒";
}
}
获取控件的值
function GetUiText(id) {
return ui[id].getText() + "";
}
function GetUISwitch(id) {
return ui[id].checked;
}
Json 转 JavaMap
function ObjectToMap(obj) {
if (obj == null || obj === undefined) {
return null;
}
let map = new java.util.HashMap();
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
map.put(key, obj[key]);
}
}
return map;
}