@countriesdb/widget 0.1.15 → 0.1.16

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,19 +1007,6 @@
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
- }
1023
1010
  /**
1024
1011
  * Main widget initialization function
1025
1012
  */
@@ -1111,31 +1098,24 @@
1111
1098
  // Try to get config from script URL (for backward compatibility with widget.blade.php)
1112
1099
  let scriptUrl = null;
1113
1100
  try {
1114
- // First, use the captured script URL (captured at module load time)
1115
- if (capturedScriptUrl) {
1116
- scriptUrl = capturedScriptUrl;
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;
1117
1105
  }
1118
1106
  else {
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);
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;
1137
1114
  }
1138
1115
  }
1116
+ if (loaderScript && loaderScript.src) {
1117
+ scriptUrl = new URL(loaderScript.src);
1118
+ }
1139
1119
  }
1140
1120
  catch {
1141
1121
  // Ignore errors
@@ -1196,30 +1176,23 @@
1196
1176
  */
1197
1177
  function getBackendUrlFromScript() {
1198
1178
  try {
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);
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;
1220
1192
  }
1221
1193
  }
1222
- if (scriptUrl) {
1194
+ if (loaderScript && loaderScript.src) {
1195
+ const scriptUrl = new URL(loaderScript.src);
1223
1196
  const hostname = scriptUrl.hostname;
1224
1197
  // List of known CDN hostnames - if script is from a CDN, use default API
1225
1198
  const cdnHostnames = [
@@ -1296,34 +1269,23 @@
1296
1269
  document.addEventListener('DOMContentLoaded', checkAutoInit, { once: true });
1297
1270
  return;
1298
1271
  }
1272
+ // Find the script tag that loaded this widget
1273
+ let loaderScript = null;
1274
+ // First try document.currentScript (works during script execution)
1275
+ if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1276
+ loaderScript = document.currentScript;
1277
+ }
1278
+ else {
1279
+ // Fallback: find script tag with @countriesdb/widget in src
1280
+ const scripts = Array.from(document.getElementsByTagName('script'));
1281
+ loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1282
+ s.src.includes('widget/dist/index.js'))) || null;
1283
+ }
1299
1284
  // Default to auto-init = true (only disable if explicitly set to false)
1300
1285
  let shouldAutoInit = true;
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) {
1286
+ if (loaderScript && loaderScript.src) {
1326
1287
  try {
1288
+ const scriptUrl = new URL(loaderScript.src);
1327
1289
  const autoInit = scriptUrl.searchParams.get('auto_init');
1328
1290
  // Only disable if explicitly set to false/0
1329
1291
  shouldAutoInit = autoInit === null || autoInit === 'true' || autoInit === '1';