tuqin
3 天以前 abfe3ffe5f6d161f2a6466c8bdbcbc17de1e64b6
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
import fs from 'node:fs';
 
const srcPath = process.argv[2];
const outPath = process.argv[3];
 
let s = fs.readFileSync(srcPath, 'utf8');
const H = 'window.location.host';
 
// 每条规则:[原串, 替换串]
const rules = [
  // 主服务域名
  ['"//api.map.baidu.com', `"//" + ${H} + "/bmap-api`],
  // 瓦片域名(裸域名,数组元素)→ 带括号表达式
  ['"apimaponline0.bdimg.com"', `(${H} + "/bmap-tile0")`],
  ['"apimaponline1.bdimg.com"', `(${H} + "/bmap-tile1")`],
  ['"apimaponline2.bdimg.com"', `(${H} + "/bmap-tile2")`],
  ['"apimaponline3.bdimg.com"', `(${H} + "/bmap-tile3")`],
  // 瓦片域名带路径(卫星图 starpic):保留后续路径
  ['"apimaponline0.bdimg.com/', `(${H} + "/bmap-tile0/`],
  ['"apimaponline1.bdimg.com/', `(${H} + "/bmap-tile1/`],
  ['"apimaponline2.bdimg.com/', `(${H} + "/bmap-tile2/`],
  ['"apimaponline3.bdimg.com/', `(${H} + "/bmap-tile3/`],
  // 静态资源
  ['"//webmap0.bdimg.com', `"//" + ${H} + "/bmap-webmap`],
  ['"//maponline0.bdimg.com', `"//" + ${H} + "/bmap-webmap`],
];
 
function syntaxError(code) {
  try {
    new Function(code);
    return null;
  } catch (e) {
    return e.message;
  }
}
 
console.log('初始语法: ' + (syntaxError(s) || 'OK'));
 
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 {
    console.log(`  OK  ${from}  (${n} 处)`);
  }
}
 
console.log('最终语法: ' + (syntaxError(s) || 'OK'));
 
const tile = (s.match(/\/bmap-tile/g) || []).length;
const api = (s.match(/\/bmap-api/g) || []).length;
console.log(`  改写点: bmap-tile ${tile} 处, bmap-api ${api} 处`);
console.log('  主瓦片用 qt=tile: ' + (s.includes('qt=tile') ? '是' : '否'));
console.log('  含 qt=vtile: ' + (s.includes('qt=vtile') ? '是' : '否'));
 
fs.writeFileSync(outPath, s);
console.log('已写出: ' + outPath);