简体   繁体   English

如何在主选项卡的新选项卡上运行功能? (谷歌浏览器)

[英]How to run function on new tab from main tab? (Google Chrome)

I need to open console and run one function on new tab, that I opened using javascrip. 我需要打开控制台并在新选项卡上运行一个功能,该功能是使用javascrip打开的。 The opening part is easy, but how to run function on other tab? 开头部分很容易,但是如何在其他选项卡上运行功能呢?

 var google = window.open("http://google.com") 

Upon reading your question, it seems you're looking to open the dev console for the popup? 读完您的问题后,您似乎想为弹出窗口打开开发者控制台? Assuming this is what you're looking for, you should just be able to right-click the popped-up window and hit 'Inspect Element'. 假设这是您要查找的内容,则只需右键单击弹出的窗口并单击“检查元素”即可。 Then go to the console from there. 然后从那里转到控制台。


If you're trying to programatically run a function from the parent onto the popup window, here's an idea for you. 如果您试图以编程方式将父级函数运行到弹出窗口中,那么这里就有个好主意。

Assuming the new window is on the same domain as yours, this solution may work for you. 假设新窗口与您位于同一域中,则此解决方案可能对您有效。 (browser support is limited) (浏览器支持有限)

On the parent page: 在父页面上:

//check if the function has been stored
if(typeof localStorage.runThis === "function"){
    //run the function in localStorage
    localStorage.runThis();
}

On the page to open in the popup: 在弹出窗口中打开:

 //check if the function has been stored if(typeof localStorage.runThis === "function"){ //run the function in localStorage localStorage.runThis(); } 

One issue is that this method relies on this criteria being met: 一个问题是该方法依赖于满足以下条件:

  • Browser supports localStorage 浏览器支持localStorage
  • Parent page and Popup page come from the same origin 父页面和弹出页面来自同一来源
  • Popup page must actually look for the function in question and execute the function itself 弹出页面实际上必须查找有问题的函数并执行函数本身

One drawback of this is that if someone were to go to the Javascript Console and set their own function into localStorage, the popup page would see their function and run potentially dangerous code - a security hole. 这样的一个缺点是,如果有人去Javascript Console并将自己的功能设置到localStorage中,则弹出页面将看到其功能并运行潜在危险的代码-一个安全漏洞。

A common solution is using localstorage . 常见的解决方案是使用localstorage

if (typeof(Storage) !== "undefined") {
  // Code for localStorage/sessionStorage.
  localStorage.setItem("lastname", "Smith");
  var lastname = localStorage.getItem("lastname");
} else {
    // Sorry! No Web Storage support..
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM