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

How can I dynamically connect LightTable to an external browser from console?

我想尝试一些新的 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 仍然看不到连接:

"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 动态连接到外部浏览器?

感谢 Bergi 在评论中指出 innerHTML 不起作用,我必须改用 DOM 方法。 在控制台中粘贴以下代码将 LightTable 与浏览器连接起来。

脚本需要询问端口,because this changes and every window uses a port这需要手动插入。

查看需要引入什么端口: 按 Ctrl + Space,键入 connect,然后按 select 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 进行连接和尝试或调试。