🏠 Home 

CKAutoLoader

[已弃用] CKAutoLoader用于在哔哩哔哩播放器页面延后加载脚本,参考了Pakku弹幕插件的加载方式。

สคริปต์นี้ไม่ควรถูกติดตั้งโดยตรง มันเป็นคลังสำหรับสคริปต์อื่น ๆ เพื่อบรรจุด้วยคำสั่งเมทา // @require https://update.greasyfork.org/scripts/405344/1027351/CKAutoLoader.js

  1. // ==UserScript==
  2. // @name CKAutoLoader
  3. // @namespace blbljsloader.ckylin.site
  4. // @version 0.2
  5. // @author CKylinMC
  6. // @grant unsafeWindow
  7. // @license GPLv3 License
  8. // ==/UserScript==
  9. if (!window.CKAutoLoader) {
  10. window.CKAutoLoader = {
  11. loaded: false,
  12. inited: false,
  13. loader_Started: false,
  14. commentDetecterTriggered: false,
  15. cblist: {},
  16. retry_count: 50,
  17. playerLoadedEvent: null,
  18. commentLoadedEvent: null,
  19. start: function () {
  20. if (!window.CKAutoLoader.loader_Started) {
  21. window.CKAutoLoader.loader();
  22. }
  23. },
  24. initEvent: function () {
  25. if (window.CKAutoLoader.inited) return;
  26. if (window.CKAutoLoader.playerLoadedEvent != null) return;
  27. window.CKAutoLoader.playerLoadedEvent = new Event("ckBilibiliPlayerLoaded");
  28. window.CKAutoLoader.commentLoadedEvent = new Event("ckBilibiliCommentLoaded");
  29. },
  30. reg: function (name, callback) {
  31. if (window.CKAutoLoader.loaded) {
  32. if (callback instanceof Function) {
  33. callback();
  34. }
  35. return;
  36. }
  37. window.CKAutoLoader.cblist[name] = callback;
  38. window.CKAutoLoader.start();
  39. },
  40. canInject: function () {
  41. //参考pakku的检测加载机制
  42. let blplayer = document.querySelector("div.bilibili-player");
  43. if (blplayer && !blplayer.querySelector(".bilibili-player-auxiliary-area")) {
  44. blplayer = blplayer.closest("body");
  45. }
  46. if (blplayer) {
  47. var list_elem = blplayer.querySelector(".bilibili-player-danmaku, .player-auxiliary-danmaku-wrap")
  48. }
  49. if (!blplayer || !list_elem) {
  50. return false;
  51. }
  52. return true;
  53. },
  54. startCommentDetecter: function () {
  55. if (window.CKAutoLoader.commentDetecterTriggered) return;
  56. window.CKAutoLoader.commentDetecterTriggered = true;
  57. window.CKAutoLoader.commentDetecter();
  58. },
  59. commentDetecter: function () {
  60. let commentElement = document.querySelector("#comment");
  61. if (!commentElement) return;
  62. if (commentElement.hasAttribute("scrollshow") &&
  63. commentElement.getAttribute("scrollshow") == "true") {
  64. window.dispatchEvent(window.CKAutoLoader.commentLoadedEvent);
  65. } else {
  66. setTimeout(() => {
  67. window.CKAutoLoader.commentDetecter()
  68. }, 1000);
  69. }
  70. },
  71. loader: function () {
  72. window.CKAutoLoader.initEvent();
  73. window.CKAutoLoader.loader_Started = true;
  74. window.CKAutoLoader.startCommentDetecter();
  75. console.log("CKAutoLoader: try inject...");
  76. if (!window.CKAutoLoader.canInject()) {
  77. if (window.CKAutoLoader.retry_count == undefined || --window.CKAutoLoader.retry_count <= 0) {
  78. console.error("CKAutoLoader: Can NOT inject scripts.");
  79. return;
  80. }
  81. setTimeout(function () {
  82. window.CKAutoLoader.loader()
  83. }, 200);
  84. return;
  85. }
  86. window.dispatchEvent(window.CKAutoLoaderplayerLoadedEvent);
  87. window.CKAutoLoader.loaded = true;
  88. for (func in window.CKAutoLoader.cblist) {
  89. if (window.CKAutoLoader.cblist[func] instanceof Function) {
  90. try {
  91. window.CKAutoLoader.cblist[func]();
  92. } catch (e) {
  93. console.error("CKAutoLoader: Errored while call: " + func + e);
  94. }
  95. } else {
  96. console.error("CKAutoLoader: Can NOT call: " + func);
  97. }
  98. }
  99. },
  100. };
  101. window.CKAutoLoader.start();
  102. }