Få fram lokalt IP-nummer som stringvariabel (JavaScript)

Permalänk
Medlem

Få fram lokalt IP-nummer som stringvariabel (JavaScript)

Hittade det här scriptet nyligen:

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; var 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]; console.log('my IP: ', myIP); pc.onicecandidate = noop; };

Det tar fram datorns lokala IP-nummer och skriver ut det i konsolen. Hur gör jag för att lagra IP-numret i en variabel så att jag kan sätta in det i någonting annat? Exempelvis: var newText = oldText.replace("localhost", IP-NUMRET HÄR);

Permalänk
99:e percentilen

Använd [code] när du postar kod.

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; var 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]; console.log('my IP: ', myIP); pc.onicecandidate = noop; };

Vi kan se att de har lagt IP-adressen i myIP. Låt oss slå in existerande kod i en funktion vi kan använda:

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

Att jag har flyttat deklarationen av myIP beror på att den måste finnas i den yttre funktionens scope, annars kan vi inte returnera den. Från början fanns den bara i den inre funktionens (temporärt pc.onicecandidate) scope.

Visa signatur

Skrivet med hjälp av Better SweClockers

Permalänk
Medlem

@Alling: Ah, jag hittade ingen tagg för det i menyn ovanför meddelanderutan, så antog att det inte fanns någon tagg för kod. Tack!

Jag vill slutligen kunna skriva in en localhost-adress i en textruta och sedan klicka på en knapp som då ersätter "localhost" med mitt lokala IP-nummer. Jag har skrivit lite jQuery som kan byta ut ett ord i textrutan mot ett annat och sedan kopiera detta. Jag försöker sätta in funktionen i koden så att "localhost" blir IP-numret, men jag har troligtvis gjort någonting väldigt fel, eftersom jag får 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() { $("input").removeClass("enter"); $("input").addClass("enter"); 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); });

Vad gör jag fel? Antar att jag måste konvertera funktionens resultat till en string eller så?

Permalänk
Medlem

Som jag ser det hämtas ip addressen i eventhandlern onicecandidate, det sker ju asynkront så värdet som returneras från getLocalIpAddress är undefined för att det är inte satt vid det tillfället.

Enklast skickar man ju in en function till getLocalIpAddress som körs när resultatet är klart

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

Permalänk
Medlem

@furbel: Fungerade precis som jag tänkte! Väldigt mycket tack! Och stort tack till dig @Alling också!

Permalänk
99:e percentilen
Skrivet av furbel:

Som jag ser det hämtas ip addressen i eventhandlern onicecandidate, det sker ju asynkront så värdet som returneras från getLocalIpAddress är undefined för att det är inte satt vid det tillfället.

Enklast skickar man ju in en function till getLocalIpAddress som körs när resultatet är klart

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

Där fick jag också lära mig något. Testade inte min kod (för det går inte på iOS) och tänkte inte alls på att originalkoden var asynkron, trots att det egentligen var tydligt. Tack så mycket!

Skickades från m.sweclockers.com

Visa signatur

Skrivet med hjälp av Better SweClockers