download_pdf.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. (function() {
  2. function fileExists(url) {
  3. // file:// 场景下 fetch 会被 CORS 限制,直接认为存在,交给浏览器下载失败再说
  4. if (location.protocol === 'file:') return Promise.resolve(true);
  5. return fetch(url, { method: 'HEAD' }).then(function(res) {
  6. return res.ok;
  7. }).catch(function() { return false; });
  8. }
  9. function getStaticBase() {
  10. // 通过当前脚本(download_pdf.js)已解析后的 src 反推出版本根路径,再拼接 _static/
  11. try {
  12. var scripts = document.getElementsByTagName('script');
  13. for (var i = 0; i < scripts.length; i++) {
  14. var src = scripts[i].getAttribute('src') || '';
  15. if (src.indexOf('download_pdf.js') !== -1) {
  16. var abs = new URL(src, window.location.href).href;
  17. // 去掉末尾 '_static/download_pdf.js'
  18. var base = abs.replace(/_static\/download_pdf\.js.*$/, '');
  19. return base + '_static/';
  20. }
  21. }
  22. } catch (e) {}
  23. // 兜底:相对当前页面(可能在子目录下,路径可能不正确)
  24. return '_static/';
  25. }
  26. function loadProjectInfo(staticBase) {
  27. // file:// 场景下尝试读取 window.projectInfo(由 project_info.js 提供)
  28. if (location.protocol === 'file:') {
  29. if (window.projectInfo) return Promise.resolve(window.projectInfo);
  30. // 注入一个 <script> 尝试加载 project_info.js
  31. return new Promise(function(resolve){
  32. var s = document.createElement('script');
  33. s.src = staticBase + 'project_info.js';
  34. s.onload = function(){ resolve(window.projectInfo || {}); };
  35. s.onerror = function(){ resolve({}); };
  36. document.head.appendChild(s);
  37. });
  38. }
  39. return fetch(staticBase + 'project_info.json', { cache: 'no-store' })
  40. .then(function(r){ return r.ok ? r.json() : {}; })
  41. .catch(function(){ return {}; });
  42. }
  43. function insertDownloadButton() {
  44. var container = document.querySelector('.wy-side-nav-search');
  45. if (!container) container = document.querySelector('.wy-nav-top');
  46. if (!container) return;
  47. var staticBase = getStaticBase();
  48. // 先渲染占位,避免闪烁
  49. var wrapper = document.createElement('div');
  50. wrapper.className = 'sdk-download-pdf-wrapper';
  51. wrapper.style.visibility = 'hidden';
  52. container.appendChild(wrapper);
  53. loadProjectInfo(staticBase).then(function(info){
  54. var displayName = (info && info.projectName) ? info.projectName : 'SDK 文档';
  55. // 以 config.yaml 的 project.name 为基准名;若后端未写入,则从已生成的中文名回退
  56. var baseName = displayName;
  57. // 后端也会写入 pdfFileName(中文文件名),若存在则以其去掉扩展名作为备选
  58. if (info && info.pdfFileName && /\.pdf$/i.test(info.pdfFileName)) {
  59. baseName = info.pdfFileName.replace(/\.pdf$/i, '');
  60. }
  61. var zhPdfName = baseName + '.pdf';
  62. var enPdfName = baseName.replace(/\s+/g, '_') + '_EN.pdf';
  63. // 检测当前语言
  64. var currentLanguage = detectCurrentLanguage();
  65. console.log('PDF下载UI检测到当前语言:', currentLanguage);
  66. // 根据当前语言创建对应的按钮
  67. var button = null;
  68. if (currentLanguage === 'zh') {
  69. // 中文按钮
  70. button = document.createElement('a');
  71. button.href = staticBase + zhPdfName;
  72. button.setAttribute('download', displayName + '.pdf');
  73. button.setAttribute('aria-label', '下载 ' + displayName + '下载 PDF');
  74. button.className = 'sdk-download-pdf-btn sdk-download-pdf-btn--zh';
  75. button.innerHTML = '\n <span class="sdk-pdf-icon" aria-hidden="true">\u2B07\uFE0F</span>\n <span class="sdk-pdf-text">下载 PDF</span>\n ';
  76. } else {
  77. // 英文按钮
  78. button = document.createElement('a');
  79. button.href = staticBase + enPdfName;
  80. button.setAttribute('download', (displayName || 'SDK Docs') + '_EN.pdf');
  81. button.setAttribute('aria-label', 'Download ' + (displayName || 'SDK Docs') + 'Download PDF');
  82. button.className = 'sdk-download-pdf-btn sdk-download-pdf-btn--en';
  83. button.innerHTML = '\n <span class="sdk-pdf-icon" aria-hidden="true">\u2B07\uFE0F</span>\n <span class="sdk-pdf-text">Download PDF</span>\n ';
  84. }
  85. // 将按钮添加到容器
  86. wrapper.appendChild(button);
  87. // 检查PDF文件是否存在
  88. fileExists(button.href).then(function(exists){
  89. wrapper.style.visibility = exists ? 'visible' : 'hidden';
  90. if (!exists) wrapper.remove();
  91. });
  92. });
  93. }
  94. /**
  95. * 检测当前页面的语言
  96. */
  97. function detectCurrentLanguage() {
  98. const htmlLang = document.documentElement.getAttribute('lang');
  99. const currentPath = window.location.pathname;
  100. // 根据HTML lang属性和URL路径判断语言
  101. if (htmlLang === 'zh-CN' || currentPath.includes('_zh.html')) {
  102. return 'zh';
  103. } else if (htmlLang === 'en' || currentPath.endsWith('.html')) {
  104. return 'en';
  105. }
  106. // 默认返回英文
  107. return 'en';
  108. }
  109. if (document.readyState === 'loading') {
  110. document.addEventListener('DOMContentLoaded', insertDownloadButton);
  111. } else {
  112. insertDownloadButton();
  113. }
  114. })();