From 5955bd821303cffa35263c41286339fb28e7536b Mon Sep 17 00:00:00 2001 From: Arpit Chakladar <54011232+arpitchakladar@users.noreply.github.com> Date: Mon, 27 Mar 2023 14:21:49 +0530 Subject: Added auto fill otp for mutation application. Scoping the outputs of all scripts in iife. Using webpack-merge to merge configuration options. --- config/webpack.config.js | 93 +++++++++++++--------- .../plugins/inject-script-webpack-plugin.js | 1 - package.json | 4 +- src/scripts.json | 1 + src/scripts/otps/grievance-application.ts | 26 ++---- src/scripts/otps/mutation-application.ts | 6 ++ src/shared/autofill-otp.ts | 31 ++++++++ yarn.lock | 12 +-- 8 files changed, 100 insertions(+), 74 deletions(-) create mode 100644 src/scripts/otps/mutation-application.ts create mode 100644 src/shared/autofill-otp.ts diff --git a/config/webpack.config.js b/config/webpack.config.js index 7a84e88..206eea1 100644 --- a/config/webpack.config.js +++ b/config/webpack.config.js @@ -1,5 +1,6 @@ const path = require("path"); const fs = require("fs"); +const { merge } = require("webpack-merge"); const CopyPlugin = require("copy-webpack-plugin"); global.ROOT_DIR = path.resolve(__dirname, ".."); @@ -76,49 +77,63 @@ const ecmaScriptModuleOptions = { } }; -const uninjectedScriptConfiguration = { - ...commonOptions, - ...ecmaScriptModuleOptions, - entry: uninjectedScriptEntries, - plugins: [ - new CopyPlugin({ - patterns: [{ - from: path.resolve(ROOT_DIR, "static"), - to: "./" - }] - }), - new CreateManifestPlugin() - ], - module: { - rules: [ - { - test: /\.(js|ts)$/, - loader: "./config/webpack/loader/dynamic-imports-loader" - }, - ...commonOptions.module.rules - ] +const scriptOptions = { + output: { + iife: true } }; -const injectedScriptConfiguration = { - ...commonOptions, - entry: injectedScriptEntries, - plugins: [ - new InjectScriptPlugin(), - ] -}; - -const sharedModulesConfiguration = { - ...commonOptions, - ...ecmaScriptModuleOptions, - output: { - library: { - type: "module" +const uninjectedScriptConfiguration = merge( + commonOptions, + ecmaScriptModuleOptions, + scriptOptions, + { + entry: uninjectedScriptEntries, + plugins: [ + new CopyPlugin({ + patterns: [{ + from: path.resolve(ROOT_DIR, "static"), + to: "./" + }] + }), + new CreateManifestPlugin() + ], + module: { + rules: [ + { + enforce: "post", + test: /\.(js|ts)$/, + loader: "./config/webpack/loader/dynamic-imports-loader" + } + ] } - }, - entry: sharedModuleEntries, - externals: sharedModuleExternals -}; + } +); + +const injectedScriptConfiguration = merge( + commonOptions, + scriptOptions, + { + entry: injectedScriptEntries, + plugins: [ + new InjectScriptPlugin() + ] + } +); + +const sharedModulesConfiguration = merge( + commonOptions, + ecmaScriptModuleOptions, + { + output: { + library: { + type: "module" + } + }, + entry: sharedModuleEntries, + externals: sharedModuleExternals + } +); module.exports = [ uninjectedScriptConfiguration, diff --git a/config/webpack/plugins/inject-script-webpack-plugin.js b/config/webpack/plugins/inject-script-webpack-plugin.js index 8d72ea2..862b3f0 100644 --- a/config/webpack/plugins/inject-script-webpack-plugin.js +++ b/config/webpack/plugins/inject-script-webpack-plugin.js @@ -1,5 +1,4 @@ const { sources } = require("webpack"); -const terser = require("terser"); const { getInjectionCode } = webpackRequire("utils/get-injection-code"); diff --git a/package.json b/package.json index 4cfb182..3506709 100644 --- a/package.json +++ b/package.json @@ -16,10 +16,10 @@ "@types/jquery": "^3.5.16", "copy-webpack-plugin": "^11.0.0", "import-manager": "^0.3.3", - "terser": "^5.16.3", "ts-loader": "^9.4.2", "typescript": "^4.9.5", "webpack": "^5.75.0", - "webpack-cli": "^5.0.1" + "webpack-cli": "^5.0.1", + "webpack-merge": "^5.8.0" } } diff --git a/src/scripts.json b/src/scripts.json index 5fb63e6..a2e48fd 100644 --- a/src/scripts.json +++ b/src/scripts.json @@ -8,6 +8,7 @@ "injected": { "login.ts": "*", "otps/grievance-application.ts": "GrievAppViewAction.action", + "otps/mutation-application.ts": "MuteApplication.action", "functionality/view-khatian.ts": "KnowYourProperty.action" }, "loaded": { diff --git a/src/scripts/otps/grievance-application.ts b/src/scripts/otps/grievance-application.ts index 140504c..10d38ef 100644 --- a/src/scripts/otps/grievance-application.ts +++ b/src/scripts/otps/grievance-application.ts @@ -1,22 +1,6 @@ -document.addEventListener("DOMContentLoaded", () => { - const proxiedPost = $.post; +import { addAutofillOtps } from "@/shared/autofill-otp"; - $.post = function() { - let element: HTMLInputElement | null = null; - if (arguments[0].endsWith("MISPetitionSmsSend.action")) { - element = document.querySelector("input[name=mobile_Code]") as HTMLInputElement; - } else if (arguments[0].endsWith("MISPetitionEmailSend.action")) { - element = document.querySelector("input[name=txtemail_Code]") as HTMLInputElement; - } - - if (element) { - const callback = arguments[2]; - arguments[2] = (data: any) => { - element!.value = data?.messageShow || ""; - callback(data); - }; - } - - return proxiedPost.apply(this, Array.from(arguments) as any); - }; -}); +addAutofillOtps([ + ["MISPetitionSmsSend.action", "input[name=mobile_Code]"], + ["MISPetitionEmailSend.action", "input[name=txtemail_Code]"] +]); diff --git a/src/scripts/otps/mutation-application.ts b/src/scripts/otps/mutation-application.ts new file mode 100644 index 0000000..ecf6aaa --- /dev/null +++ b/src/scripts/otps/mutation-application.ts @@ -0,0 +1,6 @@ +import { addAutofillOtps } from "@/shared/autofill-otp"; + +addAutofillOtps([ + ["muteSmsSend.action", "#mobile_CodeMute"], + ["muteEmailSend.action", "#txtemail_CodeMute"] +]); diff --git a/src/shared/autofill-otp.ts b/src/shared/autofill-otp.ts new file mode 100644 index 0000000..e05ea7c --- /dev/null +++ b/src/shared/autofill-otp.ts @@ -0,0 +1,31 @@ +type AutofillOTPRule = readonly [string, string] + +let otpAutofillList: AutofillOTPRule[] = []; + +document.addEventListener("DOMContentLoaded", () => { + const proxiedPost = $.post; + + $.post = function() { + for (const rule of otpAutofillList) { + if (arguments[0].endsWith(rule[0])) { + const element: HTMLInputElement | null = document.querySelector(rule[1]) as HTMLInputElement | null; + + if (element) { + const callback = arguments[2]; + arguments[2] = (data: any) => { + element!.value = data?.messageShow || ""; + callback(data); + }; + } + + break; + } + } + + return proxiedPost.apply(this, Array.from(arguments) as any); + }; +}); + +export const addAutofillOtps = (addedOTPAutofillList: AutofillOTPRule[]) => { + otpAutofillList = otpAutofillList.concat(addedOTPAutofillList); +}; diff --git a/yarn.lock b/yarn.lock index 0d5c32e..54c1e16 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1017,16 +1017,6 @@ terser@^5.14.1: commander "^2.20.0" source-map-support "~0.5.20" -terser@^5.16.3: - version "5.16.3" - resolved "https://registry.yarnpkg.com/terser/-/terser-5.16.3.tgz#3266017a9b682edfe019b8ecddd2abaae7b39c6b" - integrity sha512-v8wWLaS/xt3nE9dgKEWhNUFP6q4kngO5B8eYFUuebsu7Dw/UNAnpUod6UHo04jSSkv8TzKHjZDSd7EXdDQAl8Q== - dependencies: - "@jridgewell/source-map" "^0.3.2" - acorn "^8.5.0" - commander "^2.20.0" - source-map-support "~0.5.20" - to-regex-range@^5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-5.0.1.tgz#1648c44aae7c8d988a326018ed72f5b4dd0392e4" @@ -1091,7 +1081,7 @@ webpack-cli@^5.0.1: rechoir "^0.8.0" webpack-merge "^5.7.3" -webpack-merge@^5.7.3: +webpack-merge@^5.7.3, webpack-merge@^5.8.0: version "5.8.0" resolved "https://registry.yarnpkg.com/webpack-merge/-/webpack-merge-5.8.0.tgz#2b39dbf22af87776ad744c390223731d30a68f61" integrity sha512-/SaI7xY0831XwP6kzuwhKWVKDP9t1QY1h65lAFLbZqMPIuYcD9QAW4u9STIbU9kaJbPBB/geU/gLr1wDjOhQ+Q== -- cgit v1.2.3