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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import fs from 'node:fs';
 
const srcPath = process.argv[2];
const outPath = process.argv[3];
 
let s = fs.readFileSync(srcPath, 'utf8');
const O = 'window.location.origin';
 
// v1.4 的域名都是完整的 http:// URL,逐条精确替换(含闭合引号,避免破坏语法)
// 瓦片:maponline0-3.bdimg.com/tile/  ->  /bmap-tileN/tile/
// API :api.map.baidu.com             ->  /bmap-api
const rules = [
  // ---- 瓦片(最关键)----
  ['"http://maponline0.bdimg.com/tile/"', `(${O} + "/bmap-tile0/tile/")`],
  ['"http://maponline1.bdimg.com/tile/"', `(${O} + "/bmap-tile1/tile/")`],
  ['"http://maponline2.bdimg.com/tile/"', `(${O} + "/bmap-tile2/tile/")`],
  ['"http://maponline3.bdimg.com/tile/"', `(${O} + "/bmap-tile3/tile/")`],
 
  // ---- 主 API(含带参数的完整形式,必须整串替换)----
  ['"http://api.map.baidu.com/getmodules?v=1.4"', `(${O} + "/bmap-api/getmodules?v=1.4")`],
  ['"http://api.map.baidu.com/images/"', `(${O} + "/bmap-api/images/")`],
  ['"http://api.map.baidu.com/"', `(${O} + "/bmap-api/")`],
 
  // ---- 其他静态资源 ----
  ['"http://map.baidu.com/?sr=1"', `(${O} + "/bmap-map/?sr=1")`],
  ['"http://map.baidu.com/"', `(${O} + "/bmap-map/")`],
  ['"http://d0.map.baidu.com/resource/mappic/"', `(${O} + "/bmap-api/resource/mappic/")`],
  ['"http://d1.map.baidu.com/resource/mappic/"', `(${O} + "/bmap-api/resource/mappic/")`],
  ['"http://d2.map.baidu.com/resource/mappic/"', `(${O} + "/bmap-api/resource/mappic/")`],
  ['"http://d3.map.baidu.com/resource/mappic/"', `(${O} + "/bmap-api/resource/mappic/")`],
];
 
function syntaxError(code) {
  try {
    new Function(code);
    return null;
  } catch (e) {
    return e.message;
  }
}
 
console.log('初始语法: ' + (syntaxError(s) || 'OK'));
 
let applied = 0;
for (const [from, to] of rules) {
  const n = s.split(from).length - 1;
  if (n === 0) {
    continue;
  }
  const before = s;
  s = s.split(from).join(to);
  const err = syntaxError(s);
  if (err) {
    console.log(`  X   ${from} (${n} 处) -> ${err}`);
    s = before;
  } else {
    applied += n;
    console.log(`  OK  ${from} (${n} 处)`);
  }
}
 
console.log('最终语法: ' + (syntaxError(s) || 'OK'));
console.log(`  共替换 ${applied} 处`);
 
// 关键指标校验
const hasTile = s.includes('qt=tile');
const hasVtile = s.includes('qt=vtile');
const hasBMap = /BMap\s*\.\s*version\s*=/.test(s) || s.includes('window["BMap"]');
const leftover = (s.match(/https?:\/\/[a-z0-9.-]+\.(?:baidu|bdimg)\.com/g) || []);
console.log(`  主瓦片 qt=tile: ${hasTile ? '是' : '否'}`);
console.log(`  含 qt=vtile   : ${hasVtile ? '是(可能走矢量,需注意)' : '否'}`);
console.log(`  暴露 window.BMap: ${hasBMap ? '是' : '否'}`);
console.log(`  残留百度域名引用: ${leftover.length} 处`);
if (leftover.length > 0) {
  const uniq = [...new Set(leftover)];
  console.log('    ' + uniq.slice(0, 10).join('\n    '));
}
 
fs.writeFileSync(outPath, s);
console.log('已写出: ' + outPath);