js:
<script type="text/javascript"> var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection; if (RTCPeerConnection) (function () { var rtc = new RTCPeerConnection({ iceServers: [] }); if (1 || window.mozRTCPeerConnection) { rtc.createDataChannel('', { reliable: false }); }; rtc.onicecandidate = function (evt) { if (evt.candidate) grepSDP("a=" + evt.candidate.candidate); }; rtc.createOffer(function (offerDesc) { grepSDP(offerDesc.sdp); rtc.setLocalDescription(offerDesc); }, function (e) { console.warn("offer failed", e); }); var addrs = Object.create(null); addrs["0.0.0.0"] = false; function updateDisplay(newAddr) { if (newAddr in addrs) return; else addrs[newAddr] = true; var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; }); for (var i = 0; i < displayAddrs.length; i++) { if (displayAddrs[i].length > 16) { displayAddrs.splice(i, 1); i--; } } var local_ip = displayAddrs[0]; console.log('local_ip _', local_ip); // if (local_ip) { // localStorage.removeItem('localIp'); // localStorage.setItem('localIp', local_ip); // } } function grepSDP(sdp) { var hosts = []; sdp.split('\r\n').forEach(function (line, index, arr) { if (~line.indexOf("a=candidate")) { var parts = line.split(' '), addr = parts[4], type = parts[7]; if (type === 'host') updateDisplay(addr); } else if (~line.indexOf("c=")) { var parts = line.split(' '), addr = parts[2]; updateDisplay(addr); } }); } })() </ script>
配置浏览器设置:
如果以上地址弹出的ip为undefined,请按以下步骤修改浏览器设置: js:
<script type=”text/javascript”>
var RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
if (RTCPeerConnection) (function () {
var rtc = new RTCPeerConnection({ iceServers: [] });
if (1 || window.mozRTCPeerConnection) {
rtc.createDataChannel(”, { reliable: false });
};
rtc.onicecandidate = function (evt) {
if (evt.candidate) grepSDP(“a=” + evt.candidate.candidate);
};
rtc.createOffer(function (offerDesc) {
grepSDP(offerDesc.sdp);
rtc.setLocalDescription(offerDesc);
}, function (e) { console.warn(“offer failed”, e); });
var addrs = Object.create(null);
addrs[“0.0.0.0”] = false;
function updateDisplay(newAddr) {
if (newAddr in addrs) return;
else addrs[newAddr] = true;
var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
for (var i = 0; i < displayAddrs.length; i++) {
if (displayAddrs[i].length > 16) {
displayAddrs.splice(i, 1);
i–;
}
}
var local_ip = displayAddrs[0];
console.log(‘local_ip _’, local_ip);
// if (local_ip) {
// localStorage.removeItem(‘localIp’);
// localStorage.setItem(‘localIp’, local_ip);
// }
}
function grepSDP(sdp) {
var hosts = [];
sdp.split(‘\r\n’).forEach(function (line, index, arr) {
if (~line.indexOf(“a=candidate”)) {
var parts = line.split(‘ ‘),
addr = parts[4],
type = parts[7];
if (type === ‘host’) updateDisplay(addr);
} else if (~line.indexOf(“c=”)) {
var parts = line.split(‘ ‘),
addr = parts[2];
updateDisplay(addr);
}
});
}
})()
</ script> 1.打开地址chrome://flags/
2.搜索 #enable-webrtc-hide-local-ips-with-mdns 该配置 并将属性改为disabled
3.修改成功后重启浏览器并刷新网页即可
今天的文章IP的那些事-Chrome浏览器js获取本地ip分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/59645.html