diff options
| author | Arpit Chakladar <arpitchakladar+git@gmail.com> | 2026-07-13 11:35:08 +0530 |
|---|---|---|
| committer | Arpit Chakladar <arpitchakladar+git@gmail.com> | 2026-07-13 11:35:08 +0530 |
| commit | 600c337b1cdc884aebf59083eb57b178b43441ce (patch) | |
| tree | d1f3cb7eb1020bcdcd66d9dbf0279f79d5856332 /src | |
| parent | 98e7019c61ea0ffe344ef7ce809d38d62254a9fc (diff) | |
| download | banglar-bhumi-utils-600c337b1cdc884aebf59083eb57b178b43441ce.tar.gz banglar-bhumi-utils-600c337b1cdc884aebf59083eb57b178b43441ce.zip | |
docs: added some documentation comments to all functions
Diffstat (limited to 'src')
| -rw-r--r-- | src/background/run-ocr.ts | 9 | ||||
| -rw-r--r-- | src/offscreen/ocr/index.js | 14 | ||||
| -rw-r--r-- | src/scripts/captcha/application-receipt.ts | 5 | ||||
| -rw-r--r-- | src/scripts/captcha/know-your-property.ts | 5 | ||||
| -rw-r--r-- | src/scripts/functionality/mutation-application.ts | 6 | ||||
| -rw-r--r-- | src/scripts/functionality/sheet-map/index.ts | 26 | ||||
| -rw-r--r-- | src/scripts/functionality/view-khatian/index.ts | 17 | ||||
| -rw-r--r-- | src/scripts/login/index.ts | 15 | ||||
| -rw-r--r-- | src/scripts/stop-blocking.ts | 8 | ||||
| -rw-r--r-- | src/shared/generate-web-page.ts | 7 | ||||
| -rw-r--r-- | src/shared/intercept-jquery-ajax.ts | 20 | ||||
| -rw-r--r-- | src/shared/modify-dom.ts | 14 | ||||
| -rw-r--r-- | src/shared/observe-dom.ts | 14 | ||||
| -rw-r--r-- | src/shared/script-injector.ts | 9 | ||||
| -rw-r--r-- | src/shared/styling.ts | 7 |
15 files changed, 176 insertions, 0 deletions
diff --git a/src/background/run-ocr.ts b/src/background/run-ocr.ts index 7975fc0..4d0b080 100644 --- a/src/background/run-ocr.ts +++ b/src/background/run-ocr.ts @@ -1,6 +1,10 @@ const OFFSCREEN_DOCUMENT_PATH = "offscreen/ocr/index.html"; // Function to ensure the offscreen document is open +/** + * Ensures the offscreen OCR document is open. If it already exists + * this is a no-op. + */ async function setupOffscreenDocument() { if (await chrome.offscreen.hasDocument()) { return; // Offscreen document already open @@ -13,6 +17,11 @@ async function setupOffscreenDocument() { } // Listen for messages from content scripts (and popup if applicable) +/** + * Listens for `"OCR"` messages from content scripts. Forwards the + * image data URL to an offscreen document for Tesseract.js processing + * and sends the recognised text back to the caller. + */ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.type === "OCR" && message.dataURL) { (async () => { diff --git a/src/offscreen/ocr/index.js b/src/offscreen/ocr/index.js index 432811e..aab9da8 100644 --- a/src/offscreen/ocr/index.js +++ b/src/offscreen/ocr/index.js @@ -4,6 +4,15 @@ let ocrWorker = null; const WORKER_PATH = chrome.runtime.getURL("offscreen/ocr/static/worker.min.js"); const CORE_PATH = chrome.runtime.getURL("offscreen/ocr/static"); +/** + * Creates (or reuses) a Tesseract.js worker and recognises text from + * the given image data URL. The worker is configured to only recognise + * uppercase alphanumerics (excluding `0`, `O`, `1`, `I`) suitable for + * CAPTCHA text. + * + * @param {string} dataURL - A `data:image/png;base64,…` string. + * @returns {{ text: string, confidence: number }} + */ async function performOcrInOffscreen(dataURL) { if (!ocrWorker) { ocrWorker = await Tesseract.createWorker("eng", 1, { @@ -23,6 +32,11 @@ async function performOcrInOffscreen(dataURL) { } // Listen for messages from the Service Worker +/** + * Handles two message types from the background service worker: + * - `"OFFSCREEN_OCR_REQUEST"`: runs OCR on the supplied data URL. + * - `"OFFSCREEN_TERMINATE_OCR_WORKER"`: terminates the worker to free memory. + */ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.type === "OFFSCREEN_OCR_REQUEST" && message.dataURL) { performOcrInOffscreen(message.dataURL) diff --git a/src/scripts/captcha/application-receipt.ts b/src/scripts/captcha/application-receipt.ts index 595195e..c5bb657 100644 --- a/src/scripts/captcha/application-receipt.ts +++ b/src/scripts/captcha/application-receipt.ts @@ -1,3 +1,8 @@ +/** + * Hides the CAPTCHA section on the application-receipt page and + * stubs the form validation so the user can proceed without + * solving a CAPTCHA. + */ document.addEventListener("DOMContentLoaded", () => { $("#werter > div > form > div:nth-child(4)").hide(); (window as any).validateForm = () => true; diff --git a/src/scripts/captcha/know-your-property.ts b/src/scripts/captcha/know-your-property.ts index 6e11a3f..3abdf08 100644 --- a/src/scripts/captcha/know-your-property.ts +++ b/src/scripts/captcha/know-your-property.ts @@ -1,3 +1,8 @@ +/** + * Hides the CAPTCHA section on the Know-Your-Property page and + * stubs the captcha validation so the user can proceed without + * solving a CAPTCHA. + */ document.addEventListener("DOMContentLoaded", () => { $("#khatianPlotDiv > div:nth-child(6) > div").hide(); (window as any).validateCaptcha = () => true; diff --git a/src/scripts/functionality/mutation-application.ts b/src/scripts/functionality/mutation-application.ts index 96aec8a..c067b25 100644 --- a/src/scripts/functionality/mutation-application.ts +++ b/src/scripts/functionality/mutation-application.ts @@ -3,6 +3,12 @@ import { observeDOM } from "@/shared/observe-dom"; const sanghaFacilitationCentreBannerUrl = "$l{ /assets/sangha-facilitation-centre-banner.jpg }l$"; +/** + * On the Mutation Application form, removes the form action (to prevent + * the default server-side submit) and attaches a custom click handler + * that fetches the declaration PDF, overlays a banner image and date + * stamp on the last page, then triggers a download. + */ document.addEventListener("DOMContentLoaded", () => { observeDOM(() => { const formElement = $("#form_MutationApplication > div > div:nth-child(14) > div.col-sm-2.btreset > form"); diff --git a/src/scripts/functionality/sheet-map/index.ts b/src/scripts/functionality/sheet-map/index.ts index 71228fe..3d566c2 100644 --- a/src/scripts/functionality/sheet-map/index.ts +++ b/src/scripts/functionality/sheet-map/index.ts @@ -23,6 +23,13 @@ let plotNumberLabelTextElements = ""; document.body.appendChild(plotInformation); +/** + * Creates a header toolbar button and appends it to the page header. + * The button starts hidden and is shown when data is ready. + * + * @param text - The button label text. + * @returns The created button element. + */ const createHeaderButton = (text: string) => { const buttonContainer = document.createElement("td"); const button = document.createElement("button"); @@ -33,6 +40,12 @@ const createHeaderButton = (text: string) => { return button; }; +/** + * Displays the area and plot number for the given polygon in a floating + * info panel, or shows "0.000" / empty if no polygon is selected. + * + * @param plotPolygon - The selected plot's data, or `null`/`undefined` to clear. + */ const setPlotInformation = (plotPolygon: PlotPolygon | null | undefined = null) => { plotInformation.innerHTML = plotPolygon ? getPlotInformationElement({ area: (plotPolygon.plotArea/1000).toFixed(3), @@ -43,7 +56,14 @@ const setPlotInformation = (plotPolygon: PlotPolygon | null | undefined = null) }); }; +/** + * Opens a new window with a printable PDF view of the map, optionally + * including plot-number labels. + * + * @param labelPoints - Whether to include plot number text labels. + */ const downloadPDF = (labelPoints: boolean = true) => { + /** Extracts a detail value (district/block/mouza) from the header table by column index. */ const _getMapDetail = (i: number) => { const detail = document.querySelector(`#headerTable > tbody > tr > td:nth-child(${i})`)! .innerHTML @@ -147,6 +167,12 @@ setPlotInformation(); } }, 300); + /** + * Highlights the clicked plot on the SVG map and shows its + * information in the info panel. + * + * @param e - The mouse click event. + */ const handlePlotClick = (e: MouseEvent) => { document.querySelectorAll("path").forEach(e => { e.setAttribute("fill", "#ffcc66"); diff --git a/src/scripts/functionality/view-khatian/index.ts b/src/scripts/functionality/view-khatian/index.ts index 93d03cf..34e6765 100644 --- a/src/scripts/functionality/view-khatian/index.ts +++ b/src/scripts/functionality/view-khatian/index.ts @@ -14,6 +14,12 @@ const styles = Array.from(submitButtonElementComputedStyles) "" ); +/** + * Reads the currently selected text from a `<select>` element. + * + * @param selector - CSS selector for the select element. + * @returns The text of the selected option. + */ const getValueOfSelectElement = (selector: string) => { const element = document.querySelector<HTMLSelectElement>(selector)!; return element.options[element.selectedIndex].text; @@ -21,6 +27,10 @@ const getValueOfSelectElement = (selector: string) => { let isPlotInformation: boolean | null = null; +/** + * Opens a new window with a printable PDF view of the current khatian + * or plot details, including district/block/mouza info. + */ const downloadInformationPDF = () => { generateWebPage( getDownloadInformationPDFPageContent({ @@ -36,6 +46,13 @@ const downloadInformationPDF = () => { ); }; +/** + * Intercepts the success callback of a jQuery AJAX call. When a valid + * details table is returned, it inserts a "Download PDF" button; + * otherwise it removes an existing button. + * + * @param args - The `arguments` object from the intercepted `$.post` call. + */ const showDownloadButton = (args: any) => { if (isPlotInformation !== null) { const callback = args[2]; diff --git a/src/scripts/login/index.ts b/src/scripts/login/index.ts index bed05f9..dcf43e3 100644 --- a/src/scripts/login/index.ts +++ b/src/scripts/login/index.ts @@ -1,18 +1,33 @@ import { interceptPost, interceptGet } from "@/shared/intercept-jquery-ajax"; +/** + * Pre-processes a CAPTCHA image on a canvas by removing grayish + * background noise while preserving dark pixels, producing a clean + * binary image suitable for OCR. + * + * @param ctx - The 2D rendering context of the canvas. + * @param width - Canvas width in pixels. + * @param height - Canvas height in pixels. + */ function prepareCaptcha(ctx: CanvasRenderingContext2D, width: number, height: number) { const imgData = ctx.getImageData(0, 0, width, height); const data = imgData.data; const radius = 1; + /** Returns true when all three channels are below 50 (very dark). */ function isBlack(r: number, g: number, b: number) { return r < 50 && g < 50 && b < 50; } + /** Returns true when the colour is a mid-range grey (no strong hue). */ function isGrayish(r: number, g: number, b: number) { return Math.abs(r - g) < 15 && Math.abs(g - b) < 15 && r > 100 && r < 200; } + /** + * Checks whether a black pixel exists within `radius` pixels of (x, y). + * Used to preserve dark structures when removing grey noise. + */ function hasNearbyBlack(x: number, y: number) { for (let dx = -radius; dx <= radius; dx++) { for (let dy = -radius; dy <= radius; dy++) { diff --git a/src/scripts/stop-blocking.ts b/src/scripts/stop-blocking.ts index b8f4783..3fa3de6 100644 --- a/src/scripts/stop-blocking.ts +++ b/src/scripts/stop-blocking.ts @@ -1,5 +1,12 @@ +/** + * Proxies jQuery's `.bind()` (for the `"cut copy paste"` event) and + * `.keydown()` so that the website's copy/paste/right-click blocking + * is disabled. Reverts the prototypes after patching to avoid + * interfering with other code. + */ document.addEventListener("DOMContentLoaded", () => { const proxiedBind = $.prototype.bind; + /** Proxies jQuery `.bind()` to no-op the `"cut copy paste"` event. */ $.prototype.bind = function() { if (arguments[0].trim() === "cut copy paste") { arguments[1] = (_: any) => {}; @@ -9,6 +16,7 @@ document.addEventListener("DOMContentLoaded", () => { } const proxiedKeydown = $.prototype.keydown; + /** Proxies jQuery `.keydown()` so all key presses are allowed. */ $.prototype.keydown = function() { arguments[0] = (_: any) => true; diff --git a/src/shared/generate-web-page.ts b/src/shared/generate-web-page.ts index a07575f..990f430 100644 --- a/src/shared/generate-web-page.ts +++ b/src/shared/generate-web-page.ts @@ -1,3 +1,10 @@ +/** + * Opens a new browser tab and writes the given HTML content into it. + * Used to display dynamically-generated pages (e.g. PDF-printable views). + * + * @param content - The full HTML string to write. + * @param title - The document title (default: `"Banglar Bhumi"`). + */ export function generateWebPage(content: string, title: string = "Banglar Bhumi") { const tab = window.open("about:blank", "_blank"); if (!tab) { diff --git a/src/shared/intercept-jquery-ajax.ts b/src/shared/intercept-jquery-ajax.ts index a77dd44..7e1f54e 100644 --- a/src/shared/intercept-jquery-ajax.ts +++ b/src/shared/intercept-jquery-ajax.ts @@ -8,10 +8,15 @@ interface InterceptJqueryEntry { const postIntercepts: InterceptJqueryEntry[] = []; const getIntercepts: InterceptJqueryEntry[] = []; +/** + * On DOMContentLoaded, proxies jQuery's `$.post` and `$.get` so that + * registered interceptors are called before the real request. + */ document.addEventListener("DOMContentLoaded", () => { const proxiedPost = $.post; const proxiedGet = $.get; + /** Proxied `$.post` that invokes registered interceptors before the real call. */ $.post = function() { for (const { url, callback } of postIntercepts) { if (arguments[0].endsWith(url)) { @@ -21,6 +26,7 @@ document.addEventListener("DOMContentLoaded", () => { return proxiedPost.apply(this, Array.from(arguments) as any); }; + /** Proxied `$.get` that invokes registered interceptors before the real call. */ $.get = function() { for (const { url, callback } of getIntercepts) { if (arguments[0].endsWith(url)) { @@ -32,10 +38,24 @@ document.addEventListener("DOMContentLoaded", () => { }; }); +/** + * Registers a callback that fires whenever a jQuery POST request matches + * the given URL suffix. + * + * @param url - The URL suffix to match (checked via `endsWith`). + * @param callback - Receives the original `arguments` from `$.post`. + */ export function interceptPost(url: string, callback: InterceptJqueryAjaxCallback) { postIntercepts.push({ url, callback }); }; +/** + * Registers a callback that fires whenever a jQuery GET request matches + * the given URL suffix. + * + * @param url - The URL suffix to match (checked via `endsWith`). + * @param callback - Receives the original `arguments` from `$.get`. + */ export function interceptGet(url: string, callback: InterceptJqueryAjaxCallback) { getIntercepts.push({ url, callback }); }; diff --git a/src/shared/modify-dom.ts b/src/shared/modify-dom.ts index 22b8e8e..b268ae2 100644 --- a/src/shared/modify-dom.ts +++ b/src/shared/modify-dom.ts @@ -10,6 +10,11 @@ type DOMModificationRules = (DOMModificaitonRule | null)[]; let domModificationRules: DOMModificationRules = []; let domModificationFinishedCount = 0; +/** + * Runs whenever the DOM mutates. Iterates over pending modification + * rules; when the selector matches, the element's innerHTML and + * attributes are updated and the rule is marked as complete. + */ observeDOM(() => { for (let i = 0; i < domModificationRules.length; i++) { if (domModificationRules[i]) { @@ -45,6 +50,15 @@ observeDOM(() => { return false; }); +/** + * Registers one or more DOM modification rules. Each rule is a tuple of + * `[selector, attributeMap]`. When an element matching the selector is + * found, its `innerHTML` is replaced (if `attributeMap.innerHTML` is set) + * and all other entries in the map are applied as `setAttribute` calls + * (or `removeAttribute` when the value is `null`). + * + * @param modificationRules - Array of `[selector, attributes]` rules. + */ export function modifyDOM(modificationRules: DOMModificationRules) { domModificationRules = domModificationRules.concat(modificationRules); }; diff --git a/src/shared/observe-dom.ts b/src/shared/observe-dom.ts index e82ce1e..90a52ff 100644 --- a/src/shared/observe-dom.ts +++ b/src/shared/observe-dom.ts @@ -3,6 +3,12 @@ type ObserveDOMCallback = () => boolean; let callbacks: (ObserveDOMCallback | null)[] = []; let callbacksCalledCount = 0; +/** + * Starts a MutationObserver on the document that invokes each + * registered callback. A callback should return `true` when it + * has completed its work; once all callbacks are done the observer + * disconnects. + */ const observer = new MutationObserver(() => { for (let i = 0; i < callbacks.length; i++) { const callback = callbacks[i]; @@ -24,6 +30,14 @@ observer.observe(document, { subtree: true }); +/** + * Registers a callback to run on DOM mutations. + * The callback is invoked on each mutation tick and should return `true` + * when its work is finished. When all callbacks have completed the + * observer stops. + * + * @param callback - Function that returns `true` when done. + */ export function observeDOM(callback: ObserveDOMCallback) { callbacks.push(callback); }; diff --git a/src/shared/script-injector.ts b/src/shared/script-injector.ts index 697c8d6..16cb17c 100644 --- a/src/shared/script-injector.ts +++ b/src/shared/script-injector.ts @@ -2,6 +2,15 @@ type DataType = { [key: string]: string }; +/** + * Injects a `<script>` element into the document head (or `<html>`) with + * the given `src` pointing to an extension resource. Optional key/value + * data is attached as `data-*` attributes so the injected script can + * read them via `document.currentScript`. + * + * @param src - The extension-relative script path. + * @param data - Optional key/value pairs to set as `data-*` attributes. + */ export function injectScriptHead(src: string, data: DataType = {}) { const s = document.createElement("script") as HTMLScriptElement; s.src = chrome.runtime.getURL(src); diff --git a/src/shared/styling.ts b/src/shared/styling.ts index 759fecb..b19ace4 100644 --- a/src/shared/styling.ts +++ b/src/shared/styling.ts @@ -1,3 +1,10 @@ +/** + * Applies a set of inline CSS styles to every element matching the + * given CSS selector. + * + * @param selector - CSS selector string. + * @param styles - A map of CSS property names to values. + */ export function styles(selector: string, styles: Record<string, string>) { const elements = Array.from(document.querySelectorAll(selector)) as HTMLElement[]; |
