Javascript 按名称截取浏览器选项卡

Javascript 按名称截取浏览器选项卡,javascript,Javascript,一旦我打开了一个新的浏览器选项卡,我如何告诉JavaScript执行这个新打开的选项卡中接下来的代码,而不是第一个 假设我想自动化谷歌搜索:当用户按下一个选项卡中的按钮时,会打开一个新选项卡,导航到谷歌页面,然后在这个新选项卡中执行搜索 window.open("https://www.google.com"); //~here I need to intercept the new tab (preferably by tab name) and execute some_other_func

一旦我打开了一个新的浏览器选项卡,我如何告诉JavaScript执行这个新打开的选项卡中接下来的代码,而不是第一个

假设我想自动化谷歌搜索:当用户按下一个选项卡中的按钮时,会打开一个新选项卡,导航到谷歌页面,然后在这个新选项卡中执行搜索

window.open("https://www.google.com");
//~here I need to intercept the new tab (preferably by tab name) and execute some_other_function in it 
new_tab.onload = function() {some_other_function};

下面的代码在这里不起作用。因此,您可以在chrome控制台或应用程序中进行检查

注意:使用脚本文件更改脚本源

您可以创建脚本标记并添加脚本src,然后将其附加到新选项卡文档的头部

var newWindow=window.open(“”);
newWindow.document.head.innerHTML='Hi';
var scriptElement=document.createElement('script');
scriptElement.textContent=“警报('JS正在运行');”;
scriptElement.src=“/js/sample.js”

newWindow.document.head.appendChild(scriptElement)在打开/执行谷歌搜索的情况下:
窗口。打开('https://www.google.com/search?search+terms+here')
,但这利用了GET请求,与问题本身无关。是的,谷歌搜索只是我试图实现的一个简化示例。我将无法使用查询字符串参数。