@countriesdb/widget 0.1.13 → 0.1.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1007,6 +1007,19 @@
1007
1007
  */
1008
1008
  // Global namespace to prevent double initialization
1009
1009
  const NS_KEY = '__CountriesWidgetNS__';
1010
+ // Capture script URL immediately when module loads (while document.currentScript is available)
1011
+ let capturedScriptUrl = null;
1012
+ try {
1013
+ if (typeof document !== 'undefined' && document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1014
+ const currentScript = document.currentScript;
1015
+ if (currentScript.src) {
1016
+ capturedScriptUrl = new URL(currentScript.src);
1017
+ }
1018
+ }
1019
+ }
1020
+ catch {
1021
+ // Ignore errors
1022
+ }
1010
1023
  /**
1011
1024
  * Main widget initialization function
1012
1025
  */
@@ -1098,23 +1111,30 @@
1098
1111
  // Try to get config from script URL (for backward compatibility with widget.blade.php)
1099
1112
  let scriptUrl = null;
1100
1113
  try {
1101
- let loaderScript = null;
1102
- // First try document.currentScript (works during script execution)
1103
- if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1104
- loaderScript = document.currentScript;
1114
+ // First, use the captured script URL (captured at module load time)
1115
+ if (capturedScriptUrl) {
1116
+ scriptUrl = capturedScriptUrl;
1105
1117
  }
1106
1118
  else {
1107
- // Fallback: find script tag with @countriesdb/widget in src
1108
- const scripts = Array.from(document.getElementsByTagName('script'));
1109
- loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1110
- s.src.includes('widget/dist/index.js'))) || null;
1111
- // If still not found, try the last script with src
1112
- if (!loaderScript) {
1113
- loaderScript = scripts.filter((s) => s.src).pop() || null;
1119
+ // Fallback: try to find script tag
1120
+ let loaderScript = null;
1121
+ // Try document.currentScript (works during script execution)
1122
+ if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1123
+ loaderScript = document.currentScript;
1124
+ }
1125
+ else {
1126
+ // Fallback: find script tag with @countriesdb/widget in src
1127
+ const scripts = Array.from(document.getElementsByTagName('script'));
1128
+ loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1129
+ s.src.includes('widget/dist/index.js'))) || null;
1130
+ // If still not found, try the last script with src
1131
+ if (!loaderScript) {
1132
+ loaderScript = scripts.filter((s) => s.src).pop() || null;
1133
+ }
1134
+ }
1135
+ if (loaderScript && loaderScript.src) {
1136
+ scriptUrl = new URL(loaderScript.src);
1114
1137
  }
1115
- }
1116
- if (loaderScript && loaderScript.src) {
1117
- scriptUrl = new URL(loaderScript.src);
1118
1138
  }
1119
1139
  }
1120
1140
  catch {
@@ -1176,23 +1196,45 @@
1176
1196
  */
1177
1197
  function getBackendUrlFromScript() {
1178
1198
  try {
1179
- let loaderScript = null;
1180
- // First try document.currentScript (works during script execution)
1181
- if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1182
- loaderScript = document.currentScript;
1183
- }
1184
- else {
1185
- // Fallback: find script tag with @countriesdb/widget in src
1186
- const scripts = Array.from(document.getElementsByTagName('script'));
1187
- loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1188
- s.src.includes('widget/dist/index.js'))) || null;
1189
- // If still not found, try the last script with src
1190
- if (!loaderScript) {
1191
- loaderScript = scripts.filter((s) => s.src).pop() || null;
1199
+ // Use captured script URL first (captured at module load time)
1200
+ let scriptUrl = capturedScriptUrl;
1201
+ // If not captured, try to find script tag
1202
+ if (!scriptUrl) {
1203
+ let loaderScript = null;
1204
+ // First try document.currentScript (works during script execution)
1205
+ if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1206
+ loaderScript = document.currentScript;
1207
+ }
1208
+ else {
1209
+ // Fallback: find script tag with @countriesdb/widget in src
1210
+ const scripts = Array.from(document.getElementsByTagName('script'));
1211
+ loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1212
+ s.src.includes('widget/dist/index.js'))) || null;
1213
+ // If still not found, try the last script with src
1214
+ if (!loaderScript) {
1215
+ loaderScript = scripts.filter((s) => s.src).pop() || null;
1216
+ }
1217
+ }
1218
+ if (loaderScript && loaderScript.src) {
1219
+ scriptUrl = new URL(loaderScript.src);
1192
1220
  }
