2016-11-24 21 views
0

入力フィールドに貼り付けたローカルホストのアドレスを、ユーザーの現在のローカルIPアドレスで置き換えたいとします。私はすでに入力フィールドにlocalhostアドレスを入力し、ボタンをクリックして "localhost"を入力フィールドとクリップボードに置き換えたアドレスを受信するこのjQueryスクリプトを手に入れました。replace()(jQuery)でローカルIPを書き出す

//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アドレスを「localhost」を交換したいです。ローカルの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; 
} 

しかし、私は一種のように「localhost」を何getLocalIPAddress関数が返すと交換され、私はjQueryのコード内の関数を使用するだろうかで迷ってしまいました。これは私がそれを実行しようとしました方法ですが、私は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); 
}); 

を。

+0

コンソールで実行したときにgetLocalIPAddressが返すのは何ですか?関数がデータを返さないので、未定義が使用されているようです。 –

+0

WebRTCコードが非同期であるため、関数から値を返すことはできません。コールバック関数で置き換えを実行する必要があります。 – Barmar

答えて

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

    }); 
}); 
関連する問題