DCSS Webtiles Extension Module Loader

Load the DWEM from other Webtiles sites as well.

目前为 2024-08-17 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name DCSS Webtiles Extension Module Loader
  3. // @description Load the DWEM from other Webtiles sites as well.
  4. // @version 1.5
  5. // @author refracta
  6. // @match http://webzook.net:8080/*
  7. // @match https://crawl.kelbi.org/*
  8. // @match https://crawl.dcss.io/*
  9. // @match https://crawl.akrasiac.org:8443/*
  10. // @match https://underhound.eu:8080/*
  11. // @match https://cbro.berotato.org:8443/*
  12. // @match http://lazy-life.ddo.jp:8080/*
  13. // @match https://crawl.xtahua.com/*
  14. // @match https://crawl.project357.org/*
  15. // @match http://joy1999.codns.com:8081/*
  16. // @grant none
  17. // @run-at document-start
  18. // @namespace https://greasyfork.org/users/467840
  19. // ==/UserScript==
  20.  
  21. (function () {
  22. 'use strict';
  23. const [head] = document.getElementsByTagName('head');
  24.  
  25. async function haltRequireJS() {
  26. let disableRJSInjection = true;
  27. for (const funcName of ['appendChild', 'insertBefore']) {
  28. head['_' + funcName] = head[funcName];
  29. head[funcName] = function (tag) {
  30. if (disableRJSInjection && (tag.getAttribute('data-requirecontext') !== null || tag.getAttribute('data-requiremodule') !== null)) {
  31. return tag;
  32. }
  33. return this['_' + funcName].apply(this, arguments);
  34. }
  35. }
  36.  
  37. const scripts = head.getElementsByTagName('script');
  38. let rjsScript = Array.from(scripts).find(s => s.src?.endsWith('require.js'));
  39. if (!rjsScript) {
  40. rjsScript = {src: "/static/scripts/contrib/require.js", getAttribute: () => "/static/scripts/app"};
  41. }
  42. rjsScript?.remove?.();
  43.  
  44. const config = (() => {
  45. const dataMain = rjsScript.getAttribute('data-main');
  46. let mainScript = dataMain;
  47. const src = mainScript.split('/');
  48. mainScript = src.pop();
  49. const baseUrl = src.length ? src.join('/') + '/' : './';
  50. mainScript = mainScript.replace(/\.js$/, '');
  51. if (/^\/|:|\?|\.js$/.test(mainScript)) {
  52. mainScript = dataMain;
  53. }
  54. return {deps: [mainScript], baseUrl};
  55. })();
  56.  
  57. window.require = window.define = window.requirejs = undefined;
  58. const newRJSScript = document.createElement('script');
  59. newRJSScript.src = rjsScript.src;
  60. await new Promise(resolve => {
  61. newRJSScript.onload = newRJSScript.onreadystatechange = () => {
  62. if (!newRJSScript.readyState || /loaded|complete/.test(newRJSScript.readyState)) {
  63. newRJSScript.onload = newRJSScript.onreadystatechange = null;
  64. resolve();
  65. }
  66. };
  67. head.appendChild(newRJSScript);
  68. });
  69.  
  70. window.startMainScript = async () => {
  71. disableRJSInjection = false;
  72. require.config(config);
  73. };
  74. }
  75.  
  76. (async () => {
  77. await haltRequireJS();
  78. // localStorage.DWEM_MODULES = JSON.stringify(['https://example.org/module.js', ...]);
  79. // If DWEM_MODULES is not set, the following modules are loaded by default:
  80. localStorage.DWEM_MODULES ||= JSON.stringify(
  81. ['io-hook', 'site-information', 'websocket-factory', 'rc-manager', 'module-manager', 'sound-support', 'convenience-module']
  82. .map(m => "../modules/" + m + "/index.js")
  83. );
  84. localStorage.DWEM_LATEST_DURATION ||= 300;
  85. localStorage.DWEM_LATEST_TIME ||= 0;
  86. if (localStorage.DWEM_DEBUG === 'true') {
  87. await import(localStorage.DWEM_DEBUG_LOADER);
  88. } else {
  89. const currentTime = Date.now();
  90. if (localStorage.DWEM_LATEST_TIME && localStorage.DWEM_LATEST_DURATION) {
  91. const latestTime = parseInt(localStorage.DWEM_LATEST_TIME);
  92. const duration = parseInt(localStorage.DWEM_LATEST_DURATION);
  93. const cacheAge = (currentTime - latestTime) / 1000;
  94. console.log(`DWEM_LATEST Cache Age: ${cacheAge}s`);
  95. console.log(`DWEM_LATEST Cache Duration: ${duration}s`);
  96. if (cacheAge > duration) {
  97. try {
  98. localStorage.DWEM_LATEST = (await fetch(`https://api.github.com/repos/refracta/dcss-webtiles-extension-module/commits/main`).then(r => r.json())).sha;
  99. localStorage.DWEM_LATEST_TIME = Date.now();
  100. } catch (e) {
  101. }
  102. }
  103. }
  104. localStorage.DWEM_LATEST = localStorage.DWEM_LATEST || 'latest';
  105. await import(`https://cdn.jsdelivr.net/gh/refracta/dcss-webtiles-extension-module@${localStorage.DWEM_LATEST}/loader/dwem-core-loader.js`);
  106. }
  107. })();
  108. })();