1193
1221
  }
1194
- if (loaderScript && loaderScript.src) {
1195
- const scriptUrl = new URL(loaderScript.src);
1222
+ if (scriptUrl) {
1223
+ const hostname = scriptUrl.hostname;
1224
+ // List of known CDN hostnames - if script is from a CDN, use default API
1225
+ const cdnHostnames = [
1226
+ 'unpkg.com',
1227
+ 'cdn.jsdelivr.net',
1228
+ 'cdnjs.cloudflare.com',
1229
+ 'jsdelivr.com',
1230
+ 'cdn.jsdelivr.com',
1231
+ ];
1232
+ // If script is from a CDN, don't use its origin as backend URL
1233
+ if (cdnHostnames.some(cdn => hostname.includes(cdn))) {
1234
+ return 'https://api.countriesdb.com';
1235
+ }
1236
+ // Only use script origin if it's from the same domain (self-hosted widget)
1237
+ // This allows self-hosting the widget on the same domain as the API
1196
1238
  const scheme = scriptUrl.protocol;
1197
1239
  const host = scriptUrl.hostname;
1198
1240
  const port = scriptUrl.port ? `:${scriptUrl.port}` : '';
@@ -1254,23 +1296,34 @@
1254
1296
  document.addEventListener('DOMContentLoaded', checkAutoInit, { once: true });
1255
1297
  return;
1256
1298
  }
1257
- // Find the script tag that loaded this widget
1258
- let loaderScript = null;
1259
- // First try document.currentScript (works during script execution)
1260
- if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1261
- loaderScript = document.currentScript;
1262
- }
1263
- else {
1264
- // Fallback: find script tag with @countriesdb/widget in src
1265
- const scripts = Array.from(document.getElementsByTagName('script'));
1266
- loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1267
- s.src.includes('widget/dist/index.js'))) || null;
1268
- }
1269
1299
  // Default to auto-init = true (only disable if explicitly set to false)
1270
1300
  let shouldAutoInit = true;
1271
- if (loaderScript && loaderScript.src) {
1301
+ // Use captured script URL first (captured at module load time)
1302
+ let scriptUrl = capturedScriptUrl;
1303
+ // If not captured, try to find script tag
1304
+ if (!scriptUrl) {
1305
+ let loaderScript = null;
1306
+ // Try document.currentScript (works during script execution)
1307
+ if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1308
+ loaderScript = document.currentScript;
1309
+ }
1310
+ else {
1311
+ // Fallback: find script tag with @countriesdb/widget in src
1312
+ const scripts = Array.from(document.getElementsByTagName('script'));
1313
+ loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1314
+ s.src.includes('widget/dist/index.js'))) || null;
1315
+ }
1316
+ if (loaderScript && loaderScript.src) {
1317
+ try {
1318
+ scriptUrl = new URL(loaderScript.src);
1319
+ }
1320
+ catch {
1321
+ // Ignore errors
1322
+ }
1323
+ }
1324
+ }
1325
+ if (scriptUrl) {
1272
1326
  try {
1273
- const scriptUrl = new URL(loaderScript.src);
1274
1327
  const autoInit = scriptUrl.searchParams.get('auto_init');
1275
1328
  // Only disable if explicitly set to false/0
1276
1329
  shouldAutoInit = autoInit === null || autoInit === 'true' || autoInit === '1';