Pop up PDF EMR nếu load thất bại
(() => {
console.log('📡 PDF auto-open helper active...');
const sleep = ms => new Promise(res => setTimeout(res, ms));
async function tryOpenPdf(url) {
for (let i = 0; i < 3; i++) {
await sleep(1500); // chờ 1.5s giữa các lần thử
try {
const r = await fetch(url);
if (r.ok && r.headers.get('content-type')?.includes('pdf')) {
console.log('✅ PDF reloaded successfully, opening popup:', url);
window.open(url, '_blank');
return true;
}
} catch (e) {
console.warn('retry error', e);
}
}
console.error('❌ still failed after retries:', url);
return false;
}
const _fetch = window.fetch;
window.fetch = async (...args) => {
const url = typeof args[0] === 'string' ? args[0] : args[0].url;
const res = await _fetch(...args);
if (!res.ok && url.includes('/api/file/v1/files/upload/') && url.endsWith('.pdf')) {
console.warn('⚠️ PDF load failed, trying popup reload:', url);
tryOpenPdf(url);
}
return res;
};
// cũng bắt luôn XHR (axios)
const _open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (method, url, ...rest) {
this._url = url;
return _open.call(this, method, url, ...rest);
};
const _send = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function (...args) {
this.addEventListener('load', function () {
if (this.status === 404 && this._url.includes('/api/file/v1/files/upload/') && this._url.endsWith('.pdf')) {
console.warn('⚠️ PDF XHR 404, trying popup reload:', this._url);
tryOpenPdf(this._url);
}
});
return _send.apply(this, args);
};
})();