Skip to content

Commit 5764f2f

Browse files
committed
Refactor Search Console API initialization and error handling
Moved the gapi client initialization and authentication logic outside of the initClient function for clarity. Improved error handling by consolidating the showError function and ensuring consistent error reporting throughout the authentication and API loading process.
1 parent 1b42701 commit 5764f2f

1 file changed

Lines changed: 31 additions & 24 deletions

File tree

SearchConsoleMonitor.html

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,40 @@ <h1>Indexed Pages Count</h1>
1515

1616
<script>
1717
function initClient() {
18-
gapi.load('client:auth2', () => {
19-
gapi.auth2.init({
20-
client_id: '830015351192-38tmspassocrp65dok2mo83d54g9gtav.apps.googleusercontent.com',
21-
scope: 'https://www.googleapis.com/auth/webmasters.readonly'
22-
}).then(() => {
23-
gapi.auth2.getAuthInstance().signIn().then(() => {
24-
gapi.client.load('searchconsole', 'v1').then(fetchSitemaps)
25-
.catch(err => showError('API load error', err));
26-
}).catch(err => showError('Sign-in error', err));
27-
}).catch(err => showError('Init error', err));
28-
});
29-
}
18+
gapi.load('client:auth2', () => {
19+
gapi.auth2.init({
20+
client_id: '830015351192-38tmspassocrp65dok2mo83d54g9gtav.apps.googleusercontent.com',
21+
scope: 'https://www.googleapis.com/auth/webmasters.readonly'
22+
}).then(() => {
23+
gapi.auth2.getAuthInstance().signIn()
24+
.then(() => {
25+
gapi.client.load('searchconsole', 'v1').then(fetchSitemaps)
26+
.catch(err => showError('API load error', err));
27+
})
28+
.catch(err => showError('Sign-in error', err));
29+
}).catch(err => showError('Init error', err));
30+
});
31+
}
3032

31-
function fetchSitemaps() {
32-
gapi.client.searchconsole.sitemaps.list({
33-
siteUrl: 'https://pwindows.qzz.io/'
34-
}).then(response => {
35-
if (response.result.sitemap && response.result.sitemap.length > 0) {
36-
const data = response.result.sitemap[0].contents[0];
37-
document.getElementById('output').innerText =
38-
`Submitted: ${data.submitted}, Indexed: ${data.indexed}`;
39-
} else {
40-
document.getElementById('output').innerText = 'No sitemap data found.';
41-
}
42-
}).catch(err => showError('Fetch error', err));
33+
function fetchSitemaps() {
34+
gapi.client.searchconsole.sitemaps.list({
35+
siteUrl: 'https://pwindows.qzz.io/'
36+
}).then(response => {
37+
if (response.result.sitemap && response.result.sitemap.length > 0) {
38+
const data = response.result.sitemap[0].contents[0];
39+
document.getElementById('output').innerText =
40+
`Submitted: ${data.submitted}, Indexed: ${data.indexed}`;
41+
} else {
42+
document.getElementById('output').innerText = 'No sitemap data found.';
4343
}
44+
}).catch(err => showError('Fetch error', err));
45+
}
4446

47+
function showError(label, err) {
48+
console.error(label, err);
49+
document.getElementById('output').innerText =
50+
`${label}: ${JSON.stringify(err, null, 2)}`;
51+
}
4552
function showError(label, err) {
4653
console.error(label, err);
4754
document.getElementById('output').innerText =

0 commit comments

Comments
 (0)