2017-09-28 22:16:34 +00:00
|
|
|
/*
|
|
|
|
*
|
2022-02-21 03:32:53 +00:00
|
|
|
* OttoJS Plugin Module
|
|
|
|
* Copyright Azareal 2016 - 2019
|
2017-09-28 22:16:34 +00:00
|
|
|
*
|
|
|
|
*/
|
2017-11-11 04:06:16 +00:00
|
|
|
package common
|
2017-09-28 22:16:34 +00:00
|
|
|
|
|
|
|
import (
|
2022-02-21 03:32:53 +00:00
|
|
|
"errors"
|
2017-09-28 22:16:34 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
"github.com/robertkrimen/otto"
|
2017-09-28 22:16:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OttoPluginLang struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
vm *otto.Otto
|
|
|
|
plugins map[string]*otto.Script
|
|
|
|
vars map[string]*otto.Object
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2022-02-21 03:32:53 +00:00
|
|
|
pluginLangs["ottojs"] = &OttoPluginLang{
|
|
|
|
plugins: make(map[string]*otto.Script),
|
|
|
|
vars: make(map[string]*otto.Object),
|
|
|
|
}
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (js *OttoPluginLang) Init() (err error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
js.vm = otto.New()
|
|
|
|
js.vars["current_page"], err = js.vm.Object(`var current_page = {}`)
|
|
|
|
return err
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (js *OttoPluginLang) GetName() string {
|
2022-02-21 03:32:53 +00:00
|
|
|
return "ottojs"
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (js *OttoPluginLang) GetExts() []string {
|
2022-02-21 03:32:53 +00:00
|
|
|
return []string{".js"}
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (js *OttoPluginLang) AddPlugin(meta PluginMeta) (plugin *Plugin, err error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
script, err := js.vm.Compile("./extend/"+meta.UName+"/"+meta.Main, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pluginInit = func(plugin *Plugin) error {
|
|
|
|
retValue, err := js.vm.Run(script)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if retValue.IsString() {
|
|
|
|
ret, err := retValue.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ret != "" {
|
|
|
|
return errors.New(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin = new(Plugin)
|
|
|
|
plugin.UName = meta.UName
|
|
|
|
plugin.Name = meta.Name
|
|
|
|
plugin.Author = meta.Author
|
|
|
|
plugin.URL = meta.URL
|
|
|
|
plugin.Settings = meta.Settings
|
|
|
|
plugin.Tag = meta.Tag
|
|
|
|
plugin.Type = "ottojs"
|
|
|
|
plugin.Init = pluginInit
|
|
|
|
|
|
|
|
// TODO: Implement plugin life cycle events
|
|
|
|
|
|
|
|
buildPlugin(plugin)
|
|
|
|
|
|
|
|
plugin.Data = script
|
|
|
|
return plugin, nil
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*func (js *OttoPluginLang) addHook(hook string, plugin string) {
|
2022-02-21 03:32:53 +00:00
|
|
|
hooks[hook] = func(data interface{}) interface{} {
|
|
|
|
switch d := data.(type) {
|
|
|
|
case Page:
|
|
|
|
currentPage := js.vars["current_page"]
|
|
|
|
currentPage.Set("Title", d.Title)
|
|
|
|
case TopicPage:
|
2017-09-28 22:16:34 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
case ProfilePage:
|
2017-09-28 22:16:34 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
case Reply:
|
2017-09-28 22:16:34 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
default:
|
|
|
|
log.Print("Not a valid JS datatype")
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 22:16:34 +00:00
|
|
|
}*/
|