1
+ function urlInspectionToolController ( editorState , notificationsService , umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource ) {
2
+ var vm = this ;
3
+
4
+ vm . loading = false ;
5
+ vm . showResults = false ;
6
+ vm . oauthConfiguration = { } ;
7
+ vm . inspectionResult = { } ;
8
+
9
+ // build default url inspection object
10
+ var nodeUrls = getUrls ( ) ;
11
+ vm . inspectionObj = {
12
+ urls : nodeUrls ,
13
+ inspectionUrl : nodeUrls [ 0 ] ,
14
+ siteUrl : window . location . origin ,
15
+ languageCode : editorState . current . urls [ 0 ] . culture ,
16
+ multipleUrls : editorState . current . urls . length > 1 ,
17
+ enabled : false
18
+ } ;
19
+
20
+ // get oauth configuration
21
+ umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource . getOAuthConfiguration ( ) . then ( function ( response ) {
22
+ vm . oauthConfiguration = response ;
23
+ } ) ;
24
+
25
+ vm . onConnectClick = function ( ) {
26
+ vm . authorizationWindow = window . open ( vm . oauthConfiguration . authorizationUrl ,
27
+ "GoogleSearchConsole_Authorize" ,
28
+ "width=900,height=700,modal=yes,alwaysRaised=yes" ) ;
29
+ }
30
+
31
+ vm . onRevokeToken = function ( ) {
32
+ revokeToken ( ) ;
33
+ }
34
+
35
+ vm . onInspect = function ( ) {
36
+
37
+ vm . loading = true ;
38
+
39
+ // check if url is relative
40
+ if ( isRelativeUrl ( vm . inspectionObj . inspectionUrl ) )
41
+ vm . inspectionObj . inspectionUrl = `${ vm . inspectionObj . siteUrl } ${ vm . inspectionObj . inspectionUrl } ` ;
42
+
43
+ umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource . inspect ( vm . inspectionObj . inspectionUrl , vm . inspectionObj . siteUrl , vm . inspectionObj . languageCode )
44
+ . then ( function ( response ) {
45
+
46
+ vm . loading = false ;
47
+
48
+ if ( response . error !== undefined && response . error !== null ) {
49
+
50
+ notificationsService . warning ( response . error . status , response . error . message ) ;
51
+
52
+ // if token expired -> refresh access token
53
+ if ( response . error . code === "401" ) {
54
+ vm . isConnected = false ;
55
+
56
+ // refresh access token
57
+ refreshAccessToken ( ) ;
58
+ }
59
+ } else {
60
+ vm . showResults = true ;
61
+ vm . inspectionResult = response . inspectionResult ;
62
+ }
63
+ } ) ;
64
+ }
65
+
66
+ vm . onEdit = function ( ) {
67
+ vm . inspectionObj . multipleUrls = false ;
68
+ vm . inspectionObj . enabled = true ;
69
+ }
70
+
71
+ vm . onChangeInspectionUrl = function ( ) {
72
+ vm . inspectionObj . languageCode =
73
+ editorState . current . urls . find ( p => p . text === vm . inspectionObj . inspectionUrl ) . culture ;
74
+ }
75
+
76
+ // authorization listener
77
+ window . addEventListener ( "message" , function ( event ) {
78
+ if ( event . data . type === "google:oauth:success" ) {
79
+
80
+ var codeParam = "?code=" ;
81
+ var scopeParam = "&scope=" ;
82
+
83
+ vm . authorizationWindow . close ( ) ;
84
+
85
+ var code = event . data . url . slice ( event . data . url . indexOf ( codeParam ) + codeParam . length , event . data . url . indexOf ( scopeParam ) ) ;
86
+
87
+ umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource . getAccessToken ( code ) . then ( function ( response ) {
88
+ if ( response !== "error" ) {
89
+ vm . oauthConfiguration . isConnected = true ;
90
+ notificationsService . success ( "Google Search Console Authorization" , "Access Approved" ) ;
91
+ } else {
92
+ notificationsService . error ( "Google Search Console Authorization" , "Access Denied" ) ;
93
+ }
94
+ } ) ;
95
+ } else if ( event . data . type === "google:oauth:denied" ) {
96
+ notificationsService . error ( "Google Search Console Authorization" , "Access Denied" ) ;
97
+ vm . oauthConfiguration . isConnected = false ;
98
+ vm . authorizationWindow . close ( ) ;
99
+ }
100
+
101
+ } , false ) ;
102
+
103
+ function refreshAccessToken ( ) {
104
+ notificationsService . warning ( "Google Search Console Authorization" , "Refreshing access token." ) ;
105
+
106
+ umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource . refreshAccessToken ( ) . then (
107
+ function ( response ) {
108
+ if ( response . length !== "error" ) {
109
+
110
+ notificationsService . success ( "Google Search Console Authorization" ,
111
+ "Refresh access token - completed." ) ;
112
+
113
+ vm . isConnected = true ;
114
+ } else
115
+ notificationsService . error ( "Google Search Console Authorization" ,
116
+ "An error has occurred." ) ;
117
+ } ) ;
118
+ }
119
+
120
+ function revokeToken ( ) {
121
+ umbracoCmsIntegrationsGoogleSearchConsoleUrlInspectionToolResource . revokeToken ( ) . then ( function ( ) {
122
+ vm . oauthConfiguration . isConnected = false ;
123
+ vm . showResults = false ;
124
+ } ) ;
125
+ }
126
+
127
+ function isRelativeUrl ( url ) {
128
+ var regExp = new RegExp ( '^(?:[a-z]+:)?//' , 'i' ) ;
129
+ return ! regExp . test ( url ) ;
130
+ }
131
+
132
+ function getUrls ( ) {
133
+ var arr = [ ] ;
134
+
135
+ for ( var i = 0 ; i < editorState . current . urls . length ; i ++ ) {
136
+ var url = isRelativeUrl ( editorState . current . urls [ i ] . text )
137
+ ? `${ window . location . origin } ${ editorState . current . urls [ i ] . text } `
138
+ : editorState . current . urls [ i ] . text ;
139
+
140
+ if ( arr . indexOf ( url ) === - 1 ) {
141
+ arr . push ( url ) ;
142
+ }
143
+ }
144
+
145
+ return arr ;
146
+ }
147
+ }
148
+
149
+ angular . module ( "umbraco" )
150
+ . controller ( "UmbracoCms.Integrations.GoogleSearchConsole.UrlInspectionToolController" , urlInspectionToolController )
0 commit comments