first commit
This commit is contained in:
commit
fcc2125e49
12 files changed
+918
No files matched your search
+201
@@ -0,0 +1,201 @@
|
||||
'use strict';
|
||||
|
||||
// Manifest V3: chrome.webRequest pozwala ODCZYTYWAĆ odpowiedzi; niczego nie zmieniamy.
|
||||
// Nagłówki przechowujemy jako tablicę, NIE jako mapę {nazwa: wartość}.
|
||||
// Dwa pola "Vary" pozostają dwoma osobnymi rekordami.
|
||||
const FILTER = { urls: ['http://*/*', 'https://*/*'] };
|
||||
const MAX_REQUESTS_PER_TAB = 75;
|
||||
const MAX_VALUE_LENGTH = 16384;
|
||||
const MAX_URL_LENGTH = 4096;
|
||||
const pendingByTab = new Map();
|
||||
|
||||
function storageKey(tabId) {
|
||||
return `headers_tab_${tabId}`;
|
||||
}
|
||||
|
||||
function newState() {
|
||||
return { nextSeq: 1, navigationAt: 0, entries: [] };
|
||||
}
|
||||
|
||||
function safeString(value, limit = MAX_VALUE_LENGTH) {
|
||||
const string = String(value ?? '');
|
||||
return string.length > limit ? `${string.slice(0, limit)} … [wartość ucięta]` : string;
|
||||
}
|
||||
|
||||
function copyHeaders(headers) {
|
||||
if (!Array.isArray(headers)) return [];
|
||||
return headers.map(header => {
|
||||
let value;
|
||||
if (typeof header.value === 'string') {
|
||||
value = safeString(header.value);
|
||||
} else if (Array.isArray(header.binaryValue)) {
|
||||
value = 'hex: ' + header.binaryValue.map(byte => byte.toString(16).padStart(2, '0')).join(' ');
|
||||
value = safeString(value);
|
||||
} else {
|
||||
value = '';
|
||||
}
|
||||
return { name: safeString(header.name, 256), value };
|
||||
});
|
||||
}
|
||||
|
||||
// Kolejka per karta zapobiega utracie zdarzeń przy równoczesnym odczycie/zapisie
|
||||
// asynchronicznego chrome.storage.session. Nie synchronizujemy danych do chmury.
|
||||
function updateTab(tabId, change) {
|
||||
if (!Number.isInteger(tabId) || tabId < 0) return;
|
||||
const key = storageKey(tabId);
|
||||
const previous = pendingByTab.get(tabId) || Promise.resolve();
|
||||
const pending = previous.catch(() => {}).then(async () => {
|
||||
const saved = await chrome.storage.session.get(key);
|
||||
const state = saved[key] || newState();
|
||||
change(state);
|
||||
// Chronimy wpis dokumentu strony: przy dużej liczbie obrazów, skryptów
|
||||
// i żądań XHR nie może wypaść z historii przed otwarciem popupu.
|
||||
while (state.entries.length > MAX_REQUESTS_PER_TAB) {
|
||||
let removable = state.entries.length - 1;
|
||||
for (let i = state.entries.length - 1; i >= 0; i--) {
|
||||
if (state.entries[i].type !== 'main_frame') {
|
||||
removable = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
state.entries.splice(removable, 1);
|
||||
}
|
||||
await chrome.storage.session.set({ [key]: state });
|
||||
});
|
||||
pendingByTab.set(tabId, pending);
|
||||
pending.finally(() => {
|
||||
if (pendingByTab.get(tabId) === pending) pendingByTab.delete(tabId);
|
||||
}).catch(() => {});
|
||||
return pending;
|
||||
}
|
||||
|
||||
function currentEntry(state, details) {
|
||||
return state.entries.find(entry =>
|
||||
entry.requestId === details.requestId &&
|
||||
entry.phase !== 'redirected' &&
|
||||
entry.phase !== 'completed' &&
|
||||
entry.phase !== 'error'
|
||||
);
|
||||
}
|
||||
|
||||
function createEntry(state, details) {
|
||||
const entry = {
|
||||
id: String(state.nextSeq++),
|
||||
requestId: details.requestId,
|
||||
method: details.method || 'GET',
|
||||
url: safeString(details.url, MAX_URL_LENGTH),
|
||||
type: details.type || 'other',
|
||||
frameId: details.frameId ?? -1,
|
||||
timeStamp: details.timeStamp || Date.now(),
|
||||
requestHeaders: [],
|
||||
responseHeaders: [],
|
||||
statusCode: null,
|
||||
statusLine: '',
|
||||
phase: 'pending',
|
||||
redirectUrl: '',
|
||||
error: ''
|
||||
};
|
||||
state.entries.unshift(entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
function entryForEvent(state, details) {
|
||||
return currentEntry(state, details) || createEntry(state, details);
|
||||
}
|
||||
|
||||
function acceptEvent(state, details) {
|
||||
// Odrzuć spóźnione zdarzenia poprzedniej strony po rozpoczęciu nowej nawigacji.
|
||||
return !state.navigationAt || details.timeStamp >= state.navigationAt;
|
||||
}
|
||||
|
||||
chrome.webRequest.onBeforeRequest.addListener(details => {
|
||||
updateTab(details.tabId, state => {
|
||||
const previousHop = currentEntry(state, details);
|
||||
if (details.type === 'main_frame' && !previousHop &&
|
||||
!state.entries.some(entry => entry.requestId === details.requestId && entry.phase === 'redirected')) {
|
||||
state.entries = [];
|
||||
state.navigationAt = details.timeStamp;
|
||||
}
|
||||
if (!acceptEvent(state, details)) return;
|
||||
// Nowe wywołanie oznacza nową próbę / etap przekierowania.
|
||||
createEntry(state, details);
|
||||
});
|
||||
}, FILTER);
|
||||
|
||||
// onSendHeaders, a nie onBeforeSendHeaders: najbliższy ostatecznemu zestaw
|
||||
// nagłówków wysyłanych przez Chrome; extraHeaders ujawnia m.in. Cookie.
|
||||
chrome.webRequest.onSendHeaders.addListener(details => {
|
||||
updateTab(details.tabId, state => {
|
||||
if (!acceptEvent(state, details)) return;
|
||||
const entry = entryForEvent(state, details);
|
||||
entry.requestHeaders = copyHeaders(details.requestHeaders);
|
||||
});
|
||||
}, FILTER, ['requestHeaders', 'extraHeaders']);
|
||||
|
||||
chrome.webRequest.onHeadersReceived.addListener(details => {
|
||||
updateTab(details.tabId, state => {
|
||||
if (!acceptEvent(state, details)) return;
|
||||
const entry = entryForEvent(state, details);
|
||||
entry.statusCode = details.statusCode ?? null;
|
||||
entry.statusLine = safeString(details.statusLine || '', 200);
|
||||
entry.responseHeaders = copyHeaders(details.responseHeaders);
|
||||
entry.phase = 'headers';
|
||||
});
|
||||
}, FILTER, ['responseHeaders', 'extraHeaders']);
|
||||
|
||||
chrome.webRequest.onBeforeRedirect.addListener(details => {
|
||||
updateTab(details.tabId, state => {
|
||||
if (!acceptEvent(state, details)) return;
|
||||
const entry = entryForEvent(state, details);
|
||||
entry.statusCode = details.statusCode ?? entry.statusCode;
|
||||
entry.statusLine = safeString(details.statusLine || entry.statusLine, 200);
|
||||
if (Array.isArray(details.responseHeaders) && !entry.responseHeaders.length) {
|
||||
entry.responseHeaders = copyHeaders(details.responseHeaders);
|
||||
}
|
||||
entry.redirectUrl = safeString(details.redirectUrl, MAX_URL_LENGTH);
|
||||
entry.phase = 'redirected';
|
||||
});
|
||||
}, FILTER, ['responseHeaders', 'extraHeaders']);
|
||||
|
||||
chrome.webRequest.onCompleted.addListener(details => {
|
||||
updateTab(details.tabId, state => {
|
||||
if (!acceptEvent(state, details)) return;
|
||||
const entry = currentEntry(state, details);
|
||||
if (entry) {
|
||||
entry.statusCode = details.statusCode ?? entry.statusCode;
|
||||
entry.phase = 'completed';
|
||||
}
|
||||
});
|
||||
}, FILTER);
|
||||
|
||||
chrome.webRequest.onErrorOccurred.addListener(details => {
|
||||
updateTab(details.tabId, state => {
|
||||
if (!acceptEvent(state, details)) return;
|
||||
const entry = currentEntry(state, details);
|
||||
if (entry) {
|
||||
entry.error = safeString(details.error, 300);
|
||||
entry.phase = 'error';
|
||||
}
|
||||
});
|
||||
}, FILTER);
|
||||
|
||||
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
if (!message || message.action !== 'clearTab') return;
|
||||
const tabId = message.tabId;
|
||||
if (!Number.isInteger(tabId) || tabId < 0) {
|
||||
sendResponse({ ok: false });
|
||||
return;
|
||||
}
|
||||
// Nie ufaj tabId z dowolnej strony: wiadomości od treści stron nie są
|
||||
// obsługiwane (brak content scripts i onMessageExternal).
|
||||
updateTab(tabId, state => {
|
||||
state.entries = [];
|
||||
state.nextSeq = 1;
|
||||
}).then(() => sendResponse({ ok: true })).catch(() => sendResponse({ ok: false }));
|
||||
return true;
|
||||
});
|
||||
|
||||
chrome.tabs.onRemoved.addListener(tabId => {
|
||||
const previous = pendingByTab.get(tabId) || Promise.resolve();
|
||||
previous.catch(() => {}).then(() => chrome.storage.session.remove(storageKey(tabId))).catch(() => {});
|
||||
});
|
||||
Reference in new issue
Block a user