在replace()(jQuery)

时间:2016-11-24 10:47:35

标签: javascript jquery

我想用当前本地IP地址替换粘贴到输入字段的本地主机地址。我已经有了这个jQuery脚本,用户在输入字段中输入localhost地址,点击一个按钮,然后用" localhost"在输入字段和剪贴板中自动替换为其他内容。

//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);
});

现在我希望脚本替换" localhost"动态地使用实际的本地IP地址(而不是"其他内容",在上面的代码中)。为了返回本地IP地址,我使用了this代码段。我得到了一些帮助,将代码段转换为我可以使用的函数,该函数返回本地IP地址。

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函数返回的内容替换。这就是我尝试这样做的方式,但我最终得到了#34; localhost"替换为undefined

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);
});

我显然做了一些根本错误的事情。

1 个答案:

答案 0 :(得分:0)

我从其他开发者论坛获得此解决方案。显然,最简单的方法是创建一个可以在结果完成时运行的函数。

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);

    } );
});