DCSS Webtiles Extension Module Loader

Load the DWEM from other Webtiles sites as well.

当前为 2024-06-23 提交的版本,查看 最新版本

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