Google使用自定义文字登录网站

Luc*_*ucy 4 javascript jquery google-oauth google-signin

我已按照Google的官方教程为我的网站实现了Google登录:https : //developers.google.com/identity/sign-in/web/sign-in

使用上面的教程,我得到以下按钮

Google登录

现在,我正在尝试将按钮的文本从“登录”更改为“与Google连接”,但是什么也没有发生。

我的Javascript代码

           function onSignIn(googleUser) {

            var profile = googleUser.getBasicProfile();
            loginpassword = profile.getId();
            loginemail =  profile.getEmail();
        };
Run Code Online (Sandbox Code Playgroud)

HTML代码

     <div class="g-signin2" data-onsuccess="onSignIn">Connect with Google</div>
Run Code Online (Sandbox Code Playgroud)

知道如何更改文本吗?即使我提供了platform.js文件的客户端ID和外部链接,它仍然无法正常工作..请帮助。

Beo*_*ett 5

Google的platform.js库正在渲染按钮以及文本。

为了覆盖它,您需要构建自己的定制按钮。以Google为例:

<html>
<head>
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
  <script src="https://apis.google.com/js/api:client.js"></script>
  <script>
  var googleUser = {};
  var startApp = function() {
    gapi.load('auth2', function(){
      // Retrieve the singleton for the GoogleAuth library and set up the client.
      auth2 = gapi.auth2.init({
        client_id: 'YOUR_CLIENT_ID.apps.googleusercontent.com',
        cookiepolicy: 'single_host_origin',
        // Request scopes in addition to 'profile' and 'email'
        //scope: 'additional_scope'
      });
      attachSignin(document.getElementById('customBtn'));
    });
  };

  function attachSignin(element) {
    console.log(element.id);
    auth2.attachClickHandler(element, {},
        function(googleUser) {
          document.getElementById('name').innerText = "Signed in: " +
              googleUser.getBasicProfile().getName();
        }, function(error) {
          alert(JSON.stringify(error, undefined, 2));
        });
  }
  </script>
  <style type="text/css">
    #customBtn {
      display: inline-block;
      background: #4285f4;
      color: white;
      width: 190px;
      border-radius: 5px;
      white-space: nowrap;
    }
    #customBtn:hover {
      cursor: pointer;
    }
    span.label {
      font-weight: bold;
    }
    span.icon {
      background: url('{{path to your button image}}') transparent 5px 50% no-repeat;
      display: inline-block;
      vertical-align: middle;
      width: 42px;
      height: 42px;
      border-right: #2265d4 1px solid;
    }
    span.buttonText {
      display: inline-block;
      vertical-align: middle;
      padding-left: 42px;
      padding-right: 42px;
      font-size: 14px;
      font-weight: bold;
      /* Use the Roboto font that is loaded in the <head> */
      font-family: 'Roboto', sans-serif;
    }
  </style>
  </head>
Run Code Online (Sandbox Code Playgroud)

请注意,您可以在Google服务器上找到要下载的实际图标文件。

设置样式和Javascript后,您可以通过调用按钮本身HTML,此时您可以指定文本:

  <body>
  <!-- In the callback, you would hide the gSignInWrapper element on a
  successful sign in -->
  <div id="gSignInWrapper">
    <span class="label">Sign in with:</span>
    <div id="customBtn" class="customGPlusSignIn">
      <span class="icon"></span>
      <span class="buttonText">Google</span>
    </div>
  </div>
  <div id="name"></div>
  <script>startApp();</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

请注意,您必须遵守Google的品牌指南