0

This question is similar to this one: Is it possible to download using the Windows command line?, but with the small difference that, since the browser is the only program allowed to access the internet, it is the only option available. As far as I understand, I would say that the answer is "no way". I tried something like:

"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"  http://website.com/remotepicture.jpg > ./localfile.jpg

but, unsurprisingly, I got just an empty file.

Then I tried using a local html file, but I got to another dead end related to CORS: still getting an empty file.

<!DOCTYPE html>
<html>
  <head>
    <META HTTP-EQUIV="Refresh" CONTENT="600">
  </head>
<body>
  <script>
  function forceDownload(blob, filename) {
    var a = document.createElement('a');
    a.download = filename;
    a.href = blob;
    document.body.appendChild(a);
    a.click();
    a.remove();
   }
   // Current blob size limit is around 500MB for browsers
   function downloadResource(url, filename) {
     if (!filename) filename = url.split('\\').pop().split('/').pop();
     fetch(url, {
       headers: new Headers({
         'Origin': location.origin
       }),
       mode: 'no-cors'
     })
     .then(response => response.blob())
     .then(blob => {
       let blobUrl = window.URL.createObjectURL(blob);
       forceDownload(blobUrl, filename);
     })
     .catch(e => console.error(e));
   }
   downloadResource('http://www.klevischer-verein.de/webcam/schwanenturm.jpg');
  </script>
</body>
</html>
  • 1
    No, it’s not. However, depending on how the browser-only restriction is enforced, it may be possible to work around that. So how is it enforced? – Daniel B Jul 08 '20 at 13:51
  • No idea... nothing besides the browser sees the internet – andreaconsole Jul 08 '20 at 14:02
  • You’ll have to be a little more thorough in your investigation. Try different things and provide the resulting error messages in your question. – Daniel B Jul 08 '20 at 14:10
  • Yes, of course it's possible - in fact, it's required for the internet to work. Anything and everything you see in the browser is first stored locally in the browser cache. I suspect that what you really want is a way to use common browsers to download from specific URLs to predetermined filenames. This is possible with some browsers. And can always be triggered using a context menu on a site or by running a program that is included in Windows which are granted network access by default, such as curl or PS. Please confirm my assumptions by editing the question before I propose a solution. – shawn Jul 08 '20 at 15:02
  • Check the browser cache for the file. – DrMoishe Pippik Jul 08 '20 at 19:37
  • Thank you for the tips. Unfortunately, both using curl and using the cache are not viable solutions, but I like the discussion. Let's say I'm not looking for workarounds but just trying to understand if and how to use the browser for my purposes. Now I'm thinking about a local html file that uses javascript to save the picture to a personal folder. Does it make sense to you? – andreaconsole Jul 09 '20 at 11:34
  • Probably a no-go, but [Selenium WebDriver](https://www.selenium.dev/projects/) has bindings for a number of languages and Edge has [WebDriver files](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/) available. Not a "just Windows" solution, but if you can download anything to the PC (or get approval to do so), this could be an option. – Anaksunaman Jul 14 '20 at 10:14

1 Answers1

1

At this point, I think there's no solution in the way I wanted it (directly through the browser). So, I thank everyone for the tips and in particular DrMoishe Pippik for suggesting me to look into the browser cache. In the end, I wrote a short VBScript file to find the picture I'm looking for based on its characteristics (file size and age, and picture size). It is not very reliable, but it seems effective. Then I used a (pretty straightforward) local html file to cache the resource every n minutes. Here is the vbs code for future reference:

On Error Resume Next
sOrigin = "C:\Users\xxxxxxxxx\AppData\Local\Microsoft\Edge\User Data\Default\Cache"           'Path to check for files
repeattime = 10                                                                                     'check again every n minutes
maxage = 5
minsize = 50                                                                                       'minimum file size in kB
maxsize = 150                                                                                       'maximum file size in kB
width = 640   
height = 480                                                                               


Set oFileSys = WScript.CreateObject("Scripting.FileSystemObject")
Set objImage = WScript.CreateObject("WIA.ImageFile")
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run("cmd.exe /C peek.html")   'launch the browser
Set oFolder = oFileSys.GetFolder(sOrigin)

lastcheck = Now
Do
    For Each oFile In oFolder.Files
        'only files not older than [maxage] minutes
        If (DateDiff("n", oFile.DateLastModified,lastcheck) < maxage) Then  ' Or use DateCreated, if you wish
            'only files between the limits
            If (oFile.Size > minsize*1024 And oFile.Size < maxsize*1024) Then
                'check if it is an image
                oFileSys.CopyFile oFile.Path, ".\pictures\image.jpg", True
                objImage.LoadFile ".\pictures\image.jpg"
                If (Err.Number <> 0 Or objImage.Width <> width Or objImage.Height <> height) Then
                    oFileSys.DeleteFile ".\pictures\image.jpg"
                    Err.Clear
                Else
                    Exit For
                End If
             End If
        End If
    Next
  
    lastcheck = Now
    WScript.Sleep 1000*60*repeattime
Loop