让信仰走进生活。让基督更新生命,让爱温暖你心。
浏览器收藏夹快速获取当前公网IP
  • 首页 > 技术分享
  • 作者:醉清风
  • 2021年11月4日 16:19 星期四
  • 浏览:5619583
  • 字号:
  • 评论:0
  • 前言

    小杰经常会使用的当前电脑公网IP,在不同环境下,获取公网IP的方式有很多,小杰之前常用的就是为了方便直接使用百度搜索“IP”,由于最近百度老是抽筋,以至于现在搜搜“IP”都没有弹出直接获取当前ip定位的那个小模块了,习惯了他的方式,现在没了,很难受,所以想自己搞一个快速、简单、方便且准确的方式获取IP公网地址

    因为之前小杰也写过很多类似的接口,可以参考

    73d41567563392.png

    工具箱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编码的网站访问时将出现中文乱码,属于正常现象。

    本文转至:youn小杰

      您阅读这篇文章共花了:  
    本文作者:醉清风      文章标题: 浏览器收藏夹快速获取当前公网IP
    本文地址:http://www.ndxy.cc/?post=31
    版权声明:若无注明出处,本文皆为“七十二棵棕树-让信仰走进生活。”原创,转载请保留文章出处。