RheumatologyToolkit
Tìm kiếm
Tạo Danh sách đi buồng + Giao ban
Tạo bảng danh sách
Sao chép
(async function collectPatientData() { const delay = ms => new Promise(r => setTimeout(r, ms)); function getNextButton() { return document.querySelector('a[title^="Next record"]'); } function isButtonDisabled(btn) { if (!btn) return true; return btn.disabled || btn.getAttribute('disabled') === 'disabled' || btn.classList.contains('disableFilter'); } // Lấy giá trị field, ưu tiên "title" (ZK hay cắt ngắn text hiển thị và để full text ở title) function getFieldValue(instanceName) { let el = document.querySelector(`input[instancename="${instanceName}"]`); if (el) return (el.getAttribute('title') || el.value || '').trim(); const wrap = document.querySelector(`div[instancename="${instanceName}"]`); if (wrap) { const input = wrap.querySelector('input'); if (input) return (input.getAttribute('title') || input.value || '').trim(); return (wrap.getAttribute('title') || wrap.textContent || '').trim(); } return ''; } function getFirstCellText() { const combineInfo = document.querySelector('[instancename="HIS_PatientHistory0CombineInfo"]'); if (!combineInfo) return ''; const firstRow = combineInfo.querySelector('tr'); const cell0 = firstRow ? firstRow.querySelector('td') : null; return cell0 ? cell0.textContent.trim() : ''; } // Lấy cột "Chẩn đoán" từ bảng lưới (adtab-grid), dòng highlight // Ưu tiên đọc thuộc tính "title" (đầy đủ) thay vì textContent (có thể bị ZK cắt ngắn thành "...") function getDiagnosisFromGrid() { const grids = Array.from(document.querySelectorAll('.adtab-grid')) .filter(g => g.offsetWidth > 0 && g.offsetHeight > 0); for (const grid of grids) { const headerRow = grid.querySelector('.z-grid-header tr.z-columns'); if (!headerRow) continue; const ths = Array.from(headerRow.querySelectorAll('th.z-column')); let diagIndex = -1; ths.forEach((th, idx) => { const label = th.textContent.replace(/\s+/g, ' ').trim(); if (label === 'Chẩn đoán') diagIndex = idx; }); if (diagIndex === -1) continue; const highlightRow = grid.querySelector('.z-grid-body tr.highlight'); if (!highlightRow) continue; const tds = Array.from(highlightRow.querySelectorAll('td.z-cell')); const td = tds[diagIndex]; if (!td) continue; const span = td.querySelector('span.z-label'); let text = ''; if (span) { text = (span.getAttribute('title') || span.textContent || '').trim(); } else { text = (td.getAttribute('title') || td.textContent || '').trim(); } if (text) return text; } return ''; } function extractPatient() { const combineInfo = document.querySelector('[instancename="HIS_PatientHistory0CombineInfo"]'); let hoTen = '', tuoi = ''; if (combineInfo) { const firstRow = combineInfo.querySelector('tr'); const cells = firstRow ? firstRow.querySelectorAll('td') : []; if (cells[0]) hoTen = cells[0].textContent.replace(/Họ tên:/i, '').trim(); if (cells[1]) { const m = cells[1].textContent.match(/(\d+)\s*Tuổi/); if (m) tuoi = m[1]; } } const giuong = getFieldValue('HIS_PatientHistory0HIS_Bed_ID'); // Ưu tiên lấy từ bảng lưới (dòng highlight, cột "Chẩn đoán") let chanDoan = getDiagnosisFromGrid(); // Fallback: nếu không tìm thấy trong lưới, dùng ô input trực tiếp if (!chanDoan) { chanDoan = getFieldValue('HIS_PatientHistory0Discharge_Diagnostic'); } return { hoTen, tuoi, giuong, chanDoan }; } async function waitForUpdate(prevText, timeout = 8000) { const start = Date.now(); while (Date.now() - start < timeout) { await delay(200); if (getFirstCellText() !== prevText) return true; } return false; } const results = []; const MAX_RECORDS = 500; let count = 0; results.push(extractPatient()); let nextBtn = getNextButton(); while (nextBtn && !isButtonDisabled(nextBtn) && count < MAX_RECORDS) { const prevText = getFirstCellText(); nextBtn.click(); await waitForUpdate(prevText); await delay(400); // đợi grid con load xong (grid nằm sâu hơn nên cần thêm thời gian) results.push(extractPatient()); nextBtn = getNextButton(); count++; } // ---- Vẽ bảng ---- const container = document.createElement('div'); container.id = 'patientDataOverlay'; container.style.cssText = 'position:fixed;top:0;left:0;right:0;bottom:0;background:#fff;z-index:99999;overflow:auto;padding:20px;font-family:Arial,sans-serif;'; let tableHtml = `
STT
Họ tên
Tuổi
Giường
Chẩn đoán
`; results.forEach((r, i) => { tableHtml += `
${i + 1}
${r.hoTen}
${r.tuoi}
${r.giuong}
${r.chanDoan}
`; }); tableHtml += '
'; container.innerHTML = `
Đóng
Copy bảng vào Clipboard
${tableHtml} `; document.body.appendChild(container); document.getElementById('closePatientTable').onclick = () => container.remove(); function tsvText() { const header = ['STT', 'Họ tên', 'Tuổi', 'Giường', 'Chẩn đoán'].join('\t'); const rows = results.map((r, i) => [i + 1, r.hoTen, r.tuoi, r.giuong, r.chanDoan].join('\t')); return [header, ...rows].join('\n'); } function copyViaSelection(node) { try { const range = document.createRange(); range.selectNodeContents(node); const sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); const ok = document.execCommand('copy'); sel.removeAllRanges(); return ok; } catch (e) { return false; } } function copyViaTextarea(text) { try { const ta = document.createElement('textarea'); ta.value = text; ta.style.position = 'fixed'; ta.style.left = '-9999px'; document.body.appendChild(ta); ta.focus(); ta.select(); const ok = document.execCommand('copy'); document.body.removeChild(ta); return ok; } catch (e) { return false; } } document.getElementById('copyPatientTable').onclick = async () => { const tableEl = document.getElementById('patientResultTable'); const statusEl = document.getElementById('copyStatus'); let done = false; if (window.isSecureContext && navigator.clipboard && navigator.clipboard.write) { try { const htmlBlob = new Blob([tableEl.outerHTML], { type: 'text/html' }); const textBlob = new Blob([tsvText()], { type: 'text/plain' }); await navigator.clipboard.write([new ClipboardItem({ 'text/html': htmlBlob, 'text/plain': textBlob })]); done = true; statusEl.style.color = 'green'; statusEl.textContent = '✔ Đã copy (rich table). Dán vào Excel/Word.'; } catch (e) { /* fallback */ } } if (!done && copyViaSelection(tableEl)) { done = true; statusEl.style.color = 'green'; statusEl.textContent = '✔ Đã copy bảng (dạng bảng). Dán vào Excel/Word.'; } if (!done && copyViaTextarea(tsvText())) { done = true; statusEl.style.color = 'green'; statusEl.textContent = '✔ Đã copy (dạng text). Dán vào Excel vẫn tách cột đúng.'; } if (!done) { const range = document.createRange(); range.selectNodeContents(tableEl); const sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); statusEl.style.color = 'red'; statusEl.textContent = 'Trình duyệt chặn copy tự động — bảng đã được bôi đen sẵn, bấm Ctrl+C.'; } }; console.table(results.map((r, i) => ({ STT: i + 1, 'Họ tên': r.hoTen, Tuổi: r.tuoi, Giường: r.giuong, 'Chẩn đoán': r.chanDoan }))); console.log('Hoàn tất. Tổng số bản ghi:', results.length); })();