如何从控制台动态连接LightTable到外部浏览器?

时间:2015-12-04 15:01:39

标签: javascript ecmascript-6 ecmascript-5 lighttable

我想尝试一些新的ECMAScript功能,但与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仍然没有看到连接:

  

“没有客户可用。我们不知道您想要什么样的客户   这个。尝试通过选择其中一个连接来启动客户端   连接面板中的类型。“

如何将其更改为可以在控制台选项卡中粘贴的JavaScript代码,以便从控制台动态连接LightTable到外部浏览器?

1 个答案:

答案 0 :(得分:1)

感谢Bergi指向innerHTML不起作用的评论,我不得不使用DOM方法。 使用浏览器将控制台连接的LightTable中的代码粘贴到下面。

脚本将需要并询问端口,{}需要手动插入because this changes and every window uses a port

要查看您需要介绍的端口: 按 Ctrl + Space ,键入connect,然后选择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连接和尝试或调试内容。