From 9b12bd24c068238a949a5bf8f3e84acab09c7021 Mon Sep 17 00:00:00 2001 From: Colin Date: Sun, 11 Aug 2024 22:17:46 +0000 Subject: [PATCH] WIP: firefox: add a right-click context menu to open any URI with the system handler --- TODO.md | 6 +++ hosts/common/programs/default.nix | 1 + hosts/common/programs/firefox-xdg-open.nix | 13 +++++ hosts/common/programs/firefox.nix | 5 ++ .../additional/firefox-extensions/default.nix | 6 +-- .../firefox-xdg-open/background.html | 16 +++++++ .../firefox-xdg-open/background.js | 22 +++++++++ .../firefox-xdg-open/default.nix | 47 +++++++++++++++++++ .../firefox-xdg-open/manifest.json | 24 ++++++++++ .../firefox-xdg-open/xdg-open-scheme-handler | 9 ++++ 10 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 hosts/common/programs/firefox-xdg-open.nix create mode 100644 pkgs/additional/firefox-extensions/firefox-xdg-open/background.html create mode 100644 pkgs/additional/firefox-extensions/firefox-xdg-open/background.js create mode 100644 pkgs/additional/firefox-extensions/firefox-xdg-open/default.nix create mode 100644 pkgs/additional/firefox-extensions/firefox-xdg-open/manifest.json create mode 100755 pkgs/additional/firefox-extensions/firefox-xdg-open/xdg-open-scheme-handler diff --git a/TODO.md b/TODO.md index 3a40512d8..f825e0769 100644 --- a/TODO.md +++ b/TODO.md @@ -9,6 +9,7 @@ - trust-dns can't resolve `social.kernel.org` - trust-dns can't resolve `pe.usps.com` - trust-dns can't resolve `social.seattle.wa.us` +- trust-dns can't resolve `support.mozilla.org` - sandbox: link cache means that if i update ~/.config/... files inline, sandboxed programs still see the old version - mpv: continues to play past the end of some audio files - mpv: audiocast has mpv sending its output to the builtin speakers unless manually changed @@ -61,6 +62,11 @@ - safer (rust? actively maintained? sandboxable?) - handles spaces/symbols in filenames - has better multi-stream perf (e.g. `sane-sync-music` should be able to copy N items in parallel) +- firefox: open *all* links (http, https, ...) with system handler + - removes the need for open-in-mpv, firefox-xdg-open, etc. + - matrix room links *just work*. + - `network.protocol-handler.external.https = true` in about:config *seems* to do this, + but breaks some webpages (e.g. Pleroma) ### security/resilience - enable `snapper` btrfs snapshots (`services.snapper`) diff --git a/hosts/common/programs/default.nix b/hosts/common/programs/default.nix index 3e797c0f6..feb1e5059 100644 --- a/hosts/common/programs/default.nix +++ b/hosts/common/programs/default.nix @@ -46,6 +46,7 @@ ./fcitx5.nix ./feedbackd.nix ./firefox.nix + ./firefox-xdg-open.nix ./flare-signal.nix ./foliate.nix ./fontconfig.nix diff --git a/hosts/common/programs/firefox-xdg-open.nix b/hosts/common/programs/firefox-xdg-open.nix new file mode 100644 index 000000000..bb7d332b3 --- /dev/null +++ b/hosts/common/programs/firefox-xdg-open.nix @@ -0,0 +1,13 @@ +{ pkgs, ... }: +{ + sane.programs.firefox-xdg-open = { + packageUnwrapped = pkgs.firefox-extensions.firefox-xdg-open.systemComponent; + + sandbox.method = "bwrap"; + sandbox.whitelistDbus = [ "user" ]; # for xdg-open/portals + + mime.associations."x-scheme-handler/xdg-open" = "xdg-open.desktop"; + + suggestedPrograms = [ "xdg-utils" ]; + }; +} diff --git a/hosts/common/programs/firefox.nix b/hosts/common/programs/firefox.nix index 678eda070..64672efbf 100644 --- a/hosts/common/programs/firefox.nix +++ b/hosts/common/programs/firefox.nix @@ -231,6 +231,10 @@ in package = pkgs.firefox-extensions.ether-metamask; enable = lib.mkDefault false; # until i can disable the first-run notification }; + firefox-xdg-open = { + package = pkgs.firefox-extensions.firefox-xdg-open; + enable = lib.mkDefault true; + }; i2p-in-private-browsing = { package = pkgs.firefox-extensions.i2p-in-private-browsing; enable = lib.mkDefault config.services.i2p.enable; @@ -298,6 +302,7 @@ in fs.".config/sops".dir = lib.mkIf cfg.addons.browserpass-extension.enable {}; #< needs to be created, not *just* added to the sandbox suggestedPrograms = [ + "firefox-xdg-open" "open-in-mpv" ]; diff --git a/pkgs/additional/firefox-extensions/default.nix b/pkgs/additional/firefox-extensions/default.nix index e9abf5a9f..d52f487c4 100644 --- a/pkgs/additional/firefox-extensions/default.nix +++ b/pkgs/additional/firefox-extensions/default.nix @@ -34,10 +34,7 @@ let extid = addon.passthru.extid; # merge our requirements into the derivation args args' = args // { - passthru = { - inherit extid; - original = addon; - } // (args.passthru or {}); + passthru = addon.passthru // (args.passthru or {}); nativeBuildInputs = [ jq strip-nondeterminism @@ -137,6 +134,7 @@ in (lib.makeScope newScope (self: with self; { browserpass-extension = callPackage ./browserpass-extension { }; bypass-paywalls-clean = callPackage ./bypass-paywalls-clean { }; ctrl-shift-c-should-copy = callPackage ./ctrl-shift-c-should-copy { }; + firefox-xdg-open = callPackage ./firefox-xdg-open { }; i-still-dont-care-about-cookies = callPackage ./i-still-dont-care-about-cookies { }; open-in-mpv = callPackage ./open-in-mpv { }; sidebery = callPackage ./sidebery { }; diff --git a/pkgs/additional/firefox-extensions/firefox-xdg-open/background.html b/pkgs/additional/firefox-extensions/firefox-xdg-open/background.html new file mode 100644 index 000000000..eacdbf676 --- /dev/null +++ b/pkgs/additional/firefox-extensions/firefox-xdg-open/background.html @@ -0,0 +1,16 @@ + + + + + + + + Document + + + + + + + + diff --git a/pkgs/additional/firefox-extensions/firefox-xdg-open/background.js b/pkgs/additional/firefox-extensions/firefox-xdg-open/background.js new file mode 100644 index 000000000..701b4aff5 --- /dev/null +++ b/pkgs/additional/firefox-extensions/firefox-xdg-open/background.js @@ -0,0 +1,22 @@ +//! largely copied from OpenInMPV browser extension + +function xdgOpen(tabId, url) { + const code = ` + var link = document.createElement('a') + link.href='xdg-open:${url}' + document.body.appendChild(link) + link.click()` + console.log(code) + chrome.tabs.executeScript(tabId, { code }) +} + +[["page", "pageUrl"], ["link", "linkUrl"], ["video", "srcUrl"], ["audio", "srcUrl"]].forEach(([item, linkType]) => { + chrome.contextMenus.create({ + title: "xdg-open", + id: `open${item}inmpv`, + contexts: [item], + onclick: (info, tab) => { + xdgOpen(tab.id, info[linkType]); + }, + }); +}); diff --git a/pkgs/additional/firefox-extensions/firefox-xdg-open/default.nix b/pkgs/additional/firefox-extensions/firefox-xdg-open/default.nix new file mode 100644 index 000000000..5a1298d0c --- /dev/null +++ b/pkgs/additional/firefox-extensions/firefox-xdg-open/default.nix @@ -0,0 +1,47 @@ +{ + copyDesktopItems, + makeDesktopItem, + static-nix-shell, + stdenvNoCC, + zip, +}: +stdenvNoCC.mkDerivation { + pname = "firefox-xdg-open"; + version = "0.1"; + src = ./.; + + nativeBuildInputs = [ zip ]; + + buildPhase = '' + runHook preBuild + zip -j firefox.zip \ + background.html background.js manifest.json + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + install firefox.zip $out + runHook postInstall + ''; + + passthru.extid = "@firefox-xdg-open"; + passthru.systemComponent = static-nix-shell.mkBash { + pname = "xdg-open-scheme-handler"; + src = ./.; + pkgs = [ "xdg-utils" ]; + + nativeBuildInputs = [ + copyDesktopItems + ]; + desktopItems = [ + (makeDesktopItem { + name = "xdg-open"; + exec = "xdg-open-scheme-handler %U"; + desktopName = "xdg-open"; + comment = "Decodes xdg-open:... URIs, used to force applications to open links via the system handler"; + noDisplay = true; + }) + ]; + }; +} diff --git a/pkgs/additional/firefox-extensions/firefox-xdg-open/manifest.json b/pkgs/additional/firefox-extensions/firefox-xdg-open/manifest.json new file mode 100644 index 000000000..3ad0c1f6f --- /dev/null +++ b/pkgs/additional/firefox-extensions/firefox-xdg-open/manifest.json @@ -0,0 +1,24 @@ +{ + "manifest_version": 2, + "browser_specific_settings": { + "gecko": { + "id": "@firefox-xdg-open" + } + }, + "name": "Firefox XDG Open", + "description": "Open URIs with the system handler.", + "version": "0.1.0", + "options_ui": {}, + "background": { + "page": "background.html" + }, + "browser_action": { + "default_title": "xdg-open" + }, + "permissions": [ + "tabs", + "activeTab", + "contextMenus" + ] +} + diff --git a/pkgs/additional/firefox-extensions/firefox-xdg-open/xdg-open-scheme-handler b/pkgs/additional/firefox-extensions/firefox-xdg-open/xdg-open-scheme-handler new file mode 100755 index 000000000..7ebdd3582 --- /dev/null +++ b/pkgs/additional/firefox-extensions/firefox-xdg-open/xdg-open-scheme-handler @@ -0,0 +1,9 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i bash -p bash -p xdg-utils + +uris=() +for u in "$@"; do + uris+=("${u/xdg-open:/}") +done + +xdg-open "${uris[@]}"