Skip to content

Instantly share code, notes, and snippets.

@blha303
Created May 1, 2017 05:57
Show Gist options
  • Save blha303/c35468f56b1f7a57adc96615164ac03d to your computer and use it in GitHub Desktop.
Save blha303/c35468f56b1f7a57adc96615164ac03d to your computer and use it in GitHub Desktop.
A bookmarklet to redirect to a web service running on the lan interface
// I wanted a bookmarklet to take me to the local instance of Plex running on my desktop, so Chromecast would function correctly
// (if I used localhost it wouldn't send the correct uri)
// For this I had to get the local ethernet interface IP
// I initiate a connection to 0.0.0.0, then use the data on the connection object to get the interface IP used for that connection
// May not work on the most popular web browser
var path = prompt("Enter uri (e.g http://{}:32400/web)");
// var path = "http://{}:32400/web";
var RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
RTCPeerConnection && function() {
function c(ip) {
ip in b || (window.location.href = path.replace(/{}/g, ip))
}
function d(a) {
a.split("\r\n").forEach(function(a) {
~a.indexOf("a=candidate") ? "host" === a.split(" ")[7] && c(a.split(" ")[4]) : ~a.indexOf("c=") && c(a.split(" ")[2])
})
}
var a = new RTCPeerConnection({
iceServers: []
});
a.createDataChannel("", {
reliable: !1
}), a.onicecandidate = function(a) {
a.candidate && d("a=" + a.candidate.candidate)
}, a.createOffer(function(b) {
d(b.sdp), a.setLocalDescription(b)
}, function(a) {
console.warn("offer failed", a)
});
var b = Object.create(null);
b["0.0.0.0"] = !1
}();
javascript:var path=prompt("Enter uri (e.g http://{}:32400/web)");var RTCPeerConnection=window.webkitRTCPeerConnection||window.mozRTCPeerConnection;RTCPeerConnection&&function(){function c(ip){ip in b||(window.location.href=path.replace(/{}/g, ip))}function d(a){a.split("\r\n").forEach(function(a){~a.indexOf("a=candidate")?"host"===a.split(" ")[7]&&c(a.split(" ")[4]):~a.indexOf("c=")&&c(a.split(" ")[2])})}var a=new RTCPeerConnection({iceServers:[]});a.createDataChannel("",{reliable:!1}),a.onicecandidate=function(a){a.candidate&&d("a="+a.candidate.candidate)},a.createOffer(function(b){d(b.sdp),a.setLocalDescription(b)},function(a){console.warn("offer failed",a)});var b=Object.create(null);b["0.0.0.0"]=!1}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment