Tampermonkey và ứng dụng ký số tự động trên HIS

Tampermonkey là gì?

Tampermonkey là một tiện ích mở rộng (extension) miễn phí cho trình duyệt web, cho phép người dùng chạy các đoạn mã JavaScript tùy chỉnh (gọi là user scripts) trên các trang web, giúp tự tay bạn tùy biến và bổ sung các tính năng cần thiết vào trang web. Nói nôm na là nhúng đoạn mã JavaScript tự tạo.

Tampermonkey cũng hỗ trợ update code, khi bạn tạo version code mới thì bạn bè được chia sẻ cũng được update đồng bộ. Nó giống như là bạn tự tạo một extension rút gọn vậy.

Với hỗ trợ của AI thì việc tự tạo đoạn mã JavaScript khá dễ dàng, như đoạn mã bên dưới mình tạo bằng Gemini.

Bạn có thể tìm và tải về các user script từ các trang web như Greasy Fork hoặc OpenUserJS, tuy nhiên cần lưu ý chỉ nên cài đặt user script từ các nguồn uy tín. User script có thể truy cập và thay đổi nội dung của trang web, nên nếu bạn cài đặt một script độc hại, nó có thể đánh cắp thông tin cá nhân của bạn.

Tool ký số tự động trên HIS

Hiện tại HIS chưa tiện dụng, trên phiếu ký có nhiều chân ký số bạn cần ký lần lượt và load rất lâu, vì vậy mình viết đoạn code để tự động hóa việc lặp đi lặp lại tìm và nhấp vào vị trí cần ký rối chờ load rồi lại tìm và ký.
// ==UserScript==
// @name        Tự động ký HIS
// @namespace   http://www.xtea.vn/
// @version     1.1
// @description Tự động ký tờ điều trị và phiếu máu trên HIS
// @author      Xtea
// @icon        https://www.xtea.vn/favicon.ico
// @match       https://his.vn*
// @grant       none
// @run-at      document-start
// @downloadURL https://app.xtea.vn/autosign.user.js
// @updateURL   https://app.xtea.vn/autosign.user.js
// ==/UserScript==

(function() {
    'use strict';

    // Lấy đường dẫn hiện tại của trang
    const currentUrl = window.location.href;

    // --- XỬ LÝ CHO TRƯỜNG HỢP 1: Tờ điều trị EMR_BA077 ---
    if (currentUrl.includes('EMR_BA077')) {

        console.log("Đang thực thi script cho EMR_BA077...");

        // Chức năng 1: Xóa tham số 'lichSuKyId' khỏi URL
        const param1 = '&lichSuKyId=';
        const param2 = '?lichSuKyId=';
        let newUrl = '';

        if (currentUrl.includes(param1)) {
            newUrl = currentUrl.replace(param1, '&');
        } else if (currentUrl.includes(param2)) {
            newUrl = currentUrl.replace(param2, '?');
        }

        if (newUrl && newUrl !== currentUrl) {
            window.location.replace(newUrl);
            return;
        }

        // Chức năng 2: Tìm và ký tự động với tên
        let targetName = '';
        const usernameDiv = document.querySelector('div.username');
        if (usernameDiv) {
            targetName = usernameDiv.textContent.trim();
        } 

        const buttonText = 'Xác nhận ký BÁC SĨ ĐIỀU TRỊ';
        let checkTimer = null;

        function checkAndClickButton(observer) {
            if (!targetName) return;
            console.log("Đang tìm kiếm nút ký cho:", targetName);
            const nameElements = document.querySelectorAll('b');
            let found = false;

            nameElements.forEach(nameElement => {
                if (nameElement.textContent.trim().includes(targetName)) {
                    console.log("Tìm thấy tên bác sĩ:", targetName);
                    const signSpan = nameElement.closest('.sign');
                    if (signSpan) {
                        const confirmButton = signSpan.querySelector('button');
                        if (confirmButton && confirmButton.textContent.trim().includes(buttonText)) {
                            console.log("Tìm thấy nút xác nhận. Đang nhấp.");
                            confirmButton.click();
                            console.log("Đã click vào nút xác nhận.");
                            found = true;
                        }
                    }
                }
            });

            if (found && observer) {
                observer.disconnect();
            }
        }

        const observer = new MutationObserver((mutationsList, obs) => {
            if (checkTimer) {
                clearTimeout(checkTimer);
            }
            checkTimer = setTimeout(() => {
                checkAndClickButton(obs);
            }, 500);
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });

        checkAndClickButton();
    }

    // --- XỬ LÝ CHO TRƯỜNG HỢP 2: Tờ điều trị EMR_BA111 ---
    else if (currentUrl.includes('EMR_BA111')) {

        console.log("Đang thực thi script cho EMR_BA111...");

        const targetButtonText = 'Xác nhận ký BÁC SĨ ĐIỀU TRỊ';
        let checkTimer = null;

        function checkAndClickButton(observer) {
            console.log("Đang tìm kiếm nút ký...");
            const buttons = document.querySelectorAll('button');
            let found = false;

            buttons.forEach(button => {
                if (button.textContent.trim().includes(targetButtonText)) {
                    console.log("Tìm thấy nút ký. Đang nhấp.");
                    button.click();
                    console.log("Đã click vào nút ký.");
                    found = true;
                }
            });

            if (found && observer) {
                observer.disconnect();
            }
        }

        const observer = new MutationObserver((mutationsList, obs) => {
            if (checkTimer) {
                clearTimeout(checkTimer);
            }
            checkTimer = setTimeout(() => {
                checkAndClickButton(obs);
            }, 500);
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });

        checkAndClickButton();
    }

    // Các trường hợp khác có thể thêm vào đây
    // else if (currentUrl.includes('...')) {
    //    ...
    // }

})();

B1. Cài đặt extension Tampermonkey vào trình duyệt

https://chromewebstore.google.com/

Extension này có mặt trên store của hầu hết các trình duyệt


image.png


B2. Cài đặt ký số tự động

https://app.xtea.vn/autosign.user.js


image.png


B3. Ghim lên thanh địa chỉ


image.png


B4. Sử dụng: bật/ tắt tùy chọn “Tự động ký số”


image.png

Khi cần sử dụng chức năng ký tự động thì bật lên
Kiểm tra cập nhật khi cần

Post a Comment

Previous Post Next Post