@countriesdb/widget 0.1.15 → 0.1.17

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,30 +1098,19 @@
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);
1137
- }
1107
+ // Only consider script tags that loaded the widget bundle
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
+ }
1112
+ if (loaderScript && loaderScript.src) {
1113
+ scriptUrl = new URL(loaderScript.src);
1138
1114
  }
1139
1115
  }
1140
1116
  catch {
@@ -1196,30 +1172,19 @@
1196
1172
  */
1197
1173
  function getBackendUrlFromScript() {
1198
1174
  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);
1220
- }
1175
+ let loaderScript = null;
1176
+ // First try document.currentScript (works during script execution)
1177
+ if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1178
+ loaderScript = document.currentScript;
1221
1179
  }
1222
- if (scriptUrl) {
1180
+ else {
1181
+ // Only consider script tags that loaded the widget bundle
1182
+ const scripts = Array.from(document.getElementsByTagName('script'));
1183
+ loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1184
+ s.src.includes('widget/dist/index.js'))) || null;
1185
+ }
1186
+ if (loaderScript && loaderScript.src) {
1187
+ const scriptUrl = new URL(loaderScript.src);
1223
1188
  const hostname = scriptUrl.hostname;
1224
1189
  // List of known CDN hostnames - if script is from a CDN, use default API
1225
1190
  const cdnHostnames = [
@@ -1296,34 +1261,23 @@
1296
1261
  document.addEventListener('DOMContentLoaded', checkAutoInit, { once: true });
1297
1262
  return;
1298
1263
  }
1264
+ // Find the script tag that loaded this widget
1265
+ let loaderScript = null;
1266
+ // First try document.currentScript (works during script execution)
1267
+ if (document.currentScript && document.currentScript instanceof HTMLScriptElement) {
1268
+ loaderScript = document.currentScript;
1269
+ }
1270
+ else {
1271
+ // Fallback: find script tag with @countriesdb/widget in src
1272
+ const scripts = Array.from(document.getElementsByTagName('script'));
1273
+ loaderScript = scripts.find((s) => s.src && (s.src.includes('@countriesdb/widget') ||
1274
+ s.src.includes('widget/dist/index.js'))) || null;
1275
+ }
1299
1276
  // Default to auto-init = true (only disable if explicitly set to false)
1300
1277
  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) {
1278
+ if (loaderScript && loaderScript.src) {
1326
1279
  try {
1280
+ const scriptUrl = new URL(loaderScript.src);
1327
1281
  const autoInit = scriptUrl.searchParams.get('auto_init');
1328
1282
  // Only disable if explicitly set to false/0
1329
1283
  shouldAutoInit = autoInit === null || autoInit === 'true' || autoInit === '1';