How to save web page source/resource files in memory

I am trying to develop a Safari Extension that can save a file from a webpage into persistent storage with minimal user input.

Problem

I don't know how to code it so that it results like manually saving a file from the "Sources" tab in Safari Web Inspector.

Equivalent manual steps

  1. show source or resources of page
    • by keying ⌥⌘U or ⌥⌘A
    • or by navigating to Web Inspector then "Sources"
  2. save the file
    • by selecting the file then by keying ⌘S
    • or by secondary clicking the file then clicking "Save File"

Alternative solution

Javascript get the URL of files from the HTML elements and then pass the URL to a downloader, but I don't want to download it after the browser already did. I want to save the already downloaded temporary files to reduce network trafficking.

Theory

From my current understanding, when a webpage is viewed, Safari downloads the files and loads them in memory.

Other

I tried multiple ways to Google it but I keep getting only unrelated results.

Thank you for your time to read this and in advance.

How to save web page source/resource files in memory
 
 
Q