File size: 1,064 Bytes
65a720c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// hf-fix.js (ROOT)
// Chặn lỗi "No response from server" bằng cách ép provinces dùng file local provinces.json

(() => {
  const LOG = "[C2M-FIX]";
  const nativeFetch = window.fetch.bind(window);

  const isProvincesUrl = (u) => {
    try {
      const url = new URL(u, location.href);
      const p = url.pathname || "";
      return (
        p.endsWith("/provinces") ||
        p.endsWith("/api/provinces") ||
        p.includes("/provinces?")
      );
    } catch {
      const s = String(u || "");
      return s.includes("provinces");
    }
  };

  window.fetch = function patchedFetch(input, init) {
    const url = typeof input === "string" ? input : (input && input.url) || "";

    // Ép mọi call provinces về local file
    if (isProvincesUrl(url)) {
      console.info(`${LOG} redirect provinces -> ./provinces.json`);
      return nativeFetch("./provinces.json", { cache: "no-store" });
    }

    return nativeFetch(input, init);
  };

  console.info(`${LOG} installed (root, no folders).`);
})();