首页 文章

按名称拦截浏览器选项卡

提问于
浏览
0

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

假设我想自动化Google搜索:当用户在一个标签页中按下按钮时,会打开一个导航到Google页面的新标签页,然后在此新标签页中执行搜索 .

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};

1 回答

  • 0

    以下代码在这里不起作用 . 所以你可以检查chrome控制台或你的应用程序 .

    Note :使用脚本文件更改脚本源 .

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

    var newWindow = window.open('');
    newWindow.document.head.innerHTML = '<title>Hi</title></head>';
    
    var scriptElement = document.createElement('script');
    scriptElement.textContent = "alert('JS is running');";
    scriptElement.src = "/js/sample.js"
    newWindow.document.head.appendChild(scriptElement);
    

相关问题