Skip to content

Commit

Permalink
Merge branch 'beta'
Browse files Browse the repository at this point in the history
  • Loading branch information
gentee committed Oct 1, 2021
2 parents 7f6ec95 + caf05b5 commit 0a2d22e
Show file tree
Hide file tree
Showing 9 changed files with 1,095 additions and 95 deletions.
1,024 changes: 932 additions & 92 deletions assets.go

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions browserext.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"net/http"
"strconv"
"strings"
"sync"
"time"

"github.com/labstack/echo/v4"
)
Expand Down Expand Up @@ -60,6 +62,23 @@ type ExtInfo struct {
Url string `json:"url"`
}

type ExtTask struct {
TaskId uint32 `json:"taskid"`
}

type FillFormResponse struct {
List []es.ExtFill `json:"list,omitempty"`
Finished bool `json:"finished"`
Error string `json:"error,omitempty"`
}

const ExtQueueTimeLimit = 5

var (
extMutex = sync.Mutex{}
extQueue = make([]*es.ExtForm, 0)
)

func browserExtHandle(c echo.Context) error {
var (
err error
Expand Down Expand Up @@ -229,3 +248,66 @@ func removeBrowserHandle(c echo.Context) error {
}
return browsersResponse(c)
}

func extQueueHandle(c echo.Context) error {
var (
extForm es.ExtForm
err error
)
if err = c.Bind(&extForm); err != nil {
return jsonError(c, err)
}
extMutex.Lock()
defer extMutex.Unlock()

var added bool
for i := 0; i < len(extQueue); i++ {
if time.Since(extQueue[i].Created).Seconds() > ExtQueueTimeLimit {
extQueue[i] = &extForm
added = true
break
}
}
if !added {
extQueue = append(extQueue, &extForm)
}
return jsonSuccess(c)
}

func fillFormHandle(c echo.Context) error {
var (
err error
ext ExtTask
form FillFormResponse
)
if err = c.Bind(&ext); err != nil {
return jsonError(c, err)
}
if task, ok := tasks[ext.TaskId]; ok {
if task.Status >= TaskFinished {
form.Finished = true
}
} else {
return jsonError(c, fmt.Errorf(`unknown task %d`, ext.TaskId))
}

extMutex.Lock()
defer extMutex.Unlock()

form.List = make([]es.ExtFill, 0)
newQueue := make([]*es.ExtForm, 0)
for i, item := range extQueue {
if time.Since(item.Created).Seconds() > ExtQueueTimeLimit {
extQueue[i] = nil
continue
}
if item.TaskId == ext.TaskId {
form.List = append(form.List, item.List...)
extQueue[i] = nil
continue
}
newQueue = append(newQueue, item)
}
extQueue = newQueue
return c.JSON(http.StatusOK, &form)
}
2 changes: 1 addition & 1 deletion const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package main

const (
// Version of the application
Version = "1.21.0"
Version = "1.22.0"
// DefPort is the default web-server port
DefPort = 3234
// DefTheme is the default web-server theme
Expand Down
4 changes: 2 additions & 2 deletions languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func InitLang() {
}

func GetLangCode(user *users.User) (ret string) {
if user == nil && IsScript {
if /*user == nil &&*/ IsScript {
return scriptTask.Header.Lang
}
if u, ok := userSettings[user.ID]; ok {
Expand All @@ -55,7 +55,7 @@ func GetLangCode(user *users.User) (ret string) {
}

func GetLangId(user *users.User) (ret int) {
if user == nil && IsScript {
if /*user == nil &&*/ IsScript {
return langsId[scriptTask.Header.Lang]
}
if u, ok := userSettings[user.ID]; ok {
Expand Down
1 change: 1 addition & 0 deletions localhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func RunLocalServer(port int) *echo.Echo {
e.POST("/api/notification", notificationHandle)
e.POST("/api/taskstatus", taskStatusHandle)
e.POST("/api/runscript", runScriptHandle)
e.POST("/api/extqueue", extQueueHandle)
}
go func() {
if IsScript {
Expand Down
56 changes: 56 additions & 0 deletions script/browser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2021 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.

package script

import (
"encoding/json"
"eonza/lib"
"strings"
"time"
)

type ExtData struct {
URL string `json:"url"`
Title string `json:"title"`
HTML string `json:"html,omitempty"`
}

type ExtFill struct {
Idname string `json:"id"`
Value string `json:"value"`
}

type ExtForm struct {
List []ExtFill
Created time.Time
TaskId uint32
}

func FillForm(url, list string) {
data := (*dataScript.Global)[`data`]
if len(data) == 0 {
return
}
var ext ExtData
if err := json.Unmarshal([]byte(data), &ext); err != nil && len(ext.URL) == 0 {
return
}
if url != ext.URL && (!strings.HasSuffix(url, `*`) ||
!strings.HasPrefix(ext.URL, strings.TrimRight(url, `*`))) {
return
}
dataList := ExtForm{
Created: time.Now(),
TaskId: scriptTask.Header.TaskID,
}
if err := json.Unmarshal([]byte(list), &dataList.List); err == nil {
for i := 0; i < len(dataList.List); i++ {
if val, err := Macro(dataList.List[i].Value); err == nil {
dataList.List[i].Value = val
}
}
lib.LocalPost(scriptTask.Header.ServerPort, `api/extqueue`, dataList)
}
}
16 changes: 16 additions & 0 deletions script/embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ var (
{Prototype: `CopyClipboard(str)`, Object: CopyClipboard},
{Prototype: `File(str) str`, Object: FileLoad},
{Prototype: `Form(str)`, Object: Form},
{Prototype: `FillForm(str,str)`, Object: FillForm},
{Prototype: `GetClipboard() str`, Object: GetClipboard},
{Prototype: `IsEntry() bool`, Object: IsEntry},
{Prototype: `IsVarObj(str) bool`, Object: IsVarObj},
Expand All @@ -164,6 +165,7 @@ var (
{Prototype: `GetVarBytes(str) str`, Object: GetVarBytes},
{Prototype: `GetVarInt(str) int`, Object: GetVarInt},
{Prototype: `GetVarObj(str) obj`, Object: GetVarObj},
{Prototype: `GetConst(str) str`, Object: GetConst},
{Prototype: `SendNotification(str)`, Object: SendNotification},
{Prototype: `SendEmail(obj, obj)`, Object: SendEmail},
{Prototype: `SQLClose(str)`, Object: SQLClose},
Expand All @@ -190,6 +192,7 @@ var (
{Prototype: `XlsxRows(handle,str,str) handle`, Object: XLRows},
{Prototype: `XlsxNextRow(handle) bool`, Object: XLNextRow},
{Prototype: `XlsxGetRow(handle) obj`, Object: XLGetRow},
{Prototype: `XlsxGetCell(handle,str,str) str`, Object: XLGetCell},
// Windows functions
{Prototype: `RegistrySubkeys(int,str,int) arr.str`, Object: RegistrySubkeys},
{Prototype: `CreateRegistryKey(int,str,int) handle`, Object: CreateRegistryKey},
Expand Down Expand Up @@ -378,6 +381,19 @@ func FileLoad(rt *vm.Runtime, fname string) (ret string, err error) {
return vm.ReadFileºStr(rt, ret)
}

func GetConst(name string) (ret string, err error) {
if len(name) == 0 {
err = fmt.Errorf("invalid value")
return
}
var ok bool
ret, ok = (*dataScript.Global)[name]
if ok && ret == EonzaDynamic {
ret = GetEonzaDynamic(name)
}
return
}

func GetVar(name string) (ret string, err error) {
if IsVar(name) != 0 {
id := len(dataScript.Vars) - 1
Expand Down
4 changes: 4 additions & 0 deletions script/excel.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ func XLGetRow(rows *ExcelRows) (*core.Obj, error) {
}
return ifaceToObj(mapcols)
}

func XLGetCell(xls *Excel, sheet, axis string) (string, error) {
return xls.File.GetCellValue(sheet, axis)
}
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func RunServer(options lib.HTTPConfig) *echo.Echo {
e.GET("/api/trial/:id", trialHandle) // +
e.GET("/api/browsers", browsersHandle) // +
e.GET("/api/removebrowser/:id", removeBrowserHandle) // +
e.POST("/api/fillform", fillFormHandle)
e.POST("/api/browserrun", browserRunHandle)
e.POST("/api/browserext", browserExtHandle)
e.POST("/api/savebrowser", saveBrowserHandle) // +
Expand Down

0 comments on commit 0a2d22e

Please sign in to comment.