|
72 | 72 | (function(window, document) { |
73 | 73 | 'use strict'; |
74 | 74 |
|
| 75 | + // Detect the script URL to determine CDN base for CSS loading |
| 76 | + // This allows the CSS to be loaded from the same location as the widget.js |
| 77 | + const SCRIPT_URL = (function() { |
| 78 | + try { |
| 79 | + // Try document.currentScript first (works in most cases) |
| 80 | + if (document.currentScript && document.currentScript.src) { |
| 81 | + return document.currentScript.src; |
| 82 | + } |
| 83 | + // Fallback: find our script by looking for widget.js in script tags |
| 84 | + const scripts = document.getElementsByTagName('script'); |
| 85 | + for (let i = scripts.length - 1; i >= 0; i--) { |
| 86 | + const src = scripts[i].src || ''; |
| 87 | + if (src.includes('widget') && src.includes('.js')) { |
| 88 | + return src; |
| 89 | + } |
| 90 | + } |
| 91 | + } catch (e) { |
| 92 | + // Ignore errors |
| 93 | + } |
| 94 | + return null; |
| 95 | + })(); |
| 96 | + |
| 97 | + // Extract CDN base URL from script source |
| 98 | + const CDN_BASE = (function() { |
| 99 | + if (SCRIPT_URL) { |
| 100 | + try { |
| 101 | + const url = new URL(SCRIPT_URL); |
| 102 | + return `${url.protocol}//${url.host}`; |
| 103 | + } catch (e) { |
| 104 | + // Ignore URL parse errors |
| 105 | + } |
| 106 | + } |
| 107 | + return 'https://cdn.ezcontactform.com'; // Default CDN |
| 108 | + })(); |
| 109 | + |
75 | 110 | // Default configuration |
76 | 111 | const DEFAULT_CONFIG = { |
77 | 112 | apiBase: 'https://api.ezcontactform.com', |
| 113 | + cdnBase: CDN_BASE, // Where to load CSS from (detected from script URL) |
78 | 114 | theme: 'system', // 'system' will auto-detect light/dark based on user preference |
79 | 115 | autoInit: true, |
80 | 116 | disableStyles: false, |
|
1553 | 1589 | const link = document.createElement('link'); |
1554 | 1590 | link.id = 'ezform-styles'; |
1555 | 1591 | link.rel = 'stylesheet'; |
1556 | | - link.href = `${this.config.apiBase}/widget.css`; |
| 1592 | + // Load CSS from CDN (same location as widget.js), not from API |
| 1593 | + link.href = `${this.config.cdnBase}/widget.css`; |
1557 | 1594 | document.head.appendChild(link); |
1558 | 1595 | }, |
1559 | 1596 |
|
|
0 commit comments