Skip to main content

Remove my save_safari_webarchive script

ID
43b13ed
date
2024-06-05 18:57:32+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
d18d973
message
Remove my save_safari_webarchive script
changed files
3 files, 1 addition, 131 deletions

Changed files

web/.gitattributes (47) → web/.gitattributes (0)

diff --git a/web/.gitattributes b/web/.gitattributes
deleted file mode 100644
index c226feb..0000000
--- a/web/.gitattributes
+++ /dev/null
@@ -1 +0,0 @@
-save_safari_webarchive linguist-language=Swift

web/README.md (4756) → web/README.md (4339)

diff --git a/web/README.md b/web/README.md
index 0eb9a1c..bdf266a 100644
--- a/web/README.md
+++ b/web/README.md
@@ -54,12 +54,6 @@ scripts = [
         """
     },
     {
-        "usage": "save_safari_webarchive [URL] [OUTPUT_PATH]",
-        "description": """
-        save a copy of a web page as a Safari webarchive
-        """
-    },
-    {
         "name": "scrape_really_useful_boxes.py",
         "description": """
         scrape the Really Useful Boxes product catalogue, so I can search for boxes in ways their website doesn't allow – in particular, by dimensions, so I can find boxes that fit into specific spaces.<br/><br/><img src="really_useful_boxes.png">
@@ -132,15 +126,6 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
   </dd>
 
   <dt>
-    <a href="https://github.com/alexwlchan/scripts/blob/main/web/save_safari_webarchive">
-      <code>save_safari_webarchive [URL] [OUTPUT_PATH]</code>
-    </a>
-  </dt>
-  <dd>
-    save a copy of a web page as a Safari webarchive
-  </dd>
-
-  <dt>
     <a href="https://github.com/alexwlchan/scripts/blob/main/web/scrape_really_useful_boxes.py">
       <code>scrape_really_useful_boxes.py</code>
     </a>
@@ -158,4 +143,4 @@ cog_helpers.create_description_table(folder_name=folder_name, scripts=scripts)
     this is a wrapper around <a href="https://github.com/yt-dlp/yt-dlp">yt-dlp</a> that does parallel downloads of videos in playlists.
   </dd>
 </dl>
-<!-- [[[end]]] (checksum: 7df3c46289e99cda17cd36adb479b0f0) -->
+<!-- [[[end]]] (checksum: 93b152a3a4162f174022195ee107ad46) -->

web/save_safari_webarchive (3217) → web/save_safari_webarchive (0)

diff --git a/web/save_safari_webarchive b/web/save_safari_webarchive
deleted file mode 100755
index 5ca3556..0000000
--- a/web/save_safari_webarchive
+++ /dev/null
@@ -1,114 +0,0 @@
-#!/usr/bin/env swift
-/// Save a web page as a Safari webarchive.
-///
-/// Usage: save_safari_webarchive [URL] [OUTPUT_PATH]
-///
-/// This will save the page to the desired file, but may fail for
-/// several reasons:
-///
-///   - the web page can't be loaded
-///   - the web page returns a non-200 status code
-///   - there's already a file at that path (it won't overwrite an existing
-///     webarchive)
-///
-/// For a detailed explanation of the code in this script, see
-/// https://alexwlchan.net/2024/creating-a-safari-webarchive/
-
-import WebKit
-
-/// Print an error message and terminate the process if there are
-/// any errors while loading a page.
-class ExitOnFailureDelegate: NSObject, WKNavigationDelegate {
-  func webView(_: WKWebView, didFail: WKNavigation!, withError error: Error) {
-    fputs("Failed to load web page: \(error.localizedDescription)\n", stderr)
-    exit(1)
-  }
-
-  func webView(
-    _: WKWebView,
-    didFailProvisionalNavigation: WKNavigation!,
-    withError error: Error
-  ) {
-    fputs("Failed to load web page: \(error.localizedDescription)\n", stderr)
-    exit(1)
-  }
-
-  func webView(
-    _: WKWebView,
-    decidePolicyFor navigationResponse: WKNavigationResponse,
-    decisionHandler: (WKNavigationResponsePolicy) -> Void
-  ) {
-    if let httpUrlResponse = (navigationResponse.response as? HTTPURLResponse) {
-      if httpUrlResponse.statusCode != 200 {
-        fputs("Loading web page failed with status code \(httpUrlResponse.statusCode)\n", stderr)
-        exit(1)
-      }
-    }
-
-    decisionHandler(.allow)
-  }
-}
-
-let webView = WKWebView()
-
-let delegate = ExitOnFailureDelegate()
-webView.navigationDelegate = delegate
-
-extension WKWebView {
-
-  /// Load the given URL in the web view.
-  ///
-  /// This method will block until the URL has finished loading.
-  func load(_ urlString: String) {
-    if let url = URL(string: urlString) {
-      let request = URLRequest(url: url)
-      self.load(request)
-
-      while (self.isLoading) {
-        RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.1))
-      }
-    } else {
-      fputs("Unable to use \(urlString) as a URL\n", stderr)
-      exit(1)
-    }
-  }
-
-  /// Save a copy of the web view's contents as a webarchive file.
-  ///
-  /// This method will block until the webarchive has been saved,
-  /// or the save has failed for some reason.
-  func saveAsWebArchive(savePath: URL) {
-    var isSaving = true
-
-    self.createWebArchiveData(completionHandler: { result in
-      do {
-        let data = try result.get()
-        try data.write(
-          to: savePath,
-          options: [Data.WritingOptions.withoutOverwriting]
-        )
-        isSaving = false
-      } catch {
-        fputs("Unable to save webarchive file: \(error.localizedDescription)\n", stderr)
-        exit(1)
-      }
-    })
-
-    while (isSaving) {
-      RunLoop.main.run(until: Date(timeIntervalSinceNow: 0.1))
-    }
-  }
-}
-
-guard CommandLine.arguments.count == 3 else {
-    print("Usage: \(CommandLine.arguments[0]) <URL> <OUTPUT_PATH>")
-    exit(1)
-}
-
-let urlString = CommandLine.arguments[1]
-let savePath = URL(fileURLWithPath: CommandLine.arguments[2])
-
-webView.load(urlString)
-webView.saveAsWebArchive(savePath: savePath)
-
-print("Saved webarchive to \(savePath)")