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

后端 未结 2 1430
时光说笑
时光说笑 2021-01-15 17:54

I need to open console and run one function on new tab, that I opened using javascrip. The opening part is easy, but how to run function on other tab?

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 18:22

    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:

    //store the function in localStorage
    localStorage.runThis = function(){ alert("Hello world"); }
    //open the popup window
    newWindow = window.open("http://your-domain.com/your-page");
    

    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
    • 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.

提交回复
热议问题