Advertisement
13ooeo

Untitled

Mar 12th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. // Load the har-export-trigger library (you can include it in your HTML)
  6. // Example: <script src="https://cdn.jsdelivr.net/npm/har-export-trigger@1.0.0/dist/har-export-trigger.min.js"></script>
  7.  
  8. // Function to export HAR data
  9. function exportHar() {
  10. // Capture performance entries
  11. const entries = performance.getEntriesByType('resource');
  12.  
  13. // Create a HAR object
  14. const har = {
  15. log: {
  16. version: '1.2',
  17. creator: {
  18. name: 'My HAR Exporter',
  19. version: '1.0',
  20. },
  21. entries: entries.map(entry => ({
  22. startedDateTime: new Date(entry.startTime).toISOString(),
  23. time: entry.duration,
  24. request: {
  25. method: entry.initiatorType,
  26. url: entry.name,
  27. },
  28. response: {
  29. status: entry.transferSize > 0 ? 200 : 404,
  30. content: {
  31. mimeType: entry.initiatorType === 'img' ? 'image/png' : 'text/html',
  32. },
  33. },
  34. })),
  35. },
  36. };
  37.  
  38. // Convert to JSON
  39. const harJson = JSON.stringify(har, null, 2);
  40.  
  41. // Save as a file (you can modify this part based on your needs)
  42. const blob = new Blob([harJson], { type: 'application/json' });
  43. const url = URL.createObjectURL(blob);
  44. const a = document.createElement('a');
  45. a.href = url;
  46. a.download = 'Executor';
  47. a.click();
  48. }
  49.  
  50. // Call the exportHar function (e.g., via a button click)
  51. exportHar();
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement