1 2 // quick terminal->textarea simulation 3 var log = (function(){ 4 var area=document.querySelector("#serverlog"); 5 var output=function(str) { 6 if (str.length>0 && str.charAt(str.length-1)!='\n') { 7 str+='\n' 8 } 9 area.innerText=str+area.innerText; 10 if (console) console.log(str); 11 }; 12 return {output: output}; 13 })(); 14 15 chrome.runtime.getBackgroundPage(function(bgPage) { 16 17 bgPage.log.addListener(function(str) { 18 log.output(str); 19 }); 20 21 bgPage.TcpServer.getNetworkAddresses(function(list) { 22 var addr=document.querySelector("#addresses"); 23 for (var i=0; i<list.length; i++) { 24 if (/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(list[i].address)) { 25 var option = document.createElement('option'); 26 option.text = list[i].name+" ("+list[i].address+")"; 27 option.value = list[i].address; 28 addr.appendChild(option); 29 } 30 }; 31 }); 32 33 function setConnectedState(addr, port) { 34 document.querySelector(".serving-at").innerText=addr+":"+port; 35 document.querySelector("#server").className="connected"; 36 } 37 38 function setStoppedState() { 39 document.querySelector("#server").className=""; 40 } 41 42 document.getElementById('serverStart').addEventListener('click', function() { 43 var addr=document.getElementById("addresses").value; 44 var port=parseInt(document.getElementById("serverPort").value); 45 setConnectedState(addr, port); 46 bgPage.startServer(addr, port); 47 }); 48 49 document.getElementById('serverStop').addEventListener('click', function() { 50 setStoppedState(); 51 bgPage.stopServer(); 52 }) 53 54 var currentState=bgPage.getServerState(); 55 if (currentState.isConnected) { 56 setConnectedState(currentState.addr, currentState.port); 57 } 58 59 }) 60 61