sentero-esp12

Untitled

Dec 18th, 2021
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  // chrome.debugger.getTargets(console.log);
  2. var time = 0;
  3.  
  4. const filter = {
  5.   url: chrome.runtime.getManifest().host_permissions.map((host) => {
  6.     return {
  7.       hostContains: host.replace('https://', '').replace('/*', ''),
  8.     };
  9.   }),
  10. };
  11.  
  12. const navigationStage = async (details, stage) => {
  13.   console.log(details, stage);
  14.   time = performance.now();
  15.   const targets = await chrome.debugger.getTargets();
  16.   const tabId = details.tabId;
  17.   console.log(tabId, targets.length);
  18.   for (let i = 0; i < targets.length; i++) {
  19.     const target = targets[i];
  20.     if (target.tabId === tabId && target.attached === true) {
  21.       return;
  22.     }
  23.   }
  24.   attachDebugger({ tabId });
  25. };
  26.  
  27. chrome.webNavigation.onBeforeNavigate.addListener((details) => {
  28.   navigationStage(details, 'onBeforeNavigate');
  29. }, filter);
  30.  
  31. const enableFetch = (debuggeeId) => {
  32.   chrome.debugger.sendCommand(
  33.     debuggeeId,
  34.     'Fetch.enable',
  35.     {
  36.       patterns: filter.url.map((host) => {
  37.         return {
  38.           requestStage: 'Response',
  39.           resourceType: 'Document',
  40.           urlPattern: `*${host.hostContains.replace(/\.(?:.(?!\.))+$/, '')}*`,
  41.         };
  42.       }),
  43.     },
  44.     () => {
  45.       console.log('fetch enabled', performance.now() - time);
  46.     }
  47.   );
  48. };
  49.  
  50. const attachDebugger = (debuggeeId) => {
  51.   chrome.debugger.attach(debuggeeId, '1.3', () => {
  52.     console.log('debugger attached', performance.now() - time);
  53.     enableFetch(debuggeeId);
  54.   });
  55. };
  56.  
  57. const continueFetchRequest = (debuggeeId, requestId) => {
  58.   chrome.debugger.sendCommand(
  59.     debuggeeId,
  60.     'Fetch.continueRequest',
  61.     { requestId: String(requestId) },
  62.     () => {
  63.      
  64.     }
  65.   );
  66. };
  67.  
  68. const fullfillRequest = (encodedHTML, debuggeeId, frameId) => {
  69.   chrome.debugger.sendCommand(
  70.     debuggeeId,
  71.     'Fetch.fulfillRequest',
  72.     {
  73.       requestId: frameId.requestId,
  74.       responseCode: frameId.responseStatusCode,
  75.       body: encodedHTML,
  76.     },
  77.     () => {
  78.      
  79.     }
  80.   );
  81. };
  82.  
  83. const modifyBody = (body, debuggeeId, frameId, requestId) => {
  84.   if (body === undefined) {
  85.     continueFetchRequest(debuggeeId, requestId);
  86.     return;
  87.   }
  88.   let encodedHTML = body.body;
  89.   let decoded = atob(body.body);
  90.  
  91.   //To allow using Web workers for off-screen canvas painting
  92.   if (frameId.resourceType === 'Document') {
  93.     let finalHTML = decoded.replace(
  94.       `worker-src 'self'`,
  95.       `worker-src 'self' data:`
  96.     );
  97.     encodedHTML = btoa(finalHTML);
  98.     fullfillRequest(encodedHTML, debuggeeId, frameId);
  99.   } else {
  100.     continueFetchRequest(debuggeeId, requestId);
  101.   }
  102. };
  103.  
  104. const getResponseBody = (debuggeeId, frameId, requestId) => {
  105.   chrome.debugger.sendCommand(
  106.     debuggeeId,
  107.     'Fetch.getResponseBody',
  108.     { requestId: String(requestId) },
  109.     (body) => {
  110.       modifyBody(body, debuggeeId, frameId, requestId);
  111.     }
  112.   );
  113. };
  114.  
  115. const onFetchRequestPaused = (debuggeeId, method, frameId) => {
  116.   console.log('Fetch.requestPaused', performance.now() - time);
  117.   let requestId = frameId.requestId;
  118.   if (
  119.     frameId.resourceType === 'Document'
  120.   ) {
  121.     getResponseBody(debuggeeId, frameId, requestId);
  122.   } else {
  123.     continueFetchRequest(debuggeeId, requestId);
  124.   }
  125. };
  126.  
  127. chrome.debugger.onEvent.addListener((debuggeeId, method, frameId) => {
  128.   if (method === 'Fetch.requestPaused') {
  129.     onFetchRequestPaused(debuggeeId, method, frameId);
  130.   }
  131. });
Advertisement
Add Comment
Please, Sign In to add comment