简体   繁体   中英

How do I download the object that chrome.devtools.network.getHAR returns as a .har?

I am using chrome.devtools.network.getHAR to get the har log from a site. However, I can't figure out how to convert it into a proper .har file and download it. I can use JSON.stringify(harLog) , convert to a BLOB, download the file, and view it as JSON, but I want to be able to download the file and be able to open the .har file with a .har viewer(like chrome network tab) from the downloads folder.

Here is the code that I have thus far:

chrome.devtools.network.getHAR(
        function (harLog) {

            let harBLOB = new Blob([harLog]);

            let url = URL.createObjectURL(harBLOB);

            chrome.downloads.download({
                url: url,
                filename: "test.har"
            });
        });

With that code, it downloads a text file with the following content;

[object Object]

Solution

After viewing a .har file generated from chrome dev tools I noticed that it followed the following format

{
  "log": {
    "version": "1.2",
    "creator": {
      ...
    },
  ...
}

whereas JSON.stringify(harLog) would give me

{
  "version": "1.2",
    "creator": {
      ...
    },
  ...
}

After I assigned the harLog object to the key: log of an object and stringified it, I was able to download import the file into chrome dev tools network tab.

Working code

chrome.devtools.network.getHAR(
        function (harLog) {
            let updatedHarLog = {};
            updatedHarLog.log = harLog;

            let harBLOB = new Blob([JSON.stringify(updatedHarLog)]);

            let url = URL.createObjectURL(harBLOB);

            chrome.downloads.download({
                url: url,
                filename: "test"
            });
        }
    );

First of all, thank you wOxxOm for guiding me in the right direction with JSON.stringify()

Solution

After viewing a .har file generated from chrome dev tools I noticed that it followed the following format

{
  "log": {
    "version": "1.2",
    "creator": {
      ...
    },
  ...
}

whereas JSON.stringify(harLog) would give me

{
  "version": "1.2",
    "creator": {
      ...
    },
  ...
}

After I assigned the harLog object to the key: log of an object and stringified it, I was able to download import the file into chrome dev tools network tab.

Working code

chrome.devtools.network.getHAR(
        function (harLog) {
            let updatedHarLog = {};
            updatedHarLog.log = harLog;

            let harBLOB = new Blob([JSON.stringify(updatedHarLog)]);

            let url = URL.createObjectURL(harBLOB);

            chrome.downloads.download({
                url: url,
                filename: "test"
            });
        }
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM