2015-12-04 28 views
1

我想嘗試一些新的ECMAScript功能,但與LightTable集成的瀏覽器沒有這些功能。爲此,我需要連接到外部瀏覽器和該LightTable需要此行:如何將LightTable從控制檯動態連接到外部瀏覽器?

<script type='text/javascript' id='lt_ws' src='http://localhost:53742/socket.io/lighttable/ws.js'></script> 

我想:

document.head.innerHTML+="<script type='text/javascript' id='lt_ws' src='http://localhost:53742/socket.io/lighttable/ws.js'></script>" 

但LightTable仍然沒有看到連接:

"No client available. We don't know what kind of client you want for this one. Try starting a client by choosing one of the connection types in the connect panel."

如何將其更改爲可以粘貼到控制檯選項卡中的JavaScript代碼,以便將LightTable從控制檯動態連接到外部瀏覽器?

+0

只要'document.head.appendChild(...)' – Bergi

+0

@Bergi試過沒有工作,這就是爲什麼我在這裏 –

+0

什麼沒有工作呢? – Bergi

回答

1

感謝Bergi指出innerHTML無法正常工作,我必須改用DOM方法。 使用瀏覽器粘貼控制檯連接的LightTable中的代碼。

腳本將需要並詢問端口,because this changes and every window uses a port這需要手動插入。

要看看你需要什麼介紹端口: 按下Ctrl鍵+空間,連接類型,然後選擇Connect to a browser。你會看到那裏的端口顯示在HTML代碼片段的URL中。

var port = prompt("What's the port number?"); 
var script_tag = document.createElement("script"); 
script_tag.setAttribute("src", "http://localhost:"+port+"/socket.io/lighttable/ws.js"); 
script_tag.setAttribute("type", "text/javascript"); 
script_tag.setAttribute("id", "lt_ws"); 
document.head.appendChild(script_tag); 

另外這款作品也作爲一個書籤:

javascript:(function()%7Bvar port %3D prompt("What's the port number%3F")%3Bvar script_tag %3D document.createElement("script")%3Bscript_tag.setAttribute("src"%2C "http%3A%2F%2Flocalhost%3A"%2Bport%2B"%2Fsocket.io%2Flighttable%2Fws.js")%3Bscript_tag.setAttribute("type"%2C "text%2Fjavascript")%3Bscript_tag.setAttribute("id"%2C "lt_ws")%3Bdocument.head.appendChild(script_tag)%7D)() 

這種方法的好處是,你可以連接並嘗試或調試的東西已經加載的實際網頁使用LightTable。

相關問題