首页 文章

谷歌登录使用自定义文本的网站

提问于
浏览
2

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

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

Google signin

现在我正在尝试将按钮的文本从“登录”更改为“与Google连接”但是没有发生任何事情......

我的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和外部链接,它仍然无法工作..请帮助..

2 回答

  • 1

    来自谷歌的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 .

  • 3

    或者您可以更改使用Javascript生成的范围文本,尝试使用代码中的“DivID”替换您的Div ID

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

相关问题