chext
Version:
CHrome EXtension Tester
76 lines (65 loc) • 2.35 kB
JavaScript
function getLocalIPs(callback) {
var ips = [];
var RTCPeerConnection = window.RTCPeerConnection ||
window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
var pc = new RTCPeerConnection({
// Don't specify any stun/turn servers, otherwise you will
// also find your public IP addresses.
iceServers: []
});
// Add a media line, this is needed to activate candidate gathering.
pc.createDataChannel('');
// onicecandidate is triggered whenever a candidate has been found.
pc.onicecandidate = function(e) {
if (!e.candidate) {
// Candidate gathering completed.
callback(ips);
return;
}
var ip = /^candidate:.+ (\S+) \d+ typ/.exec(e.candidate.candidate)[1];
if (ips.indexOf(ip) == -1) // avoid duplicate entries (tcp/udp)
ips.push(ip);
};
pc.createOffer(function(sdp) {
pc.setLocalDescription(sdp);
}, function onerror() {});
}
window.reporter_websocket = new WebSocket("ws://localhost:10101")
window.relayTestResults = function relayTestResults(testResults){
console.log("relaying test results", testResults)
testResults.forEach(function(res){
reporter_websocket.send(JSON.stringify(res))
})
}
window.background = true;
getLocalIPs(function(ips){
window.testWorker = new Worker("mocha/worker.js")
testWorker.onmessage = function(e){
console.log("got message from testWorker",e)
if (e.data.results){
console.log("worker final tests" )
relayTestResults([e.data])
//report test back to node.js
}
}
chrome.windows.create({url :["http://localhost:9999/test.html", "test.html", "http://"+ ips[0] + ":9999/test.html"], focused: false}, function(e){
var extension = e.tabs[1].id
, localhost_fake_HTTPS = e.tabs[0].id
, remote_HTTP = e.tabs[2].id;
window.finishTests = function(testResults ){
relayTestResults([testResults])
chrome.tabs.remove(extension)
}
chrome.runtime.onMessage.addListener(
function(testResults, sender, sendResponse) {
relayTestResults(testResults)
if( testResults.length === 2)
testResults = testResults[0]
if (testResults.environment.indexOf("https") >= 0)
chrome.tabs.remove(localhost_fake_HTTPS);
else
chrome.tabs.remove(remote_HTTP);
}
);
});
})