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

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

本文关键字:连接 外部 浏览器 动态 LightTable 控制台      更新时间:2023-09-26

我想尝试一些新的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动态连接到外部浏览器?

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

脚本将需要并询问端口,因为这会发生变化,并且每个窗口都使用一个端口,所以需要手动插入。

要查看您需要介绍的端口:按Ctrl+空格,键入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进行连接、尝试或调试。