training-media-mirrors.js 1.1 KB

12345678910111213141516171819202122
  1. function trainingMediaMirrorUrl(value) {
  2. if (typeof value !== 'string') return value;
  3. const match = /^(?:https?:\/\/a018\.2018\.z01\.com|\/\/a018\.2018\.z01\.com)?(\/UploadFiles\/.+)$/i.exec(value.trim());
  4. if (!match) return value;
  5. const path = match[1];
  6. const mirrored = TRAINING_MEDIA_MIRRORS.get(path);
  7. return mirrored ? 'https://h5.xiaoshu.pro' + mirrored : value;
  8. }
  9. function trainingMediaMirrorHtml(html) {
  10. if (typeof html !== 'string') return html;
  11. return html.replace(/<(img|video|audio|source|track|a)\b(?:[^"'<>]|"[^"]*"|'[^']*')*>/gi, (tag, element) => {
  12. return tag.replace(/(\s+)([^\s=<>]+)(\s*=\s*)("[^"]*"|'[^']*'|[^\s"'=<>`]+)/g, (attribute, space, name, equals, raw) => {
  13. const key = name.toLowerCase();
  14. if (key !== 'src' && key !== 'poster' && !(element.toLowerCase() === 'a' && key === 'href')) return attribute;
  15. const quote = raw[0] === '"' || raw[0] === "'" ? raw[0] : '';
  16. const value = quote ? raw.slice(1, -1) : raw;
  17. const target = trainingMediaMirrorUrl(value);
  18. return target === value ? attribute : space + name + equals + quote + target + quote;
  19. });
  20. });
  21. }