仅在用户单击按钮时触发Google登录功能

时间:2019-03-18 09:44:43

标签: javascript

点击以下用于Google登录按钮的代码。但是,此功能每次都在加载时触发,而不是在用户单击“登录”按钮时触发。即使用户不感兴趣,它也会通过在窗口中显示标志来影响用户体验。

除了onload函数外,以上代码按预期工作正常。是否只有在用户触发Google登录按钮后才能加载此功能?在此之前,请勿加载此功能。

我不确定如何从上述Javascript更改代码以及哪一行代码。

JS:

//Google Sign in    
var googleUser = {};
window.startApp = function() {
  gapi.load('auth2', function(){
    // Retrieve the singleton for the GoogleAuth library and set up the client.
    window.auth2 = gapi.auth2.init({
      client_id: 'xxxxxxxxxxxxxxx.apps.googleusercontent.com',
      cookiepolicy: 'single_host_origin',
      // Request scopes in addition to 'profile' and 'email'
      scope: 'profile email'
    });
    attachSignin(document.getElementById('googleLink'));
  });
};

window.onbeforeunload = function(e){
  gapi.auth2.getAuthInstance().signOut();
};

window.attachSignin= function attachSignin(element) {

  window.auth2.attachClickHandler(element, {},
    function(googleUser) {
      console.log('Signed in: ' +
          googleUser.getBasicProfile().getName());
    }, function(error) {

  });

  // Listen for sign-in state changes.user
  window.auth2.isSignedIn.listen(function(user){
    googleUser = auth2.currentUser.get();
    var profile = googleUser.getBasicProfile();
    that.vue.onGoogleLoginProcess();
  });
}

window.onload=(function(){
  startApp();
}).bind(this);

HTML:

<div id="googleLink" class="customGPlusSignIn">
    <span>Sign in with Google</span>
</div>

2 个答案:

答案 0 :(得分:0)

只需删除以下代码段即可。

window.onload=(function(){
  startApp();
}).bind(this);

并在onclick="startApp();"中添加div,如下所示。

<div id="googleLink" class="customGPlusSignIn" onclick="startApp();">
    <span>Sign in with Google</span>
</div>

答案 1 :(得分:-1)

您必须删除功能

library(shiny)

# a module that only has a text input
moduleUI = function(id){
    ns = NS(id)
    tagList(
        # the module is enclosed within a div with it's own id to allow it's removal
        div(id = id,
            textInput(ns('text'),label = 'text'))
    )
}

# the module simply returns it's input and reports when there is a change
module = function(input,output,session){
    observeEvent(input$text,{
        print('module working')
    })
    return(input$text)
}


# ui is iniated with add/remove buttons
ui = fluidPage(
    actionButton('add','+'),
    actionButton('remove','-')
)

server = function(input, output,session) {
    # number of ui elements already created
    uiCount = reactiveVal(0)

    moduleOuts = reactiveValues()

    observeEvent(input$add,{
        # count the UI elements created to assign new IDs
        uiCount(uiCount()+1)
        # insert module UI before the add/remove buttons
        insertUI('#add',
                 'beforeBegin',
                 moduleUI(paste0("module",uiCount())))

        # failed attempt to get the output
        moduleOuts[[as.character(uiCount())]] = callModule(module,id = paste0("module",uiCount()))
    })

    observeEvent(input$remove,{
        # if remove button is pressed, remove the UI and reduce the UI count
        removeUI(
            selector = paste0("#module",uiCount())
        )
        uiCount(uiCount()-1)
    })

    # simple test
    observe({
        print(moduleOuts[['1']])
    })

    # test to get all values
    observe({
        seq_len(uiCount()) %>% sapply(function(i){
            print(moduleOuts[[as.character(i)]])
        })
    })

}


shinyApp(ui = ui, server = server)

并在window.onload 中添加onclick="startApp();"