aboutsummaryrefslogtreecommitdiffstats
path: root/ff2mpv.go
diff options
context:
space:
mode:
authorclsr <clsr@clsr.net>2018-12-11 18:24:52 +0100
committerclsr <clsr@clsr.net>2018-12-11 18:31:16 +0100
commitcff36f68517806cd0ae28889d276012003d57012 (patch)
tree78cd0d6cdafdf76790f7e7a6ff882075f6f5bc7d /ff2mpv.go
downloadff2mpv-go-cff36f68517806cd0ae28889d276012003d57012.tar.gz
ff2mpv-go-cff36f68517806cd0ae28889d276012003d57012.zip
Initial commitv1.0.0
Diffstat (limited to 'ff2mpv.go')
-rw-r--r--ff2mpv.go103
1 files changed, 103 insertions, 0 deletions
diff --git a/ff2mpv.go b/ff2mpv.go
new file mode 100644
index 0000000..7f0c939
--- /dev/null
+++ b/ff2mpv.go
@@ -0,0 +1,103 @@
+package main
+
+import (
+ "encoding/binary"
+ "encoding/json"
+ "fmt"
+ "io"
+ "os"
+ "os/exec"
+)
+
+var mpvCmd = []string{"mpv", "--player-operation-mode=pseudo-gui", "--"}
+
+type ffRequest struct {
+ URL string `json:"url"`
+}
+
+type ffManifest struct {
+ Name string `json:"name"`
+ Description string `json:"description"`
+ Path string `json:"path"`
+ Type string `json:"type"`
+ AllowedExtensions []string `json:"allowed_extensions"`
+}
+
+func ff2mpv() {
+ // extract message length from the first 4 bytes
+ var l uint32
+ // Go does not have an easy way to do sutff with native endianness without
+ // using usafe. Blame stupid WebExtension spec that uses *native* endianness in
+ // a *protocol*. Seriously, who designed that‽
+ if err := binary.Read(os.Stdin, endianness, &l); err != nil {
+ panic(err)
+ }
+ if l > 1024*1024 {
+ panic(fmt.Sprintf("native messaging message length too large: %d", l))
+ }
+
+ // read raw message
+ buf := make([]byte, l)
+ if n, err := os.Stdin.Read(buf); err != nil {
+ panic(err)
+ } else if n != len(buf) {
+ panic(fmt.Sprintf("read message length %d does not match expected length %d", n, l))
+ }
+
+ // decode JSON
+ var data ffRequest
+ if err := json.Unmarshal(buf, &data); err != nil {
+ panic(err)
+ }
+
+ // run mpv
+ args := append(mpvCmd, data.URL)
+ cmd := exec.Command(args[0], args[1:]...)
+ if err := cmd.Start(); err != nil {
+ panic(err)
+ }
+
+ // respond
+ outmsg := []byte("{}")
+ outlen := uint32(len(outmsg))
+ binary.Write(os.Stdout, endianness, outlen)
+ os.Stdout.Write(outmsg)
+}
+
+func manifest() {
+ exe, err := os.Executable()
+ if err != nil {
+ panic(err)
+ }
+ data, err := json.MarshalIndent(ffManifest{
+ Name: "ff2mpv",
+ Description: "ff2mpv's extenal manifest (ff2mpv-go)",
+ Path: exe,
+ Type: "stdio",
+ AllowedExtensions: []string{"ff2mpv@yossarian.net"},
+ }, "", "\t")
+ if err != nil {
+ panic(err)
+ }
+ os.Stdout.Write(data)
+}
+
+func usage(w io.Writer) {
+ fmt.Fprintf(os.Stderr, "args: %#v\n", os.Args)
+ fmt.Fprintf(w, "usage: %s [--manifest]\n", os.Args[0])
+}
+
+func main() {
+ arg := ""
+ if len(os.Args) == 2 {
+ arg = os.Args[1]
+ }
+ switch arg {
+ case "--manifest":
+ manifest()
+ case "--help":
+ usage(os.Stdout)
+ default:
+ ff2mpv()
+ }
+}