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

时间:2016-01-08 13:11:50

标签: javascript jquery google-oauth google-signin

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

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

Google signin

现在我尝试更改按钮的文字,从#34;登录"到"与谷歌联系"但没有任何事情发生..

我的Javascript代码..

           function onSignIn(googleUser) {

            var profile = googleUser.getBasicProfile();
            loginpassword = profile.getId();
            loginemail =  profile.getEmail();
        };

HTML代码..

     <div class="g-signin2" data-onsuccess="onSignIn">Connect with Google</div>

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

3 个答案:

答案 0 :(得分:4)

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

要覆盖它,您需要build your own custom button。来自谷歌的例子:

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

请注意,您可以找到实际的图标文件on Google's server for download

设置好样式和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>

请注意,您必须遵守Google's branding guidelines

答案 1 :(得分:1)

或者您也可以更改使用Javascript生成的跨度文本,请尝试以下操作 将代码中的Div ID替换为“ DivID”

setTimeout(function () {
        $('#DivID div div span span:last').text("Sign up with Google");
        $('#DivID div div span span:first').text("Sign up with Google");
    }, 3000);

答案 2 :(得分:1)

例如,对于HTML部分,我们有以下内容:

<div id="GoogeSigninButton" data-onsuccess="onSignIn" class="g-signin2"></div>

,对于按钮代码后的脚本标签,如下所示:

<script>
    setTimeout(function () {
        $('#GoogeSigninButton div div span span:last').text("Custom Sign in Text");
        $('#GoogeSigninButton div div span span:first').text("Custom Sign in Text");
        $('#GoogeSigninButton div div span span:last').css("font-family", "Arial");//for font formatting
        $('#GoogeSigninButton div div span span:first').css("font-family", "Arial");//for font formatting
    }, 10000);
</script>

在分页加载后约10秒钟后,它将使按钮的文本变为所需的自定义文本和自定义字体。