1/* 2 * Accessibility Lite â frontend script (~3 KB, vanilla JS, no jQuery) 3 */ 4(function () { 5 'use strict'; 6 7 function init() { 8 var KEY = 'al-a11y'; 9 var MIN = 100; 10 var MAX = 200; 11 var STEP = 10; 12 13 var opts = window.AlA11yOpts || {}; 14 15 var skipLink = document.querySelector('.al-skip-link'); 16 if (skipLink) { 17 var targetId = (skipLink.getAttribute('href') || '').replace('#', ''); 18 if (targetId && !document.getElementById(targetId)) { 19 var fallback = document.querySelector('main, [role="main"], #main, #primary'); 20 if (fallback) { 21 if (!fallback.id) { 22 fallback.id = 'al-content-fallback'; 23 } 24 skipLink.setAttribute('href', '#' + fallback.id); 25 } 26 } 27 // On themes without wp_body_open, PHP falls back to printing at wp_footer â 28 // move it back to the first tab stop so accesskey="s" still skips past the whole page. 29 if (document.body.firstChild !== skipLink) { 30 document.body.insertBefore(skipLink, document.body.firstChild); 31 } 32 } 33 34 var toolbar = document.getElementById('al-toolbar'); 35 if (!toolbar) { 36 return; 37 } 38 39 var body = document.body; 40 var toggleBtn = toolbar.querySelector('.al-toggle button'); 41 var toggleSr = toggleBtn.querySelector('.al-sr'); 42 var active = {}; 43 var scale = MIN; 44 var schema = null; 45 46 function btn(action) { 47 return toolbar.querySelector('.al-items [data-action="' + action + '"]'); 48 } 49 50 function setBtn(action, on) { 51 var b = btn(action); 52 if (b) { 53 b.classList.toggle('active', !!on); 54 } 55 if (on) { 56 active[action] = true; 57 } else { 58 delete active[action]; 59 } 60 } 61 62 function setClass(action, on) { 63 body.classList.toggle('al-' + action, !!on); 64 } 65 66 function applyScale() { 67 var activeScale = scale > MIN; 68 body.classList.toggle('al-resize', activeScale); 69 body.style.setProperty('--al-scale', (scale / 100).toFixed(2)); 70 setBtn('resize-plus', activeScale); 71 } 72 73 function setSchema(action) { 74 if (schema) { 75 setClass(schema, false); 76 setBtn(schema, false); 77 } 78 schema = action || null; 79 if (schema) { 80 setClass(schema, true); 81 setBtn(schema, true); 82 } 83 } 84 85 function toggle(action) { 86 var on = !active[action]; 87 setClass(action, on); 88 setBtn(action, on); 89 } 90 91 function resize(dir) { 92 var next = scale + dir * STEP; 93 if (next < MIN || next > MAX) { 94 return; 95 } 96 scale = next; 97 applyScale(); 98 save(); 99 } 100 101 function reset() { 102 for (var a in active) { 103 if (Object.prototype.hasOwnProperty.call(active, a)) { 104 setClass(a, false); 105 setBtn(a, false); 106 } 107 } 108 active = {}; 109 schema = null; 110 scale = MIN; 111 body.classList.remove('al-resize'); 112 body.style.removeProperty('--al-scale'); 113 clearSave(); 114 } 115 116 function save() { 117 if (opts.save !== '1') { 118 return; 119 } 120 var hours = parseInt(opts.saveExp, 10) || 720; 121 try { 122 localStorage.setItem(KEY, JSON.stringify({ 123 actions: active, 124 scale: scale, 125 schema: schema, 126 expires: Date.now() + hours * 36e5 127 })); 128 } catch (e) { /* storage unavailable */ } 129 } 130 131 function clearSave() { 132 try { 133 localStorage.removeItem(KEY); 134 } catch (e) { /* ignore */ } 135 } 136 137 function restore() { 138 if (opts.save !== '1') { 139 return; 140 } 141 var raw; 142 try { 143 raw = localStorage.getItem(KEY); 144 } catch (e) { 145 return; 146 } 147 if (!raw) { 148 return; 149 } 150 var d; 151 try { 152 d = JSON.parse(raw); 153 } catch (e) { 154 return; 155 } 156 if (!d || !d.expires || d.expires < Date.now()) { 157 clearSave(); 158 return; 159 } 160 var a = d.actions || {}; 161 scale = Math.min(MAX, Math.max(MIN, d.scale || MIN)); 162 applyScale(); 163 if (d.schema && a[d.schema]) { 164 setSchema(d.schema); 165 } 166 ['links-underline', 'readable-font'].forEach(function (k) { 167 if (a[k]) { 168 toggle(k); 169 } 170 }); 171 } 172 173 function openToolbar() { 174 toolbar.classList.add('al-open'); 175 toggleBtn.setAttribute('aria-expanded', 'true'); 176 if (toggleSr && opts.closeLabel) { 177 toggleSr.textContent = opts.closeLabel; 178 } 179 } 180 181 function closeToolbar() { 182 toolbar.classList.remove('al-open'); 183 toggleBtn.setAttribute('aria-expanded', 'false'); 184 if (toggleSr && opts.openLabel) { 185 toggleSr.textContent = opts.openLabel; 186 } 187 } 188 189 var SCHEMA_ACTIONS = ['grayscale', 'high-contrast', 'negative-contrast', 'light-bg']; 190 191 toolbar.addEventListener('click', function (e) { 192 var b = e.target.closest('[data-action]'); 193 if (!b) { 194 return; 195 } 196 var action = b.getAttribute('data-action'); 197
198 if (action === 'reset') { 199 reset(); 200 return; 201 } 202 if (action === 'resize-plus') { 203 resize(1); 204 return; 205 } 206 if (action === 'resize-minus') { 207 resize(-1); 208 return; 209 } 210 if (SCHEMA_ACTIONS.indexOf(action) !== -1) { 211 setSchema(active[action] ? null : action); 212 save(); 213 return; 214 } 215 toggle(action); 216 save(); 217 }); 218 219 toggleBtn.addEventListener('click', function () { 220 if (toolbar.classList.contains('al-open')) { 221 closeToolbar(); 222 } else { 223 openToolbar(); 224 } 225 }); 226 227 /* Tab onto the toggle button opens the toolbar (same as the original) */ 228 toggleBtn.addEventListener('keydown', function (e) { 229 if (e.key === 'Tab') { 230 openToolbar(); 231 } 232 }); 233 234 /* Escape closes the toolbar */ 235 document.addEventListener('keydown', function (e) { 236 if (e.key === 'Escape' && toolbar.classList.contains('al-open')) { 237 closeToolbar(); 238 toggleBtn.focus(); 239 } 240 }); 241 242 restore(); 243 } 244 245 // If PHP fell back to printing the toolbar at wp_footer (see a11y-lite.php), 246 // this script can run before that markup exists in the DOM. Wait for the 247 // parser to finish instead of silently no-op'ing on a null #al-toolbar. 248 if (document.readyState === 'loading') { 249 document.addEventListener('DOMContentLoaded', init); 250 } else { 251 init(); 252 } 253})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.