检测错误:javascript中google auth2的"popup_blocked_by_browser"

ale*_*ela 5 google-app-engine popup popup-blocker

经过大量的谷歌搜索没有找到解决方案如何捕获google auth2的窗口弹出窗口阻止程序的错误

控制台错误中出错:"popup_blocked_by_browser".我想做的就是告诉用户应该为auth启用弹出窗口.

使用window.open()的样本不好,因为它们打开了无用的窗口.正如我看到很多人在寻找这个.

有什么建议?

小智 5

有同样的问题.似乎浏览器(或至少是chrome)将阻止任何"window.open"调用,它不是作为用户交互的一部分而调用的.

请参阅此处以获得更深入的解释.

__

我以前在click事件监听器中有下一个代码:

gapi.load('auth2', function() {
  var auth2 = gapi.auth2.init({
    client_id: '.apps.googleusercontent.com',
    fetch_basic_profile: true,
    scope: 'profile'
  });
  auth2.signIn()
    .then(function() {
      var profile = auth2.currentUser.get().getBasicProfile();
      ... 
    })
    .catch(function(err) { ... });
});
Run Code Online (Sandbox Code Playgroud)

请注意加载'auth2'的异步方式,这是google docu所说的.

我改成了:

// way earlier, before click event can happen
// we call the gapi.load method.
gapi.load('auth2', function() {});
Run Code Online (Sandbox Code Playgroud)

然后,在内部点击事件处理程序中,我们可以:

var auth2 = gapi.auth2.init({
    client_id: '.apps.googleusercontent.com',
    fetch_basic_profile: true,
    scope: 'profile'
  });
  auth2.signIn()
    .then(function() { ... })
    .catch(function(err) { ... });
Run Code Online (Sandbox Code Playgroud)

...所以浏览器不会阻止谷歌登录弹出窗口


ale*_*ela 0

最后!!SignIn() 方法使用 JS Promise。所以可以使用的代码是:

gapi.auth2.getAuthInstance().signIn().then(function(){}, function(error){ if (error) alert('please allow popup for this app')})
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助!