DCSS Webtiles Extension Module Loader

Load the DWEM from other Webtiles sites as well.

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