此处的媒体资源仅用作样例展示,生产环境中请自行更换为您持有使用权的媒体资源,非常感谢。
   
   
配置说明
引入的新文件
请将下述文件放置于 source 目录中的合适位置,在此处我们使用的是 others 目录。
random-post-cover.js1 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
   | (() => {      const defaultCoverSrc = "/images/default.webp";      const randomImageSrcTemplate = "https://www.unpkg.com/[email protected]/source/images/thumb/thumb_{no}.webp";      const randomImageCount = 20;
       const usedImages = new Array(randomImageCount);
       const generateNewCoverID = () => {     let remailFailCounts = 2;      let imageNo;     while (remailFailCounts > 0) {              imageNo = Math.floor(Math.random() * randomImageCount);       if (!usedImages[imageNo]) {                  break;       } else {                  remailFailCounts--;       }     }
      if (remailFailCounts <= 0) {              imageNo = -1;       for (let i = 0; i < randomImageCount; i++) {         if (!usedImages[i]) {                      imageNo = i;           break;         }       }       if (imageNo === -1) {                  for (let i = 0; i < randomImageCount; i++) {           usedImages[i] = false;         }                  imageNo = Math.floor(Math.random() * randomImageCount);       }     }
           usedImages[imageNo] = true;
           return imageNo + 1;   }
       const randAll = () => {     const allDefaultImageEls = document.querySelectorAll(`img.kratos-entry-thumb-img[src='${defaultCoverSrc}']`);     for (const el of allDefaultImageEls) {       el.setAttribute('src', randomImageSrcTemplate.replace("{no}", generateNewCoverID().toString()));     }   }
       randAll();      window.addEventListener('pjax:complete', randAll); })();
  | 
 
更新的配置项
需要更新 additional_injections ,加入以下内容:
1 2 3
   | additional_injections:   after_footer: |     <script src="/others/random-post-cover.js"></script>
   | 
 
假设您把上述的文件放置在 source 目录的 others 目录中,且您的站点配置为根目录模式,那么您应当可以通过上述的路径 /others/random-post-cover.js 访问到对应的文件。如果您使用的是其他配置,您需要对应调整这个路径。
当您配置完成后重启 Hexo ,您应当可以看见使用默认图片的文章图片被自动更换。预祝您使用愉快。
为避免让访客感到疑惑,您还可以修改配置项,使用一张「加载中」的图片作为默认的封面路径。