Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
varWikipediaAuth;varSCOPE='https://en.wikipedia.org/w/api.php';functionhandleClientLoad(){// Load the API's client and auth2 modules.// Call the initClient function after the modules load.api.load('client:auth2',initClient);}functioninitClient(){// In practice, your app can retrieve one or more discovery documents.vardiscoveryUrl='https://en.wikipedia.org/w/rest.php/v1/page/Earth';// Initialize the api.client object, which app uses to make API requests.// Get API key and client ID from API Console.// 'scope' field specifies space-delimited list of access scopes.api.client.init({'apiKey':'YOUR_API_KEY','clientId':'YOUR_CLIENT_ID','discoveryDocs':[discoveryUrl],'scope':SCOPE}).then(function(){WikipediaAuth=api.auth2.getAuthInstance();// Listen for sign-in state changes.WikipediaAuth.isSignedIn.listen(updateSigninStatus);// Handle initial sign-in state. (Determine if user is already signed in.)varuser=WikipediaAuth.currentUser.get();setSigninStatus();// Call handleAuthClick function when user clicks on// "Sign In/Authorize" button.$('#sign-in-or-out-button').click(function(){handleAuthClick();});$('#revoke-access-button').click(function(){revokeAccess();});});}functionhandleAuthClick(){if(WikipediaAuth.isSignedIn.get()){// User is authorized and has clicked "Sign out" button.WikipediaAuth.signOut();}else{// User is not signed in. Start Wikipedia auth flow.WikipediaAuth.signIn();}}functionrevokeAccess(){WikipediaAuth.disconnect();}functionsetSigninStatus(){varuser=WikipediaAuth.currentUser.get();varisAuthorized=user.hasGrantedScopes(SCOPE);if(isAuthorized){$('#sign-in-or-out-button').html('Sign out');$('#revoke-access-button').css('display','inline-block');$('#auth-status').html('You are currently signed in and have granted '+'access to this app.');}else{$('#sign-in-or-out-button').html('Sign In/Authorize');$('#revoke-access-button').css('display','none');$('#auth-status').html('You have not authorized this app or you are '+'signed out.');}}functionupdateSigninStatus(){setSigninStatus();}