@quicktvui/web-cli 2.2.0 → 2.3.0

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.
@@ -292,6 +292,87 @@
292
292
  // 页面加载后的冷却期:这段时间内忽略 bundle-update 事件
293
293
  // 防止 SSE 重连后收到缓存的旧 bundle-update 导致无限刷新
294
294
  var sseCooldownUntil = Date.now() + 3000; // 3 秒冷却期
295
+
296
+ // ========== CORS 代理 ==========
297
+ // 在 web-cli dev 模式下,拦截所有跨域 fetch/XHR/Image 请求,
298
+ // 转换为 /proxy/ 路径由 DevServer 代理转发,解决浏览器 CORS 限制
299
+ // TV 原生环境无 CORS 限制,但浏览器中所有外部请求都会被拦截
300
+ (function initDevProxy() {
301
+ var LOCAL_ORIGIN = window.location.origin;
302
+
303
+ /**
304
+ * 判断 URL 是否为跨域请求(需要代理)
305
+ */
306
+ function isCrossOrigin(url) {
307
+ if (!url) return false;
308
+ try {
309
+ var urlObj = new URL(url, window.location.href);
310
+ return urlObj.origin !== LOCAL_ORIGIN;
311
+ } catch (e) {
312
+ return false;
313
+ }
314
+ }
315
+
316
+ /**
317
+ * 将跨域 URL 转换为代理路径
318
+ * https://api.example.com/v1/data?a=1 → /proxy/https/api.example.com/v1/data?a=1
319
+ */
320
+ function toProxyUrl(url) {
321
+ try {
322
+ var urlObj = new URL(url, window.location.href);
323
+ var proxyPath = '/proxy/' + urlObj.protocol.replace(':', '') + '/' + urlObj.host + urlObj.pathname + urlObj.search + urlObj.hash;
324
+ return proxyPath;
325
+ } catch (e) {
326
+ return url;
327
+ }
328
+ }
329
+
330
+ // ===== 拦截 fetch =====
331
+ var originalFetch = window.fetch;
332
+ window.fetch = function(input, init) {
333
+ var url = (typeof input === 'string') ? input : (input && input.url ? input.url : input);
334
+ if (isCrossOrigin(url)) {
335
+ var proxyUrl = toProxyUrl(url);
336
+ console.log('[DevProxy] fetch:', url, '->', proxyUrl);
337
+ if (typeof input === 'string') {
338
+ input = proxyUrl;
339
+ } else if (input instanceof Request) {
340
+ input = new Request(proxyUrl, init || {});
341
+ }
342
+ }
343
+ return originalFetch.call(this, input, init);
344
+ };
345
+
346
+ // ===== 拦截 XMLHttpRequest =====
347
+ var originalXHROpen = XMLHttpRequest.prototype.open;
348
+ XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
349
+ if (isCrossOrigin(url)) {
350
+ var proxyUrl = toProxyUrl(url);
351
+ console.log('[DevProxy] XHR:', url, '->', proxyUrl);
352
+ url = proxyUrl;
353
+ }
354
+ return originalXHROpen.call(this, method, url, async !== false, user, password);
355
+ };
356
+
357
+ // ===== 拦截 Image src =====
358
+ var originalImageSrcDescriptor = Object.getOwnPropertyDescriptor(HTMLImageElement.prototype, 'src');
359
+ if (originalImageSrcDescriptor && originalImageSrcDescriptor.set) {
360
+ Object.defineProperty(HTMLImageElement.prototype, 'src', {
361
+ ...originalImageSrcDescriptor,
362
+ set(value) {
363
+ if (isCrossOrigin(value)) {
364
+ var proxyUrl = toProxyUrl(value);
365
+ console.log('[DevProxy] Image:', value, '->', proxyUrl);
366
+ return originalImageSrcDescriptor.set.call(this, proxyUrl);
367
+ }
368
+ return originalImageSrcDescriptor.set.call(this, value);
369
+ }
370
+ });
371
+ }
372
+
373
+ console.log('[DevProxy] CORS proxy initialized - all cross-origin requests will be proxied');
374
+ })();
375
+
295
376
  if (window.__BUNDLE_CONFIG__ && window.__BUNDLE_CONFIG__.watch) {
296
377
  try {
297
378
  var sse = new EventSource(window.__BUNDLE_CONFIG__.sseEndpoint);