mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-06 00:47:09 +00:00
refactor(display): use chrome devtools protocol (#38)
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package browser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/chromedp/chromedp"
|
||||
)
|
||||
|
||||
var Browser BrowserType = BrowserType{}
|
||||
|
||||
type BrowserType struct {
|
||||
Ctx context.Context
|
||||
Cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (b *BrowserType) Init() error {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to determine user home directory: %w", err)
|
||||
}
|
||||
browserProfileDirPath := filepath.Join(home, ".local", "share", "plg-mudics", "browser-display")
|
||||
if err := os.MkdirAll(browserProfileDirPath, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("failed to create local config directory: %w", err)
|
||||
}
|
||||
|
||||
opts := []chromedp.ExecAllocatorOption{
|
||||
chromedp.Flag("headless", false),
|
||||
chromedp.Flag("app", "http://example.com"), // app mode prevents a few unwanted features of chrome, the start url is directly overwritten
|
||||
chromedp.Flag("start-fullscreen", true),
|
||||
chromedp.Flag("hide-scrollbars", true),
|
||||
chromedp.Flag("allow-file-access-from-files", true),
|
||||
chromedp.Flag("user-data-dir", browserProfileDirPath),
|
||||
chromedp.Flag("autoplay-policy", "no-user-gesture-required"),
|
||||
}
|
||||
|
||||
initCtx, _ := chromedp.NewExecAllocator(context.Background(), opts...)
|
||||
b.Ctx, b.Cancel = chromedp.NewContext(initCtx)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *BrowserType) OpenPage(url string) {
|
||||
chromedp.Run(b.Ctx, chromedp.Navigate(url))
|
||||
}
|
||||
|
||||
// Yes, we need that trick with creating a temp file and not directly sending html since
|
||||
// chrome only allows us to access local files via other local files
|
||||
func (b *BrowserType) OpenHTML(html string) error {
|
||||
var err error
|
||||
|
||||
tempFile, err := os.CreateTemp("", "mudics-*.html")
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not create tempfile: %w", err)
|
||||
}
|
||||
defer tempFile.Close()
|
||||
|
||||
_, err = tempFile.WriteString(html)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not write to tempfile: %w", err)
|
||||
}
|
||||
|
||||
chromedp.Run(b.Ctx, chromedp.Navigate("file://"+tempFile.Name()))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *BrowserType) OpenPDF(path string) {
|
||||
b.OpenPage("file://" + path + "#toolbar=0&view=Fit")
|
||||
}
|
||||
Reference in New Issue
Block a user