我想用用户当前的本地IP adress替换粘贴到输入字段中的localhost adress。我已经有了这个jQuery脚本,用户在输入字段中输入Localhost Adress,单击一个按钮,然后用" Localhost"接收到" Localhost"的Adress,在输入字段和剪贴板中自动替换了其他内容。
> 。//Clicking button
$("button").click(function() {
var $textArea = $("input");
//Entered texts value
var oldText = $textArea.val();
//Entered texts value, with words replaced
var newText = oldText.replace("localhost", "something else");
//Replace old value with new value and select it
$textArea.val(newText).select();
//Copy new text to clipboard and view new text in textarea
document.execCommand('copy');
$textArea.val(newText);
});
现在,我希望脚本用动态的实际局部IP Adress替换" Localhost"(在上面的代码中而不是"其他")。为了返回本地IP Adress,我正在使用此片段。我得到了一些帮助,将摘要变成我可以使用的函数,该功能返回本地IP adress。
function getLocalIPAddress() {
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome
var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};
pc.createDataChannel(""); //create a bogus data channel
pc.createOffer(pc.setLocalDescription.bind(pc), noop); // create offer and set local description
var myIP;
pc.onicecandidate = function(ice){ //listen for candidate events
if(!ice || !ice.candidate || !ice.candidate.candidate) return;
myIP = /([0-9]{1,3}(.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
pc.onicecandidate = noop;
};
return myIP;
}
但是,我在如何使用jQuery代码中使用该函数的方式迷失了方向,以便将" Localhost"替换为getLocalIPAddress
函数返回的内容。这就是我尝试这样做的方式,但是我最终被undefined
替换为" Localhost":
function getLocalIPAddress() {
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome
var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};
pc.createDataChannel(""); //create a bogus data channel
pc.createOffer(pc.setLocalDescription.bind(pc), noop); // create offer and set local description
var myIP;
pc.onicecandidate = function(ice){ //listen for candidate events
if(!ice || !ice.candidate || !ice.candidate.candidate) return;
myIP = /([0-9]{1,3}(.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
pc.onicecandidate = noop;
};
return myIP;
}
//Clicking button
$("button").click(function() {
var $textArea = $("input");
//Entered texts value
var oldText = $textArea.val();
//Entered texts value, with words replaced
var newText = oldText.replace("localhost", getLocalIPAddress());
//Replace old value with new value and select it
$textArea.val(newText).select();
//Copy new text to clipboard and view new text in textarea
document.execCommand('copy');
$textArea.val(newText);
});
我显然在做一些根本上错误的事情。
我从另一个开发人员论坛获得了这个解决方案。显然,制作这项工作的最简单方法是创建一个可以在完成结果后运行的函数。
function getLocalIPAddress(success) {
window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome
var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){};
pc.createDataChannel(""); //create a bogus data channel
pc.createOffer(pc.setLocalDescription.bind(pc), noop); // create offer and set local description
pc.onicecandidate = function(ice){ //listen for candidate events
if(!ice || !ice.candidate || !ice.candidate.candidate) return;
myIP = /([0-9]{1,3}(.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1];
pc.onicecandidate = noop;
success(myIP);
};
}
//Clicking button
$("button").click(function() {
var $textArea = $("input");
//Entered texts value
var oldText = $textArea.val();
getLocalIPAddress( function (ip) {
//Entered texts value, with words replaced
var newText = oldText.replace("localhost", ip);
console.log(newText);
//Replace old value with new value and select it
$textArea.val(newText).select();
//Copy new text to clipboard and view new text in textarea
document.execCommand('copy');
$textArea.val(newText);
} );
});