Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // chrome.debugger.getTargets(console.log);
- var time = 0;
- const filter = {
- url: chrome.runtime.getManifest().host_permissions.map((host) => {
- return {
- hostContains: host.replace('https://', '').replace('/*', ''),
- };
- }),
- };
- const navigationStage = async (details, stage) => {
- console.log(details, stage);
- time = performance.now();
- const targets = await chrome.debugger.getTargets();
- const tabId = details.tabId;
- console.log(tabId, targets.length);
- for (let i = 0; i < targets.length; i++) {
- const target = targets[i];
- if (target.tabId === tabId && target.attached === true) {
- return;
- }
- }
- attachDebugger({ tabId });
- };
- chrome.webNavigation.onBeforeNavigate.addListener((details) => {
- navigationStage(details, 'onBeforeNavigate');
- }, filter);
- const enableFetch = (debuggeeId) => {
- chrome.debugger.sendCommand(
- debuggeeId,
- 'Fetch.enable',
- {
- patterns: filter.url.map((host) => {
- return {
- requestStage: 'Response',
- resourceType: 'Document',
- urlPattern: `*${host.hostContains.replace(/\.(?:.(?!\.))+$/, '')}*`,
- };
- }),
- },
- () => {
- console.log('fetch enabled', performance.now() - time);
- }
- );
- };
- const attachDebugger = (debuggeeId) => {
- chrome.debugger.attach(debuggeeId, '1.3', () => {
- console.log('debugger attached', performance.now() - time);
- enableFetch(debuggeeId);
- });
- };
- const continueFetchRequest = (debuggeeId, requestId) => {
- chrome.debugger.sendCommand(
- debuggeeId,
- 'Fetch.continueRequest',
- { requestId: String(requestId) },
- () => {
- }
- );
- };
- const fullfillRequest = (encodedHTML, debuggeeId, frameId) => {
- chrome.debugger.sendCommand(
- debuggeeId,
- 'Fetch.fulfillRequest',
- {
- requestId: frameId.requestId,
- responseCode: frameId.responseStatusCode,
- body: encodedHTML,
- },
- () => {
- }
- );
- };
- const modifyBody = (body, debuggeeId, frameId, requestId) => {
- if (body === undefined) {
- continueFetchRequest(debuggeeId, requestId);
- return;
- }
- let encodedHTML = body.body;
- let decoded = atob(body.body);
- //To allow using Web workers for off-screen canvas painting
- if (frameId.resourceType === 'Document') {
- let finalHTML = decoded.replace(
- `worker-src 'self'`,
- `worker-src 'self' data:`
- );
- encodedHTML = btoa(finalHTML);
- fullfillRequest(encodedHTML, debuggeeId, frameId);
- } else {
- continueFetchRequest(debuggeeId, requestId);
- }
- };
- const getResponseBody = (debuggeeId, frameId, requestId) => {
- chrome.debugger.sendCommand(
- debuggeeId,
- 'Fetch.getResponseBody',
- { requestId: String(requestId) },
- (body) => {
- modifyBody(body, debuggeeId, frameId, requestId);
- }
- );
- };
- const onFetchRequestPaused = (debuggeeId, method, frameId) => {
- console.log('Fetch.requestPaused', performance.now() - time);
- let requestId = frameId.requestId;
- if (
- frameId.resourceType === 'Document'
- ) {
- getResponseBody(debuggeeId, frameId, requestId);
- } else {
- continueFetchRequest(debuggeeId, requestId);
- }
- };
- chrome.debugger.onEvent.addListener((debuggeeId, method, frameId) => {
- if (method === 'Fetch.requestPaused') {
- onFetchRequestPaused(debuggeeId, method, frameId);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment