module Uzbl.WithSource where
import GHC.IO.Handle (hPutStr, hGetContents, hClose)
import System.Environment (lookupEnv)
import System.Process ( createProcess, proc
, StdStream(CreatePipe), std_out, std_in)
getSource ∷ IO (Maybe String)
getSource = lookupEnv "UZBL_SOCKET" >>= \case
Nothing → return Nothing
Just f →
let sp = (proc "socat" ["-", "unix-connect:\"" ++ f ++ "\""])
{ std_out = CreatePipe, std_in = CreatePipe }
in createProcess sp >>= \case
(Just hin, Just hout, _, _) → do
hPutStr hin "js document.documentElement.outerHTML"
hClose hin
c ← hGetContents hout
length c `seq` hClose hout
return $ Just c
_ → return Nothing