Log-In to a Web Page and Create a WebArchive

Hey @ShaneStanley,

Is there a way to get a webarchive via this method after having logged-in to the page?

Either by POSTing arguments directly OR by scraping Safari or Chrome Cookies?

TIA.

-Chris

------------------------------------------------------------
use framework "Foundation"
use framework "WebKit"
use scripting additions
------------------------------------------------------------
property theSender : missing value
property thePath : missing value
------------------------------------------------------------

set theURL to "https://apple.stackexchange.com/questions/352328/permission-denied-when-running-macos-installer-from-command-line"
set fileName to "“Permission denied” when running macOS installer from command line"
set thePath to (POSIX path of (path to downloads folder)) & fileName & ".webarchive"
its archivePage:theURL toPath:thePath sender:me

------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------
on archivePage:thePageURL toPath:aPath sender:sender
   set my theSender to sender # Store main script so we can call back
   set my thePath to aPath # Store path for use later
   # Make a WebView
   set theView to current application's WebView's alloc()'s initWithFrame:{origin:{x:0, y:0}, |size|:{width:100, height:100}}
   # Tell it call delegate methods on me
   theView's setFrameLoadDelegate:me
   # Load the page
   theView's setMainFrameURL:thePageURL
end archivePage:toPath:sender:
------------------------------------------------------------
# Called when our WebView loads a frame
on WebView:aWebView didFinishLoadForFrame:webFrame
   # The main frame is our interest
   if webFrame = aWebView's mainFrame() then
      # Get the text of the page
      set theText to (webFrame's DOMDocument()'s documentElement()'s outerText())
      # Get the data and write it to file
      set theArchiveData to webFrame's dataSource()'s webArchive()'s |data|()
      theArchiveData's writeToFile:thePath atomically:true
      # Tell our script it's all done
      theSender's jobDone:"The webarchive was saved"
   end if
end WebView:didFinishLoadForFrame:
------------------------------------------------------------
# Called when there's a problem
on WebView:WebView didFailLoadWithError:theError forFrame:webFrame
   # Got an error, bail
   WebView's stopLoading:me
   theSender's jobDone:"The webarchive was not saved"
end WebView:didFailLoadWithError:forFrame:
------------------------------------------------------------
# Called when the job's done
on jobDone:theMessage
   display notification theMessage sound name "Ping"
end jobDone:
------------------------------------------------------------

You can, although I’ve never tried it. I suspect you need to create an NSURLRequest, and then tell the WebView’s mainFrame to loadRequest:. Not trivial.

1 Like