How to trigger click on “Save to Google Drive” button

烈酒焚心 提交于 2021-02-07 09:58:41

问题


How to trigger "click" on Save to Google Drive button, i mean, i have my own design for "Save to Google Drive" button. is there any programaticly trigger to open "save to google drive" popups in Javascript?

Thanks

EDIT

i want to modify this api

https://developers.google.com/drive/v3/web/savetodrive#getting_started


回答1:


You can try:

html:

<html>

<head>
  <title>Save to Drive</title>
</head>

<body>
  <input type="button" id="doitButton" value="Save Chat History in Drive">
  <input type="button" id="authorizeButton" value="Authorize" onClick="checkAuth()">
  <script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
</body>

</html>

js:

var CLIENT_ID = 'CLIENT_ID';
var SCOPES = 'https://www.googleapis.com/auth/drive';

function handleClientLoad() {
  window.setTimeout(checkAuth, 1);
}

function checkAuth() {
  gapi.auth.authorize({
    'client_id': CLIENT_ID,
    'scope': SCOPES,
    'immediate': true
  }, handleAuthResult);
}

function handleAuthResult(authResult) {
  var authButton = document.getElementById('authorizeButton');
  var doitButton = document.getElementById('doitButton');
  authButton.style.display = 'none';
  doitButton.style.display = 'none';
  if (authResult && !authResult.error) {
    // Access token has been successfully retrieved, requests can be sent to
    // the API.
    doitButton.style.display = 'block';
    doitButton.onclick = uploadFile;
  } else {
    // No access token could be retrieved, show the button to start the
    // authorization flow.
    authButton.style.display = 'block';
    authButton.onclick = function() {
      gapi.auth.authorize({
        'client_id': CLIENT_ID,
        'scope': SCOPES,
        'immediate': false
      }, handleAuthResult);
    };
  }
}

function uploadFile(evt) {
  gapi.client.load('drive', 'v2', function() {
    insertFile();
  });
}

function insertFile() {
  //YOUR INSERT CODE
}

As you can see, the handleAuthResult() get the OAuth result and conditionally check the authorization then if successful will add onclick="uploadFile()".



来源:https://stackoverflow.com/questions/37581127/how-to-trigger-click-on-save-to-google-drive-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!