@@ -19,6 +19,7 @@ class AccessibilityCheckerHighlight {
1919 } ;
2020
2121 this . settings = { ...defaultSettings , ...settings } ;
22+ this . _scanAttempted = false ;
2223
2324 this . highlightPanel = this . addHighlightPanel ( ) ;
2425 this . nextButton = document . querySelector ( '#edac-highlight-next' ) ;
@@ -188,15 +189,21 @@ class AccessibilityCheckerHighlight {
188189 } ,
189190 ) ;
190191 }
192+ } else if ( ! self . _scanAttempted && response . data ?. [ 0 ] ?. code === - 3 ) {
193+ // Only try kickoffScan once per highlightAjax call
194+ self . _scanAttempted = true ;
195+ self . kickoffScan ( ) ;
196+ // After kickoffScan, try highlightAjax again, but only once
197+ setTimeout ( ( ) => {
198+ self . highlightAjax ( ) . then ( resolve ) . catch ( reject ) ;
199+ } , 5000 ) ; // Wait 5s for scan to complete.
191200 } else {
192- resolve ( [ ] ) ;
193- //console.log(response );
201+ // Default: resolve with empty issues/fixes
202+ resolve ( { issues : [ ] , fixes : [ ] } ) ;
194203 }
195204 } else {
196205 self . showWait ( false ) ;
197206
198- //console.log( 'Request failed. Returned status of ' + xhr.status );
199-
200207 reject ( {
201208 status : xhr . status ,
202209 statusText : xhr . statusText ,
@@ -541,7 +548,11 @@ class AccessibilityCheckerHighlight {
541548 }
542549 }
543550 ) . catch ( ( err ) => {
544- //TODO:
551+ // Output a message that says that there are no issues or that the issues could not be loaded.
552+ const summary = document . querySelector ( '.edac-highlight-panel-controls-summary' ) ;
553+ if ( summary ) {
554+ summary . textContent = __ ( 'An error occurred when loading the issues.' , 'accessibility-checker' ) ;
555+ }
545556 } ) ;
546557 }
547558
@@ -1088,6 +1099,115 @@ class AccessibilityCheckerHighlight {
10881099 element . removeAttribute ( 'data-edac-landmark-label-id' ) ;
10891100 } ) ;
10901101 }
1102+
1103+ /**
1104+ * Kick off the accessibility scan.
1105+ */
1106+ kickoffScan ( ) {
1107+ const getPageDensity = ( ) => {
1108+ const elementCount = document . body . getElementsByTagName ( '*' ) . length ;
1109+ const contentLength = document . body . innerText . length ;
1110+ return { elementCount, contentLength } ;
1111+ } ;
1112+ const densityMetrics = getPageDensity ( ) ;
1113+ const self = this ;
1114+ const scriptId = 'edac-accessibility-checker-scanner-script' ;
1115+ if ( ! document . getElementById ( scriptId ) ) {
1116+ const script = document . createElement ( 'script' ) ;
1117+ script . src = window . edacFrontendHighlighterApp ?. scannerBundleUrl || '/wp-content/plugins/accessibility-checker/build/pageScanner.bundle.js' ;
1118+ script . id = scriptId ;
1119+ script . onload = function ( ) {
1120+ setTimeout ( ( ) => {
1121+ self . _runScanOrShowError ( densityMetrics ) ;
1122+ } , 100 ) ;
1123+ } ;
1124+ script . onerror = function ( ) {
1125+ self . showWait ( false ) ;
1126+ self . showScanError ( 'Failed to load scanner script.' ) ;
1127+ } ;
1128+ document . head . appendChild ( script ) ;
1129+ } else {
1130+ self . _runScanOrShowError ( densityMetrics ) ;
1131+ }
1132+ }
1133+
1134+ _runScanOrShowError ( densityMetrics ) {
1135+ if ( window . runAccessibilityScan ) {
1136+ this . runAccessibilityScanAndSave ( densityMetrics ) ;
1137+ } else {
1138+ this . showWait ( false ) ;
1139+ this . showScanError ( __ ( 'Scanner function not found.' , 'accessibility-checker' ) ) ;
1140+ }
1141+ }
1142+
1143+ runAccessibilityScanAndSave ( densityMetrics ) {
1144+ const self = this ;
1145+ const summary = document . querySelector ( '.edac-highlight-panel-controls-summary' ) ;
1146+ if ( summary ) {
1147+ summary . textContent = __ ( 'Scanning...' , 'accessibility-checker' ) ;
1148+ summary . classList . remove ( 'edac-error' ) ;
1149+ }
1150+ window . runAccessibilityScan ( ) . then ( ( result ) => {
1151+ const postId = window . edacFrontendHighlighterApp && window . edacFrontendHighlighterApp . postID ;
1152+ const nonce = window . edacFrontendHighlighterApp && window . edacFrontendHighlighterApp . restNonce ;
1153+ if ( ! postId || ! nonce ) {
1154+ self . showWait ( false ) ;
1155+ self . showScanError ( __ ( 'Missing postId or nonce.' , 'accessibility-checker' ) ) ;
1156+ return ;
1157+ }
1158+ if ( ! result || ! result . violations || result . violations . length === 0 ) {
1159+ self . showWait ( false ) ;
1160+ self . showScanError ( __ ( 'No violations found, skipping save.' , 'accessibility-checker' ) ) ;
1161+ return ;
1162+ }
1163+ self . saveScanResults ( postId , nonce , result . violations , densityMetrics ) ;
1164+ } ) . catch ( ( ) => {
1165+ self . showWait ( false ) ;
1166+ self . showScanError ( __ ( 'Accessibility scan error.' , 'accessibility-checker' ) ) ;
1167+ } ) ;
1168+ }
1169+
1170+ saveScanResults ( postId , nonce , violations , densityMetrics ) {
1171+ const self = this ;
1172+ fetch ( '/wp-json/accessibility-checker/v1/post-scan-results/' + postId , {
1173+ method : 'POST' ,
1174+ headers : {
1175+ 'Content-Type' : 'application/json' ,
1176+ 'X-WP-Nonce' : nonce ,
1177+ } ,
1178+ body : JSON . stringify ( {
1179+ violations,
1180+ isSkipped : false ,
1181+ isFailure : false ,
1182+ densityMetrics,
1183+ } ) ,
1184+ } )
1185+ . then ( ( response ) => response . json ( ) )
1186+ . then ( ( data ) => {
1187+ self . showWait ( false ) ;
1188+ if ( data && data . success ) {
1189+ // Optionally show a success message or update UI
1190+ } else {
1191+ self . showScanError ( __ ( 'Saving failed.' , 'accessibility-checker' ) ) ;
1192+ }
1193+ } )
1194+ . catch ( ( ) => {
1195+ self . showWait ( false ) ;
1196+ self . showScanError ( __ ( 'Error saving scan results.' , 'accessibility-checker' ) ) ;
1197+ } ) ;
1198+ }
1199+
1200+ /**
1201+ * Show an error message in the scan panel or as an alert fallback.
1202+ * @param {string } message
1203+ */
1204+ showScanError ( message ) {
1205+ const summary = document . querySelector ( '.edac-highlight-panel-controls-summary' ) ;
1206+ if ( summary ) {
1207+ summary . textContent = message ;
1208+ summary . classList . add ( 'edac-error' ) ;
1209+ }
1210+ }
10911211}
10921212
10931213// Some systems (Cloudflare Rocket Loader) defers scripts for performance but that can
0 commit comments