release
This commit is contained in:
+386
@@ -0,0 +1,386 @@
|
||||
// content.js
|
||||
console.log('[EXT][content] loaded');
|
||||
|
||||
const dataType = 'metaData';
|
||||
const userDataType = 'userData';
|
||||
let isProcessing = false;
|
||||
let storageData = {};
|
||||
|
||||
function injectScript(file) {
|
||||
console.log('[EXT][content] inject:', file);
|
||||
|
||||
const script = document.createElement('script');
|
||||
script.src = chrome.runtime.getURL(file) + '?t=' + Date.now();
|
||||
script.type = 'text/javascript';
|
||||
script.async = false;
|
||||
|
||||
(document.head || document.documentElement).appendChild(script);
|
||||
}
|
||||
|
||||
window.addEventListener('message', (event) => {
|
||||
if (
|
||||
event.source !== window ||
|
||||
!event.data ||
|
||||
event.data.source !== 'medods-extension' ||
|
||||
event.data.type !== dataType &&
|
||||
event.data.type !== userDataType
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[EXT][content] save ${event.data.type}`);
|
||||
|
||||
const saveData = {
|
||||
[event.data.type]: event.data.payload,
|
||||
};
|
||||
|
||||
chrome.storage.local.set(saveData);
|
||||
|
||||
});
|
||||
|
||||
async function loadPageData() {
|
||||
storageData = await chrome.storage.local.get(dataType);
|
||||
|
||||
if (!storageData || !storageData.metaData || Object.keys(storageData.metaData).length === 0) {
|
||||
console.log(`[EXT][content] no data for ${dataType}, injecting...`);
|
||||
injectScript(`${dataType}.js`);
|
||||
} else {
|
||||
console.log(`[EXT][content] metadata already exists in storage`);
|
||||
}
|
||||
}
|
||||
|
||||
function isDocumentsPage() {
|
||||
return (
|
||||
location.href.includes('/clients/') &&
|
||||
location.href.includes('/documents')
|
||||
);
|
||||
}
|
||||
|
||||
function isMedodsPage() {
|
||||
return (
|
||||
document.title === 'MEDODS'
|
||||
)
|
||||
}
|
||||
|
||||
// Обработчик сообщений от singing.js
|
||||
window.addEventListener('message', async (event) => {
|
||||
if (
|
||||
event.source !== window ||
|
||||
!event.data ||
|
||||
event.data.source !== 'medods-extension'
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ОБРАБОТЧИК ПРЕДВАРИТЕЛЬНОЙ ПРОВЕРКИ
|
||||
if (event.data.type === 'preSingingCheck') {
|
||||
// Проверка занятости
|
||||
if (isProcessing) {
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'preSingingCheckResponse',
|
||||
payload: {
|
||||
success: false,
|
||||
error: 'Система занята, попробуйте позже'
|
||||
}
|
||||
}, '*');
|
||||
return;
|
||||
}
|
||||
|
||||
// Получаем данные из storage
|
||||
if (!storageData.metaData) {
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'preSingingCheckResponse',
|
||||
payload: {
|
||||
success: false,
|
||||
error: 'Данные не загружены'
|
||||
}
|
||||
}, '*');
|
||||
return;
|
||||
}
|
||||
|
||||
// Подготавливаем данные для отправки
|
||||
const userData = storageData.metaData.userData;
|
||||
const patientId = storageData.metaData.patients?.[0]?.idPatientMis;
|
||||
|
||||
if (!userData || !patientId) {
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'preSingingCheckResponse',
|
||||
payload: {
|
||||
success: false,
|
||||
error: 'Не удалось получить данные пользователя или пациента'
|
||||
}
|
||||
}, '*');
|
||||
return;
|
||||
}
|
||||
|
||||
const preSingingData = {
|
||||
userId: userData.id,
|
||||
patientId: patientId,
|
||||
docNumbers: event.data.payload.docNumbers
|
||||
};
|
||||
|
||||
// Отправляем запрос в background для предварительной проверки
|
||||
chrome.runtime.sendMessage({
|
||||
action: 'preSingingCheck',
|
||||
data: preSingingData
|
||||
}, (response) => {
|
||||
// Пересылаем результат в singing.js
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'preSingingCheckResponse',
|
||||
payload: response
|
||||
}, '*');
|
||||
});
|
||||
}
|
||||
|
||||
// ОБРАБОТЧИК ОТПРАВКИ ДОКУМЕНТОВ
|
||||
if (event.data.type === 'sendDocuments') {
|
||||
// Проверка занятости
|
||||
if (isProcessing) {
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'sendDocumentsResponse',
|
||||
payload: {
|
||||
response: {
|
||||
success: false,
|
||||
status: 'processing',
|
||||
error: 'Система занята, попробуйте позже'
|
||||
}
|
||||
}
|
||||
}, '*');
|
||||
return;
|
||||
}
|
||||
|
||||
isProcessing = true;
|
||||
|
||||
console.log('[EXT][content] Forwarding to background:', {
|
||||
docNumbers: event.data.payload.docNumbers
|
||||
});
|
||||
|
||||
// Немедленный ответ о принятии задачи
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'sendDocumentsResponse',
|
||||
payload: {
|
||||
response: {
|
||||
success: true,
|
||||
message: 'Документы приняты в обработку',
|
||||
status: 'processing'
|
||||
}
|
||||
}
|
||||
}, '*');
|
||||
|
||||
// Фильтруем документы по номерам
|
||||
const filteredDocs = storageData.metaData.docs.filter(doc =>
|
||||
event.data.payload.docNumbers.includes(parseInt(doc.number, 10))
|
||||
);
|
||||
|
||||
console.log(`📄 [Content] Найдено документов для отправки: ${filteredDocs.length}`);
|
||||
|
||||
if (filteredDocs.length === 0) {
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'sendDocumentsResponse',
|
||||
payload: {
|
||||
response: {
|
||||
success: false,
|
||||
error: 'No matching documents found'
|
||||
}
|
||||
}
|
||||
}, '*');
|
||||
isProcessing = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Собираем все данные для отправки
|
||||
const sendData = {
|
||||
practitioner: storageData.metaData.practitioner,
|
||||
patients: storageData.metaData.patients,
|
||||
docs: filteredDocs,
|
||||
};
|
||||
|
||||
// Асинхронная обработка в background
|
||||
setTimeout(() => {
|
||||
chrome.runtime.sendMessage({
|
||||
action: 'processAndSendDocuments',
|
||||
data: sendData
|
||||
}, (response) => {
|
||||
isProcessing = false;
|
||||
console.log('[EXT][content] Response from background:', response);
|
||||
|
||||
// Пересылаем результат в singing.js
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'backgroundProcessingResult',
|
||||
payload: {
|
||||
success: response?.success || false,
|
||||
message: response.message || '',
|
||||
sentCount: response?.sentCount || 0
|
||||
}
|
||||
}, '*');
|
||||
});
|
||||
}, 0);
|
||||
}
|
||||
|
||||
// В content.js, в основном обработчике window.addEventListener добавьте:
|
||||
|
||||
// ОБРАБОТЧИК ПОДГОТОВКИ ДОКУМЕНТОВ (используем существующие метаданные)
|
||||
if (event.data.type === 'prepareDocuments') {
|
||||
// Проверяем наличие метаданных
|
||||
if (!storageData.metaData) {
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'prepareDocumentsResponse',
|
||||
payload: {
|
||||
success: false,
|
||||
message: 'Метаданные не загружены'
|
||||
}
|
||||
}, '*');
|
||||
return;
|
||||
}
|
||||
|
||||
// Получаем idPatientMis из метаданных
|
||||
const idPatientMis = storageData.metaData.patients?.[0]?.idPatientMis;
|
||||
|
||||
if (!idPatientMis) {
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'prepareDocumentsResponse',
|
||||
payload: {
|
||||
success: false,
|
||||
message: 'Не удалось получить idPatientMis из метаданных'
|
||||
}
|
||||
}, '*');
|
||||
return;
|
||||
}
|
||||
|
||||
// Отправляем запрос в background для получения документов
|
||||
chrome.runtime.sendMessage({
|
||||
action: 'getDocuments',
|
||||
data: {
|
||||
idPatientMis: idPatientMis
|
||||
}
|
||||
}, (response) => {
|
||||
// Пересылаем результат в documents.js
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'prepareDocumentsResponse',
|
||||
payload: response
|
||||
}, '*');
|
||||
});
|
||||
}
|
||||
|
||||
// ОБРАБОТЧИК ПОЛУЧЕНИЯ ДОКУМЕНТА
|
||||
if (event.data.type === 'getDocument') {
|
||||
chrome.runtime.sendMessage({
|
||||
action: 'getDocument',
|
||||
data: event.data.payload
|
||||
}, (response) => {
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'getDocumentResponse',
|
||||
payload: response
|
||||
}, '*');
|
||||
});
|
||||
}
|
||||
|
||||
// ОБРАБОТЧИК ПРОВЕРКИ ПРАВ НА ОТЗЫВ
|
||||
if (event.data.type === 'checkRevokePermission') {
|
||||
const hasPermission = String(storageData.metaData?.userData?.id) === String(event.data.payload.userIdLpu);
|
||||
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'checkRevokePermissionResponse',
|
||||
payload: {
|
||||
hasPermission: hasPermission
|
||||
}
|
||||
}, '*');
|
||||
}
|
||||
|
||||
// ОБРАБОТЧИК ОТЗЫВА ДОКУМЕНТОВ
|
||||
if (event.data.type === 'revokeDocuments') {
|
||||
console.log('[EXT][content] Запрос на отзыв документов:', event.data.payload);
|
||||
|
||||
chrome.runtime.sendMessage({
|
||||
action: 'revokeDocuments',
|
||||
data: event.data.payload
|
||||
}, (response) => {
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'revokeDocumentsResponse',
|
||||
payload: response
|
||||
}, '*');
|
||||
});
|
||||
}
|
||||
|
||||
// ОБРАБОТЧИК ПОВТОРНОЙ ОТПРАВКИ ДОКУМЕНТОВ
|
||||
if (event.data.type === 'resendDocuments') {
|
||||
console.log('[EXT][content] Запрос на повторную отправку документов:', event.data.payload);
|
||||
|
||||
chrome.runtime.sendMessage({
|
||||
action: 'resendDocuments',
|
||||
data: event.data.payload
|
||||
}, (response) => {
|
||||
window.postMessage({
|
||||
source: 'medods-extension',
|
||||
type: 'resendDocumentsResponse',
|
||||
payload: response
|
||||
}, '*');
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// Основной цикл
|
||||
let documentsActive = false;
|
||||
let medodsActive = false;
|
||||
let lastCheck = 0;
|
||||
|
||||
function loop(ts) {
|
||||
if (ts - lastCheck > 200) {
|
||||
lastCheck = ts;
|
||||
|
||||
const now = isDocumentsPage();
|
||||
|
||||
if (now && !documentsActive) {
|
||||
console.log('[EXT][content] ENTER documents');
|
||||
loadPageData()
|
||||
.then(() => {
|
||||
injectScript('documents.js');
|
||||
injectScript('singing.js');
|
||||
documentsActive = true;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('[EXT][content] Error on enter documents:', error);
|
||||
window.location.reload();
|
||||
});
|
||||
}
|
||||
|
||||
if (!now && documentsActive) {
|
||||
console.log('[EXT][content] LEAVE documents');
|
||||
documentsActive = false;
|
||||
isProcessing = false;
|
||||
chrome.storage.local.remove(dataType)
|
||||
.then(() => { })
|
||||
.catch(error => {
|
||||
console.error('[EXT][content] Error on leave documents:', error);
|
||||
window.location.reload();
|
||||
});
|
||||
}
|
||||
|
||||
const medodsNow = isMedodsPage();
|
||||
|
||||
if (medodsNow && !medodsActive) {
|
||||
medodsActive = true;
|
||||
injectScript('userData.js');
|
||||
}
|
||||
}
|
||||
|
||||
requestAnimationFrame(loop);
|
||||
}
|
||||
|
||||
// Запуск
|
||||
loop(0);
|
||||
Reference in New Issue
Block a user