前言
小杰经常会使用的当前电脑公网IP,在不同环境下,获取公网IP的方式有很多,小杰之前常用的就是为了方便直接使用百度搜索“IP”,由于最近百度老是抽筋,以至于现在搜搜“IP”都没有弹出直接获取当前ip定位的那个小模块了,习惯了他的方式,现在没了,很难受,所以想自己搞一个快速、简单、方便且准确的方式获取IP公网地址
因为之前小杰也写过很多类似的接口,可以参考
工具箱:https://tools.yum6.cn/Tools/ip/
下面我们将使用到小杰API接口:https://api.yum6.cn/ip.php 进行IP的获取,接口源码过于凌乱就不发布了。
同类收藏夹工具《浏览器收藏夹一键获取网站ip》
教程
将下面的代码保存成一个收藏即可,使用时打开网站点击一下这个收藏即可。
javascript:with(document)0[body.appendChild(createElement('script')).src='//api.yum6.cn/getIp.js?v1'];
下面是Js源代码你们可以参考及修改成自己喜欢的样子
/*创建div*/
var d = document.createElement("div"),
x = document.createElement("div");
/*导入数据*/
var arr = ['正在获取请等待……'];
d.innerHTML = arr.join("<br>");
d.setAttribute("id", "show-loading");
d.style["z-index"] = 199710;
d.style.textAlign = "canter";
d.style.display = "block";
d.style.position = "fixed";
d.style.top = d.style.right = "5px";
d.style.padding = "5px 20px 5px 10px";
d.style.background = "#fff";
d.style.color = "#000";
d.style.font = "12px Arial";
d.style.lineHeight = "18px";
d.style.border = "1px solid #ccc";
x.innerHTML = "x";
x.title = "关闭";
x.style.position = "absolute";
x.style.color = "red";
x.style.padding = "3px";
x.style.cursor = "pointer";
x.style.font = "14px Arial";
x.style.top = x.style.right = 0;
x.onclick = function() {
d.style.display = "none";
};
d.appendChild(x);
window.showAddressInfoWin = d;
document.body.appendChild(d);
/*ajax获取数据*/
var ajax = new XMLHttpRequest();
ajax.open('get', 'https://api.yum6.cn/ip.php');
ajax.send();
ajax.onreadystatechange = function() {
l = document.getElementById('show-loading');
if (ajax.readyState == 4 && ajax.status == 200) {
console.log(ajax.responseText);
var obj = JSON.parse(ajax.responseText);
// l.style.display = 'none';
l.parentNode.removeChild(l);
/*创建div*/
var d = document.createElement("div"),
x = document.createElement("div");
/*导入数据*/
var arr = ['IP:' + obj.ip, '地区:' + obj.location, 'Longip:' + obj.longip, 'Ipv4:' + obj.ipv4, 'Network:' + obj.network];
d.innerHTML = arr.join("<br>");
d.style["z-index"] = 199710;
d.style.textAlign = "canter";
d.style.display = "block";
d.style.position = "fixed";
d.style.top = d.style.right = "5px";
d.style.padding = "5px 20px 5px 10px";
d.style.background = "#fff";
d.style.color = "#000";
d.style.font = "12px Arial";
d.style.lineHeight = "18px";
d.style.border = "1px solid #ccc";
x.innerHTML = "x";
x.title = "关闭";
x.style.position = "absolute";
x.style.color = "red";
x.style.padding = "3px";
x.style.cursor = "pointer";
x.style.font = "14px Arial";
x.style.top = x.style.right = 0;
x.onclick = function() {
d.style.display = "none";
};
d.appendChild(x);
window.showAddressInfoWin = d;
document.body.appendChild(d);
} else {
l.innerHTML = '糟糕!是意外的情况。';
}
}
该源码请以UTF-8的编码方式保存,在GB2312/GBK编码的网站访问时将出现中文乱码,属于正常现象。 














发表评论: