Webpack

Expose webpack modules to userscripts

当前为 2023-08-26 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/473902/1241348/Webpack.js

  1. // ==UserScript==
  2. // @name Webpack
  3. // @namespace osu
  4. // @version 1.0.6
  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.  
  19. // Based on `Webpack-module-crack` and `moduleRaid`
  20. class Webpack {
  21. constructor(options) {
  22. if (this.injected) {
  23. return;
  24. }
  25. const { moduleId, chunkId, entryPoint } = options || {};
  26. this.moduleId = moduleId || Math.random().toString(36).substring(2, 6);
  27. this.chunkId = chunkId || Math.floor(101 + Math.random() * 899);
  28. this.modules = {};
  29. this.data = [
  30. [this.chunkId],
  31. {
  32. [this.moduleId]: (module, exports, require) => {
  33. const installedModules = require.c;
  34. for (const id in installedModules) {
  35. const exports = installedModules[id].exports;
  36. if (isNonEmptyObj(exports)) {
  37. this.modules[id] = exports;
  38. }
  39. }
  40. },
  41. },
  42. [[this.moduleId]],
  43. ];
  44. if (entryPoint) {
  45. this.inject(entryPoint);
  46. } else {
  47. this.inject("webpackJsonp");
  48. }
  49. }
  50.  
  51. inject(entryPoint) {
  52. try {
  53. if (unsafeWindow) {
  54. unsafeWindow[entryPoint].push(this.data);
  55. } else {
  56. window[entryPoint].push(this.data);
  57. }
  58. Webpack.prototype.injected = true;
  59. } catch (err) {
  60. throw new Error(`Injection failed: ${err.message}`);
  61. }
  62. }
  63. }