ask-widget.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. // data-open: set per-page by the theme (window.dwAsk) — 'once' on the homepage.
  31. var open = ( window.dwAsk && window.dwAsk.open ) || '';
  32. if ( open ) {
  33. s.setAttribute( 'data-open', open );
  34. }
  35. document.body.appendChild( s );
  36. }
  37. fetch( WIDGET_URL, { method: 'GET', cache: 'no-store' } )
  38. .then( function ( r ) {
  39. var ct = ( r.headers.get( 'content-type' ) || '' ).toLowerCase();
  40. if ( r.ok && ct.indexOf( 'javascript' ) !== -1 ) {
  41. inject();
  42. }
  43. } )
  44. .catch( function () {} ); // backend unreachable → no widget, no error
  45. } )();