|
7 | 7 | <head> |
8 | 8 | <div class="spacing-start"></div> |
9 | 9 | <title>Indexed Pages Monitor</title> |
10 | | - <script src="https://apis.google.com/js/api.js"></script> |
| 10 | + <!-- Load Google APIs client --> |
| 11 | + <script src="https://apis.google.com/js/api.js" onload="gapiLoaded()"></script> |
| 12 | + <!-- Load Google Identity Services --> |
| 13 | + <script src="https://accounts.google.com/gsi/client" async defer onload="gisLoaded()"></script> |
11 | 14 | </head> |
12 | 15 | <body> |
13 | 16 | <h1>Indexed Pages Count</h1> |
14 | | - <button onclick="initClient()">Authorize & Fetch</button> |
| 17 | + <button onclick="authorizeAndFetch()">Authorize & Fetch</button> |
15 | 18 | <div id="output"></div> |
16 | 19 |
|
17 | 20 | <script> |
18 | | - function initClient() { |
19 | | - gapi.load('client:auth2', () => { |
20 | | - gapi.auth2.init({ |
21 | | - client_id: '830015351192-38tmspassocrp65dok2mo83d54g9gtav.apps.googleusercontent.com', |
22 | | - scope: 'https://www.googleapis.com/auth/webmasters.readonly' |
23 | | - }).then(() => { |
24 | | - gapi.auth2.getAuthInstance().signIn() |
25 | | - .then(() => { |
26 | | - gapi.client.load('searchconsole', 'v1').then(fetchSitemaps) |
27 | | - .catch(err => showError('API load error', err)); |
28 | | - }) |
29 | | - .catch(err => showError('Sign-in error', err)); |
30 | | - }).catch(err => showError('Init error', err)); |
31 | | - }); |
32 | | -} |
| 21 | + let tokenClient; |
| 22 | + let gapiInited = false; |
33 | 23 |
|
34 | | -function fetchSitemaps() { |
35 | | - gapi.client.searchconsole.sitemaps.list({ |
36 | | - siteUrl: 'https://pwindows.qzz.io/' |
37 | | - }).then(response => { |
38 | | - if (response.result.sitemap && response.result.sitemap.length > 0) { |
39 | | - const data = response.result.sitemap[0].contents[0]; |
40 | | - document.getElementById('output').innerText = |
41 | | - `Submitted: ${data.submitted}, Indexed: ${data.indexed}`; |
42 | | - } else { |
43 | | - document.getElementById('output').innerText = 'No sitemap data found.'; |
| 24 | + // Load GAPI client |
| 25 | + function gapiLoaded() { |
| 26 | + gapi.load('client', initializeGapiClient); |
| 27 | + } |
| 28 | + |
| 29 | + async function initializeGapiClient() { |
| 30 | + await gapi.client.init({ |
| 31 | + discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/searchconsole/v1/rest'], |
| 32 | + }); |
| 33 | + gapiInited = true; |
| 34 | + } |
| 35 | + |
| 36 | + // Load GIS client |
| 37 | + function gisLoaded() { |
| 38 | + tokenClient = google.accounts.oauth2.initTokenClient({ |
| 39 | + client_id: '830015351192-38tmspassocrp65dok2mo83d54g9gtav.apps.googleusercontent.com', |
| 40 | + scope: 'https://www.googleapis.com/auth/webmasters.readonly', |
| 41 | + callback: (tokenResponse) => { |
| 42 | + if (tokenResponse.access_token) { |
| 43 | + fetchSitemaps(); |
| 44 | + } else { |
| 45 | + showError('Token error', tokenResponse); |
| 46 | + } |
| 47 | + }, |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + // Trigger authorization |
| 52 | + function authorizeAndFetch() { |
| 53 | + if (!gapiInited) { |
| 54 | + showError('Init error', {message: 'GAPI not initialized yet'}); |
| 55 | + return; |
| 56 | + } |
| 57 | + tokenClient.requestAccessToken(); |
| 58 | + } |
| 59 | + |
| 60 | + // Fetch sitemap data |
| 61 | + async function fetchSitemaps() { |
| 62 | + try { |
| 63 | + const response = await gapi.client.searchconsole.sitemaps.list({ |
| 64 | + siteUrl: 'https://www.pwindows.qzz.io/', |
| 65 | + }); |
| 66 | + if (response.result.sitemap && response.result.sitemap.length > 0) { |
| 67 | + const data = response.result.sitemap[0].contents[0]; |
| 68 | + document.getElementById('output').innerText = |
| 69 | + `Submitted: ${data.submitted}, Indexed: ${data.indexed}`; |
| 70 | + } else { |
| 71 | + document.getElementById('output').innerText = 'No sitemap data found.'; |
| 72 | + } |
| 73 | + } catch (err) { |
| 74 | + showError('Fetch error', err); |
| 75 | + } |
44 | 76 | } |
45 | | - }).catch(err => showError('Fetch error', err)); |
46 | | -} |
47 | 77 |
|
48 | | -function showError(label, err) { |
49 | | - console.error(label, err); |
50 | | - document.getElementById('output').innerText = |
51 | | - `${label}: ${JSON.stringify(err, null, 2)}`; |
52 | | -} |
| 78 | + // Error handler |
53 | 79 | function showError(label, err) { |
54 | 80 | console.error(label, err); |
55 | 81 | document.getElementById('output').innerText = |
|
0 commit comments