Webpack

Expose webpack modules to userscripts

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

  1. // ==UserScript==
  2. // @name Webpack
  3. // @namespace osu
  4. // @version 1.0.7
  5. // @description Expose webpack modules to userscripts
  6. // @author Magnus Cosmos
  7. // ==/UserScript==
  8.  
  9. function isNonEmptyObj(obj) {
  10. if (obj === null || (typeof obj !== "function" && typeof obj !== "object")) {
  11. return false;
  12. }
  13. for (const _key in obj) {
  14. return true;
  15. }
  16. return false;
  17. }
  18. class webpack {
  19. constructor() {
  20. if (this.constructor == webpack) {
  21. throw new Error("webpack class cannot be instantiated.");
  22. }
  23. this.loaded = false;
  24. this.modules = {};
  25. }
  26.  
  27. inject(entryPoint, data) {
  28. try {
  29. if (unsafeWindow) {
  30. unsafeWindow[entryPoint].push(data);
  31. } else {
  32. window[entryPoint].push(data);
  33. }
  34. } catch (err) {
  35. throw new Error(`Injection failed: ${err.message}`);
  36. }
  37. }
  38. }
  39. // Based on `Webpack-module-crack` and `moduleRaid`
  40. class Webpack extends webpack {
  41. constructor(options) {
  42. super();
  43. if (this.loaded) {
  44. return;
  45. }
  46. let { moduleId, chunkId, entryPoint } = options || {};
  47. moduleId = moduleId || Math.random().toString(36).substring(2, 6);
  48. chunkId = chunkId || Math.floor(101 + Math.random() * 899);
  49. entryPoint = entryPoint || "webpackJsonp";
  50. const data = [
  51. [chunkId],
  52. {
  53. [moduleId]: (_module, _exports, require) => {
  54. const installedModules = require.c;
  55. for (const id in installedModules) {
  56. const exports = installedModules[id].exports;
  57. if (isNonEmptyObj(exports)) {
  58. this.modules[id] = exports;
  59. }
  60. }
  61. },
  62. },
  63. [[moduleId]],
  64. ];
  65. this.inject(entryPoint, data);
  66. this.loaded = true;
  67. }
  68. }