diff --git a/config.json b/config.json index bebbdef8..c4244c39 100644 --- a/config.json +++ b/config.json @@ -21,7 +21,8 @@ "video", "loading", "fade", - "mobile" + "mobile", + "extension" ], "scripts": { "main": "main" diff --git a/layout/layout.go b/layout/layout.go index 9e617c67..b963046f 100644 --- a/layout/layout.go +++ b/layout/layout.go @@ -9,5 +9,5 @@ import ( // Render layout. func Render(ctx *aero.Context, content string) string { user := utils.GetUser(ctx) - return components.Layout(ctx.App, user, content) + return components.Layout(ctx.App, ctx, user, content) } diff --git a/layout/layout.pixy b/layout/layout.pixy index 00629b93..86007cdb 100644 --- a/layout/layout.pixy +++ b/layout/layout.pixy @@ -1,4 +1,4 @@ -component Layout(app *aero.Application, user *arn.User, content string) +component Layout(app *aero.Application, ctx *aero.Context, user *arn.User, content string) html(lang="en") head title= app.Config.Title @@ -6,7 +6,7 @@ component Layout(app *aero.Application, user *arn.User, content string) meta(name="theme-color", content=app.Config.Manifest.ThemeColor) link(rel="manifest", href="/manifest.json") body - #container + #container(class=utils.GetContainerClass(ctx)) #header Navigation(user) #content-container diff --git a/main.go b/main.go index edc47ead..16c31cf2 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "github.com/animenotifier/notify.moe/pages/animelist" "github.com/animenotifier/notify.moe/pages/animelistitem" "github.com/animenotifier/notify.moe/pages/dashboard" + "github.com/animenotifier/notify.moe/pages/embed" "github.com/animenotifier/notify.moe/pages/forum" "github.com/animenotifier/notify.moe/pages/forums" "github.com/animenotifier/notify.moe/pages/login" @@ -70,6 +71,7 @@ func configure(app *aero.Application) *aero.Application { app.Ajax("/login", login.Get) app.Ajax("/airing", airing.Get) app.Ajax("/webdev", webdev.Get) + app.Ajax("/extension/embed", embed.Get) // app.Ajax("/genres", genres.Get) // app.Ajax("/genres/:name", genre.Get) diff --git a/pages/animelist/animelist.go b/pages/animelist/animelist.go index 0982543f..d06f7227 100644 --- a/pages/animelist/animelist.go +++ b/pages/animelist/animelist.go @@ -21,7 +21,7 @@ func Get(ctx *aero.Context) string { animeList := viewUser.AnimeList() if animeList == nil { - return ctx.Error(http.StatusNotFound, "Anime list not found", err) + return ctx.Error(http.StatusNotFound, "Anime list not found", nil) } sort.Slice(animeList.Items, func(i, j int) bool { diff --git a/pages/embed/embed.go b/pages/embed/embed.go new file mode 100644 index 00000000..8c486ce2 --- /dev/null +++ b/pages/embed/embed.go @@ -0,0 +1,31 @@ +package embed + +import ( + "net/http" + "sort" + + "github.com/aerogo/aero" + "github.com/animenotifier/notify.moe/components" + "github.com/animenotifier/notify.moe/utils" +) + +// Get anime list in the browser extension. +func Get(ctx *aero.Context) string { + user := utils.GetUser(ctx) + + if user == nil { + return ctx.Error(http.StatusUnauthorized, "Not logged in", nil) + } + + animeList := user.AnimeList() + + if animeList == nil { + return ctx.Error(http.StatusNotFound, "Anime list not found", nil) + } + + sort.Slice(animeList.Items, func(i, j int) bool { + return animeList.Items[i].FinalRating() < animeList.Items[j].FinalRating() + }) + + return utils.AllowEmbed(ctx, ctx.HTML(components.AnimeList(animeList))) +} diff --git a/pages/settings/settings.go b/pages/settings/settings.go index 978866bb..761cc9a5 100644 --- a/pages/settings/settings.go +++ b/pages/settings/settings.go @@ -16,5 +16,5 @@ func Get(ctx *aero.Context) string { return ctx.Error(http.StatusForbidden, "Not logged in", nil) } - return ctx.HTML(components.Settings(user)) + return utils.AllowEmbed(ctx, ctx.HTML(components.Settings(user))) } diff --git a/styles/extension.scarlet b/styles/extension.scarlet new file mode 100644 index 00000000..d22d4624 --- /dev/null +++ b/styles/extension.scarlet @@ -0,0 +1,10 @@ +.embedded + .anime-list + th + display none + + #content + // font-size 0.9em + + #header + display none \ No newline at end of file diff --git a/utils/allowembed.go b/utils/allowembed.go new file mode 100644 index 00000000..16761b8c --- /dev/null +++ b/utils/allowembed.go @@ -0,0 +1,10 @@ +package utils + +import "github.com/aerogo/aero" + +// AllowEmbed allows the page to be called by the browser extension. +func AllowEmbed(ctx *aero.Context, response string) string { + // This is a bit of a hack. + ctx.SetResponseHeader("X-Frame-Options", "ALLOW-FROM chrome-extension://hjfcooigdelogjmniiahfiilcefdlpha/options.html") + return response +} diff --git a/utils/container.go b/utils/container.go new file mode 100644 index 00000000..33343873 --- /dev/null +++ b/utils/container.go @@ -0,0 +1,15 @@ +package utils + +import ( + "github.com/aerogo/aero" +) + +// GetContainerClass returns the class for the "container" element. +// In the browser extension it will get the "embedded" class. +func GetContainerClass(ctx *aero.Context) string { + if ctx.URI() == "/extension/embed" { + return "embedded" + } + + return "" +}