v0.2
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
if (window.__takeid_injected__) return;
|
||||
window.__takeid_injected__ = true;
|
||||
|
||||
const TARGET_SUBSTR = 'getDataViewList';
|
||||
const LOG_PREFIX = '[TakeID]';
|
||||
|
||||
function extractValue(field) {
|
||||
if (field === null || field === undefined) return null;
|
||||
if (typeof field === 'string') return field;
|
||||
if (typeof field === 'number' || typeof field === 'boolean') return String(field);
|
||||
if (typeof field === 'object') {
|
||||
if (Object.prototype.hasOwnProperty.call(field, 'value')) {
|
||||
return extractValue(field.value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function parseAndPost(responseText) {
|
||||
if (!responseText || typeof responseText !== 'string') return;
|
||||
let data;
|
||||
try {
|
||||
data = JSON.parse(responseText);
|
||||
} catch (e) {
|
||||
return;
|
||||
}
|
||||
if (!data || !Array.isArray(data.datos)) return;
|
||||
|
||||
const items = data.datos
|
||||
.map((row, index) => {
|
||||
if (!row || typeof row !== 'object') return null;
|
||||
const id = extractValue(row.id);
|
||||
const code = extractValue(row.code);
|
||||
return { index, id, code };
|
||||
})
|
||||
.filter((item) => item && item.id);
|
||||
|
||||
console.log(LOG_PREFIX, 'Posting', items.length, 'items to content script');
|
||||
window.postMessage(
|
||||
{
|
||||
type: 'TAKEID_DATA',
|
||||
items: items,
|
||||
timestamp: Date.now()
|
||||
},
|
||||
'*'
|
||||
);
|
||||
}
|
||||
|
||||
// Verifie si l'URL OU le body contient getDataViewList
|
||||
function isTargetRequest(url, body) {
|
||||
if (url && url.indexOf(TARGET_SUBSTR) !== -1) return true;
|
||||
if (body && typeof body === 'string' && body.indexOf(TARGET_SUBSTR) !== -1) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// --- Monkey-patch XMLHttpRequest ---
|
||||
const originalOpen = XMLHttpRequest.prototype.open;
|
||||
const originalSend = XMLHttpRequest.prototype.send;
|
||||
|
||||
XMLHttpRequest.prototype.open = function (method, url) {
|
||||
try {
|
||||
this.__takeid_url = typeof url === 'string' ? url : (url && url.toString ? url.toString() : '');
|
||||
} catch (e) {
|
||||
this.__takeid_url = '';
|
||||
}
|
||||
return originalOpen.apply(this, arguments);
|
||||
};
|
||||
|
||||
XMLHttpRequest.prototype.send = function (body) {
|
||||
try {
|
||||
// Convertir le body en string pour la recherche
|
||||
let bodyStr = '';
|
||||
if (body) {
|
||||
if (typeof body === 'string') {
|
||||
bodyStr = body;
|
||||
} else if (body instanceof FormData) {
|
||||
// FormData: on ne peut pas facilement chercher dedans, skip
|
||||
bodyStr = '';
|
||||
} else {
|
||||
try { bodyStr = JSON.stringify(body); } catch (e) { bodyStr = ''; }
|
||||
}
|
||||
}
|
||||
|
||||
if (isTargetRequest(this.__takeid_url, bodyStr)) {
|
||||
console.log(LOG_PREFIX, 'Intercepted getDataViewList - URL:', this.__takeid_url);
|
||||
this.addEventListener('load', function () {
|
||||
try {
|
||||
let text = null;
|
||||
if (this.responseType === '' || this.responseType === 'text') {
|
||||
text = this.responseText;
|
||||
} else if (this.responseType === 'json' && this.response) {
|
||||
try {
|
||||
text = JSON.stringify(this.response);
|
||||
} catch (e) {
|
||||
text = null;
|
||||
}
|
||||
}
|
||||
if (text) {
|
||||
console.log(LOG_PREFIX, 'Response received, length:', text.length);
|
||||
parseAndPost(text);
|
||||
} else {
|
||||
console.warn(LOG_PREFIX, 'Could not read response text, responseType:', this.responseType);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn(LOG_PREFIX, 'Error reading response:', e);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// swallow
|
||||
}
|
||||
return originalSend.apply(this, arguments);
|
||||
};
|
||||
|
||||
// --- Monkey-patch fetch (au cas ou SmartUI l'utilise) ---
|
||||
if (typeof window.fetch === 'function') {
|
||||
const originalFetch = window.fetch;
|
||||
window.fetch = function (input, init) {
|
||||
let url = '';
|
||||
try {
|
||||
if (typeof input === 'string') {
|
||||
url = input;
|
||||
} else if (input && typeof input.url === 'string') {
|
||||
url = input.url;
|
||||
}
|
||||
} catch (e) {
|
||||
url = '';
|
||||
}
|
||||
|
||||
const promise = originalFetch.apply(this, arguments);
|
||||
|
||||
if (url && url.indexOf(TARGET_SUBSTR) !== -1) {
|
||||
promise
|
||||
.then((response) => {
|
||||
try {
|
||||
const clone = response.clone();
|
||||
clone
|
||||
.text()
|
||||
.then((text) => parseAndPost(text))
|
||||
.catch(() => {});
|
||||
} catch (e) {
|
||||
// swallow
|
||||
}
|
||||
return response;
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
|
||||
return promise;
|
||||
};
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user