DCSS Webtiles Extension Module Loader

Load the DWEM from other Webtiles sites as well.

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

  1. // ==UserScript==
  2. // @name DCSS Webtiles Extension Module Loader
  3. // @description Load the DWEM from other Webtiles sites as well.
  4. // @version 1.4
  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. localStorage.DWEM_LATEST_DURATION ||= 300;
  80. localStorage.DWEM_LATEST_TIME ||= 0;
  81. if (localStorage.DWEM_DEBUG === 'true') {
  82. await import(localStorage.DWEM_DEBUG_LOADER);
  83. } else {
  84. const currentTime = Date.now();
  85. if (localStorage.DWEM_LATEST_TIME && localStorage.DWEM_LATEST_DURATION) {
  86. const latestTime = parseInt(localStorage.DWEM_LATEST_TIME);
  87. const duration = parseInt(localStorage.DWEM_LATEST_DURATION);
  88. const cacheAge = (currentTime - latestTime) / 1000;
  89. console.log(`DWEM_LATEST Cache Age: ${cacheAge}s`);
  90. console.log(`DWEM_LATEST Cache Duration: ${duration}s`);
  91. if (cacheAge > duration) {
  92. try {
  93. localStorage.DWEM_LATEST = (await fetch(`https://api.github.com/repos/refracta/dcss-webtiles-extension-module/commits/main`).then(r => r.json())).sha;
  94. localStorage.DWEM_LATEST_TIME = Date.now();
  95. } catch (e) {
  96. }
  97. }
  98. }
  99. localStorage.DWEM_LATEST = localStorage.DWEM_LATEST || 'latest';
  100. await import(`https://cdn.jsdelivr.net/gh/refracta/dcss-webtiles-extension-module@${localStorage.DWEM_LATEST}/loader/dwem-core-loader.js`);
  101. }
  102. })();
  103. })();