Webpack

Expose webpack modules to userscripts

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

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

  1. function isNonEmptyObj(obj) {
  2. if (obj === null || (typeof obj !== 'function') && (typeof obj !== 'object')) {
  3. return false;
  4. }
  5. for (const prop in obj) {
  6. return true;
  7. }
  8. return false;
  9. }
  10.  
  11. // Based on `Webpack-module-crack` and `moduleRaid`
  12. class webpack {
  13. constructor(options) {
  14. const { moduleId, chunkId, entryPoint } = options || {};
  15. this.moduleId = moduleId || Math.random().toString(36).substring(2,6);
  16. this.chunkId = chunkId || Math.floor(101 + Math.random() * 899);
  17. this.modules = {};
  18. this.data = [
  19. [this.chunkId], {
  20. [this.moduleId]: (module, exports, require) => {
  21. const modules = require.m;
  22. const deferredModule = require.s;
  23. const installedModules = require.c;
  24. for (const id in installedModules) {
  25. const exports = installedModules[id].exports;
  26. if (isNonEmptyObj(exports)) {
  27. this.modules[id] = exports;
  28. }
  29. }
  30. }
  31. }, [[this.moduleId]]
  32. ];
  33. if (entryPoint) {
  34. this.inject(entryPoint);
  35. } else {
  36. this.inject("webpackJsonp");
  37. }
  38. }
  39.  
  40. inject(entryPoint) {
  41. try {
  42. if (unsafeWindow) {
  43. unsafeWindow[entryPoint].push(this.data);
  44. } else {
  45. window[entryPoint].push(this.data);
  46. }
  47. } catch(err) {
  48. console.log(`Injection failed: ${err}`);
  49. }
  50. }
  51. }