ask-widget.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * Loads "davo-bot 2000" (the floating AI launcher) site-wide.
  3. *
  4. * The widget bundle (/ask/widget.js) and its API (/ask/api/ask) are served by
  5. * the ralph RAG server and proxied SAME-ORIGIN under /ask/ by Apache — in
  6. * production on davidawindham.com and locally on daw.stu (see the vhost
  7. * ProxyPass directives). Same-origin means no CORS to worry about.
  8. *
  9. * We probe the widget URL first and only inject it if the response is really
  10. * JavaScript: when ralph is down, the request falls through to WordPress and
  11. * returns HTML, and injecting that would throw. The probe makes it a silent
  12. * no-op until the backend is actually serving the widget. The widget sets
  13. * window.__dawaskLoaded to guard against a double instance.
  14. */
  15. ( function () {
  16. if ( typeof document === 'undefined' || typeof fetch === 'undefined' ) {
  17. return;
  18. }
  19. var WIDGET_URL = '/ask/widget.js';
  20. var API_URL = '/ask/api/ask';
  21. function inject() {
  22. if ( document.getElementById( 'dawask-script' ) ) {
  23. return; // already injected
  24. }
  25. var s = document.createElement( 'script' );
  26. s.id = 'dawask-script';
  27. s.src = WIDGET_URL;
  28. s.setAttribute( 'data-api-url', API_URL );
  29. s.setAttribute( 'data-mode', 'launcher' );
  30. document.body.appendChild( s );
  31. }
  32. fetch( WIDGET_URL, { method: 'GET', cache: 'no-store' } )
  33. .then( function ( r ) {
  34. var ct = ( r.headers.get( 'content-type' ) || '' ).toLowerCase();
  35. if ( r.ok && ct.indexOf( 'javascript' ) !== -1 ) {
  36. inject();
  37. }
  38. } )
  39. .catch( function () {} ); // backend unreachable → no widget, no error
  40. } )();