UNPKG

moduleraid

Version:
3 lines (2 loc) 4.44 kB
function t(){return t=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var o=arguments[e];for(var s in o)Object.prototype.hasOwnProperty.call(o,s)&&(t[s]=o[s])}return t},t.apply(this,arguments)}export default class{constructor(e){this.entrypoint=void 0,this.debug=void 0,this.strict=void 0,this.moduleID=Math.random().toString(36).substring(7),this.functionArguments=[[[0],[(t,e,o)=>{this.modules=o.c,this.constructors=o.m,this.get=o}]],[[1e3],{[this.moduleID]:(t,e,o)=>{this.modules=o.c,this.constructors=o.m,this.get=o}},[[this.moduleID]]]],this.arrayArguments=[[[this.moduleID],{},t=>{Object.keys(t.m).forEach(e=>{try{this.modules[e]=t(e)}catch(t){this.log(`[arrayArguments/1] Failed to require(${e}) with error:\n${t}\n${t.stack}`)}}),this.get=t}],this.functionArguments[1]],this.modules={},this.constructors=[],this.get=void 0;let o={entrypoint:"webpackJsonp",debug:!1,strict:!1};"object"==typeof e&&(o=t({},o,e)),this.entrypoint=o.entrypoint,this.debug=o.debug,this.strict=o.strict,this.detectEntrypoint(),this.fillModules(),this.replaceGet(),this.setupPushEvent()}log(t){this.debug&&console.warn(`[moduleRaid] ${t}`)}replaceGet(){null===this.get&&(this.get=t=>this.modules[t])}fillModules(){if("function"==typeof window[this.entrypoint]?this.functionArguments.forEach((t,e)=>{try{if(this.modules&&Object.keys(this.modules).length>0)return;window[this.entrypoint](...t)}catch(t){this.log(`moduleRaid.functionArguments[${e}] failed:\n${t}\n${t.stack}`)}}):this.arrayArguments.forEach((t,e)=>{try{if(this.modules&&Object.keys(this.modules).length>0)return;window[this.entrypoint].push(t)}catch(t){this.log(`Pushing moduleRaid.arrayArguments[${e}] into ${this.entrypoint} failed:\n${t}\n${t.stack}`)}}),this.modules&&0==Object.keys(this.modules).length){let t=!1,e=0;if("function"!=typeof window[this.entrypoint]||!window[this.entrypoint]([],[],[e]))throw Error("Unknown Webpack structure");for(;!t;)try{this.modules[e]=window[this.entrypoint]([],[],[e]),e++}catch(e){t=!0}}}setupPushEvent(){const t=window[this.entrypoint].push;window[this.entrypoint].push=(...e)=>{const o=Reflect.apply(t,window[this.entrypoint],e);return document.dispatchEvent(new CustomEvent("moduleraid:webpack-push",{detail:e})),o}}detectEntrypoint(){if(null!=window[this.entrypoint])return;if(this.strict)throw Error(`Strict mode is enabled and entrypoint at window.${this.entrypoint} couldn't be found. Please specify the correct one!`);let t=Object.keys(window);if(t=t.filter(t=>t.toLowerCase().includes("chunk")||t.toLowerCase().includes("webpack")).filter(t=>"function"==typeof window[t]||Array.isArray(window[t])),t.length>1)throw Error(`Multiple possible endpoints have been detected, please create a new moduleRaid instance with a specific one:\n${t.join(", ")}`);if(0===t.length)throw Error("No Webpack JSONP entrypoints could be detected");this.log(`Entrypoint has been detected at window.${t[0]} and set for injection`),this.entrypoint=t[0]}searchObject(t,e){for(const o in t){const s=t[o],n=e.toLowerCase();if("object"!=typeof s){if(o.toString().toLowerCase().includes(n))return!0;if("object"!=typeof s){if(s.toString().toLowerCase().includes(n))return!0}else if(this.searchObject(s,e))return!0}}return!1}findModule(t){const e=[],o=Object.keys(this.modules);if(0===o.length)throw new Error("There are no modules to search through!");return o.forEach(o=>{const s=this.modules[o.toString()];if(void 0!==s)try{if("string"==typeof t)switch(t=t.toLowerCase(),typeof s){case"string":s.toLowerCase().includes(t)&&e.push(s);break;case"function":s.toString().toLowerCase().includes(t)&&e.push(s);break;case"object":this.searchObject(s,t)&&e.push(s)}else{if("function"!=typeof t)throw new TypeError(`findModule can only find via string and function, ${typeof t} was passed`);t(s)&&e.push(s)}}catch(t){this.log(`There was an error while searching through module '${o}':\n${t}\n${t.stack}`)}}),e}findConstructor(t){const e=[],o=Object.keys(this.constructors);if(0===o.length)throw new Error("There are no constructors to search through!");return o.forEach(o=>{const s=this.constructors[o];try{"string"==typeof t?(t=t.toLowerCase(),s.toString().toLowerCase().includes(t)&&e.push([this.constructors[o],this.modules[o]])):"function"==typeof t&&t(s)&&e.push([this.constructors[o],this.modules[o]])}catch(t){this.log(`There was an error while searching through constructor '${o}':\n${t}\n${t.stack}`)}}),e}} //# sourceMappingURL=moduleraid.modern.js.map