简体   繁体   English

您如何在父窗口中访问有关子窗口的信息?

[英]How do you access information about a child window in the parent window?

I'm currently trying to figure out how to use React to open a new child window, execute authenticate and then write back to the parent page that the authentication is complete. 我目前正在试图弄清楚如何使用React打开一个新的子窗口,执行身份验证,然后写回到身份验证已完成的父页面。

I have a button labeled "Add New" which when you click on it opens a popup window that has a button on the page. 我有一个标记为“添加新”的按钮,当您单击它时,将打开一个弹出窗口,该页面上有一个按钮。 When you click the button, a new window opens so that the customer can accept the OAuth request in their account, and then the window reroutes to the callback URL upon successful authentication. 当您单击按钮时,将打开一个新窗口,以便客户可以在其帐户中接受OAuth请求,然后在成功进行身份验证后,该窗口将重新路由到回调URL。 However, I want to be able to stop the customer from seeing it reroute, and rather have the window close upon successful auth completion, as well as for the parent page to have information written to it where I can see that the auth was done and trigger the next function. 但是,我希望能够阻止客户看到它的重新路由,而希望成功完成身份验证后关闭窗口,并且使父页面上写入信息,这样我就可以看到身份验证已完成并且触发下一个功能。 Any ideas on how to do this? 有关如何执行此操作的任何想法?

I tried going through the different Window functions to see if there was anything that returned the current URL of the child window, so that I could do an if statement that says when the URL = the callback URL, shut down the window and write to the page, but I've had no luck. 我尝试遍历不同的Window函数以查看是否有任何返回子窗口当前URL的东西,以便我可以执行if语句,当URL =回调URL时,关闭窗口并写入页,但我没有运气。 It seems like once the new window opens, there's no information retained by the parent window about that window. 新窗口打开后,父窗口似乎没有关于该窗口的任何信息。 Any ideas on how to get the information on the new child window written to the parent window would be greatly appreciated. 任何关于如何在新的子窗口上获取信息的信息都将写入父窗口。

In case it would help, I pasted some of the functions below. 如果有帮助,我粘贴了以下一些功能。

Below is the AuthButton component, which is called in the App component. 下面是AuthButton组件,在App组件中被调用。 The AuthButton component opens the popup, which includes the button that will open the authentication window. AuthButton组件将打开弹出窗口,其中包含将打开身份验证窗口的按钮。

<AuthButton
      activatePopup={this.activatePopup.bind(this)}
      auth={this.auth.bind(this)}
      showPopup={this.state.showPopup}
      closePopup={this.activateAuthPopup.bind(this)}
/>

This is the button component that opens up the new child window: 这是打开新的子窗口的按钮组件:

class Button extends Component {
    constructor(props) {
        super(props);
        this.state = {
        };
    }
    render() {
        return (
            <div>
                <button 
                style={{
                    backgroundColor: "fff", 
                    backgroundImage: `url(${logo})`, 
                    backgroundSize: '70%', 
                    color: "#000", 
                    width: "18em", 
                    height: "10em", 
                    backgroundRepeat: "no-repeat", 
                    backgroundPosition: "center", 
                    margin: "10px",
                    borderStyle: "solid",
                    borderColor: "#000",
                    borderWidth: "2px",
                    borderRadius: "5px"
                }} 
                onClick = {
                    this.props.auth
                }></button>
          </div>
        );
    }
}

Within the App.js file in the App component there is an auth function (listed below). 在App组件的App.js文件中,有一个auth函数(在下面列出)。 Axios is hitting a route in the app, and it returns the auth URL and we take that URL and execute window.open which opens a new window where the customer can authenticate. Axios在应用程序中找到路由,并返回身份验证URL,我们采用该URL并执行window.open,这将打开一个新窗口,客户可以在此进行身份验证。

  auth = () => {
      axios('/auth/oauth/authorize', {
        headers: {
          "Access-Control-Allow-Origin": true,
          'maxRedirects': 0
        }
      })
      .then(response => {
        console.log("Response", response);
        console.log("URL", response.data.href);
        var strWindowFeatures = "width=1000,height=800,resizable,scrollbars=yes,status=1";
        var windowpop = window.open(response.data.href, null, strWindowFeatures)
        console.log("Window Location Href", windowpop.location.href);
      })
      .catch(error => {
        console.log('Error fetching and parsing data', error);
      });
  }

The Window Location Href returns 'about:blank'. 窗口位置Href返回'about:blank'。

Let me know if I can provide any other information! 让我知道是否可以提供其他信息! Thank you! 谢谢!

So to solve this issue, we are currently closing the window in the route functions when the auth is successful, and in the React function I am running a timer that looks for if window.closed is true or false, and if it is true than we are changing certain states. 因此,要解决此问题,我们当前在身份验证成功时关闭route函数中的窗口,并且在React函数中,我运行一个计时器,用于查找window.closed是true还是false,以及是否true我们正在改变某些状态。

auth = () => {
      axios('/auth/oauth/authorize', {
        headers: {
          "Access-Control-Allow-Origin": true,
          'maxRedirects': 0
        }
      })
      .then(response => {
        var strWindowFeatures = "width=1000,height=800,resizable,scrollbars=yes,status=1";
        var windowpop = window.open(response.data.href, null, strWindowFeatures)
        this.setState({
          authUrl: response.data.href
        })
        windowpop.focus()
        var newThis = this; 
        var timer = setInterval(function() {   
            if(windowpop.closed) {  
                clearInterval(timer);
                newThis.setState({
                  logo: logo,
                  opacity: 0.5,
                  success: true
                });
                console.log('closed');  
            }  
        }, 1000); 
      })
      .catch(error => {
        console.log('Error fetching and parsing data', error);
      });
  }

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

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