about summary refs log tree commit diff stats
path: root/modules/home.legacy/conf/firefox/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'modules/home.legacy/conf/firefox/scripts')
-rw-r--r--modules/home.legacy/conf/firefox/scripts/default.nix29
-rwxr-xr-xmodules/home.legacy/conf/firefox/scripts/extract_cookies.sh77
-rwxr-xr-xmodules/home.legacy/conf/firefox/scripts/unzip_mozlz4.py44
-rwxr-xr-xmodules/home.legacy/conf/firefox/scripts/update_extensions.sh18
4 files changed, 168 insertions, 0 deletions
diff --git a/modules/home.legacy/conf/firefox/scripts/default.nix b/modules/home.legacy/conf/firefox/scripts/default.nix
new file mode 100644
index 00000000..1127662b
--- /dev/null
+++ b/modules/home.legacy/conf/firefox/scripts/default.nix
@@ -0,0 +1,29 @@
+{
+  pkgs,
+  sysLib,
+  ...
+}: let
+  unzip_mozlz4 = pkgs.stdenv.mkDerivation {
+    name = "unzip_mozlz4";
+    propagatedBuildInputs = [
+      (pkgs.python3.withPackages (pythonPackages:
+        with pythonPackages; [
+          lz4
+        ]))
+    ];
+    dontUnpack = true;
+    installPhase = "install -Dm755 ${./unzip_mozlz4.py} $out/bin/unzip_mozlz4";
+  };
+  extract_cookies = sysLib.writeShellScript {
+    name = "extract_cookies";
+    src = ./extract_cookies.sh;
+    dependencies = with pkgs; [
+      bash
+      sqlite
+      mktemp
+      coreutils
+    ];
+  };
+in {
+  inherit unzip_mozlz4 extract_cookies;
+}
diff --git a/modules/home.legacy/conf/firefox/scripts/extract_cookies.sh b/modules/home.legacy/conf/firefox/scripts/extract_cookies.sh
new file mode 100755
index 00000000..e3d50d43
--- /dev/null
+++ b/modules/home.legacy/conf/firefox/scripts/extract_cookies.sh
@@ -0,0 +1,77 @@
+#!/usr/bin/env bash
+# copied from https://superuser.com/a/1239036
+# extract_cookies.sh:
+#
+# Convert from Firefox's cookies.sqlite format to Netscape cookies,
+# which can then be used by wget and curl. (Why don't wget and curl
+# just use libsqlite if it's installed? Mysteries abound.)
+#
+# Note: This script reads directly from the standard cookie jar file,
+# which means cookies which are kept only in memory ("session cookies")
+# will not be extracted. You will need an extension to do that.
+
+# USAGE:
+#
+# $ extract_cookies.sh > /tmp/cookies.txt
+# or
+# $ extract_cookies.sh ~/.mozilla/firefox/*default*/cookies.sqlite > /tmp/cookies.txt
+
+# USING WITH WGET:
+# $ wget --load-cookies=/tmp/cookies.txt http://example.com
+
+# USING WITH CURL:
+# $ curl --cookie /tmp/cookies.txt http://example.com
+
+# Note: If you do not specify an SQLite filename, this script will
+# intelligently find it for you.
+#
+# A) Usually it will check all profiles under ~/.mozilla/firefox/ and
+# use the cookies.sqlite that was updated most recently.
+#
+# B) If you've redirected stdin (with < or |) , then that will be used.
+
+# HISTORY: I believe this is circa 2010 from:
+# http://slacy.com/blog/2010/02/using-cookies-sqlite-in-wget-or-curl/
+# However, that site is down now.
+
+# Cleaned up by Hackerb9 (2017) to be more robust and require less typing.
+
+cleanup() {
+    rm -f "$TMPFILE"
+    exit 0
+}
+trap cleanup EXIT INT QUIT TERM
+
+if [ "$#" -ge 1 ]; then
+    SQLFILE="$1"
+else
+    SQLFILE="$HOME/.mozilla/firefox/default/cookies.sqlite"
+fi
+
+if ! [ -r "$SQLFILE" ]; then
+    echo "Error. File $SQLFILE is not readable." >&2
+    exit 1
+fi
+
+# We have to copy cookies.sqlite, because FireFox has a lock on it
+TMPFILE=$(mktemp /tmp/cookies.sqlite.XXXXXXXXXX)
+cat "$SQLFILE" >>"$TMPFILE"
+
+# This is the format of the sqlite database:
+# CREATE TABLE moz_cookies (id INTEGER PRIMARY KEY, name TEXT, value TEXT, host TEXT, path TEXT,expiry INTEGER, lastAccessed INTEGER, isSecure INTEGER, isHttpOnly INTEGER);
+
+echo "# Netscape HTTP Cookie File"
+sqlite3 -separator $'\t' "$TMPFILE" <<EOF
+.mode tabs
+.header off
+select host,
+case substr(host,1,1)='.' when 0 then 'FALSE' else 'TRUE' end,
+path,
+case isSecure when 0 then 'FALSE' else 'TRUE' end,
+expiry,
+name,
+value
+from moz_cookies;
+EOF
+
+cleanup
diff --git a/modules/home.legacy/conf/firefox/scripts/unzip_mozlz4.py b/modules/home.legacy/conf/firefox/scripts/unzip_mozlz4.py
new file mode 100755
index 00000000..9a2348bf
--- /dev/null
+++ b/modules/home.legacy/conf/firefox/scripts/unzip_mozlz4.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+# source: https://unix.stackexchange.com/a/497861
+# Command-line tool to decompress mozLz4 files used for example by Firefox to store various kinds of session backup information.
+# Works in both Python 2.7.15 and 3.6.7, as of version 2.1.6 of the LZ4 Python bindings at pypi.org/project/lz4.
+# To use in another script, simply cut and paste the import statement and the mozlz4_to_text() function (lines 8 to 17).
+
+import lz4.block  # pip install lz4 --user
+
+
+def mozlz4_to_text(filepath):
+    # Given the path to a "mozlz4", "jsonlz4", "baklz4" etc. file,
+    # return the uncompressed text.
+    bytestream = open(filepath, "rb")
+    bytestream.read(8)  # skip past the b"mozLz40\0" header
+    valid_bytes = bytestream.read()
+    text = lz4.block.decompress(valid_bytes)
+    return text
+
+
+def main(args):
+    # Given command-line arguments of an input filepath for a ".mozlz4" file
+    # and optionally an output filepath, write the decompressed text to the
+    # output filepath.
+    # Default output filepath is the input filepath minus the last three characters
+    # (e.g. "foo.jsonlz4" becomes "foo.json")
+    filepath_in = args[0]
+    if len(args) < 2:
+        filepath_out = filepath_in[:-3]
+    else:
+        filepath_out = args[1]
+    text = mozlz4_to_text(filepath_in)
+    with open(filepath_out, "wb") as outfile:
+        outfile.write(text)
+    print("Wrote decompressed text to {}".format(filepath_out))
+
+
+if __name__ == "__main__":
+    import sys
+
+    args = sys.argv[1:]
+    if args and args[0] not in ("--help", "-h"):
+        main(args)
+    else:
+        print("Usage: mozlz4.py <mozlz4 file to read> <location to write>")
diff --git a/modules/home.legacy/conf/firefox/scripts/update_extensions.sh b/modules/home.legacy/conf/firefox/scripts/update_extensions.sh
new file mode 100755
index 00000000..86bd843c
--- /dev/null
+++ b/modules/home.legacy/conf/firefox/scripts/update_extensions.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+tmp=$(mktemp)
+cat <<EOF | awk '!/^\s*#/' >"$tmp"
+    darkreader:navbar
+    keepassxc-browser:navbar
+    vhack-libredirect:navbar
+    torproject-snowflake:navbar
+    tridactyl-vim:menupanel
+    ublock-origin:menupanel
+EOF
+
+# The bin is provided in the devshell;
+# The cat execution should be unquoted;
+# shellcheck disable=SC2046
+generate_extensions $(cat "$tmp") >"$(dirname "$0")"/../config/extensions/extensions.json
+
+rm "$tmp"