Skip to content

Commit ee2df28

Browse files
committed
scriptcat包框架
0 parents  commit ee2df28

11 files changed

Lines changed: 156 additions & 0 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
.idea
3+
.vscode
4+
5+
config.yaml

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# CloudCat
2+

cmd/main/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
func main() {
4+
5+
}

config.yaml.example

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
# 启动模式 debug/prod
3+
mode: debug
4+
5+
db:
6+
mysql:
7+
dsn: root:password@tcp(127.0.0.1:3306)/scriptweb?charset=utf8mb4&collation=utf8mb4_unicode_ci&parseTime=True&loc=Local&multiStatements=true
8+
prefix: cm_
9+
10+
redis:
11+
addr: localhost:6379
12+
password:
13+
db: 0
14+
15+
# 缓存redis
16+
cache:
17+
addr: localhost:6379
18+
password:
19+
db: 1
20+
21+
# 暴露的端口
22+
webPort: 8080
23+
24+
# 论坛的oauth2登录
25+
oauth:
26+
clientId: xxx
27+
clientSecret: xxx
28+
29+
# jwt token
30+
jwt:
31+
token: xxx
32+
33+
# 执行的定时任务
34+
crontab:
35+
- scripts

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/scriptscat/cloudcat
2+
3+
go 1.15
4+
5+
require rogchap.com/v8go v0.6.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rogchap.com/v8go v0.6.0 h1:wNn5Iu06+ke7HM511zXTVBBpRNYaauyZoPMTrLsGurU=
2+
rogchap.com/v8go v0.6.0/go.mod h1:IitZnaOtWSJadY/7qinKHIEHpxsilMWyLQ+Efdo4n4I=

pkg/scriptcat/context.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package scriptcat
2+
3+
import "rogchap.com/v8go"
4+
5+
type Context struct {
6+
ctx *v8go.Context
7+
opts *Options
8+
}
9+
10+
func NewContext(iso *Isolate, opt ...Option) (*Context, error) {
11+
global, err := v8go.NewObjectTemplate(iso.iso)
12+
if err != nil {
13+
return nil, err
14+
}
15+
options := &Options{
16+
iso: iso.iso,
17+
global: global,
18+
}
19+
for _, o := range opt {
20+
o(options)
21+
}
22+
ctx, err := v8go.NewContext(iso.iso, options.global)
23+
if err != nil {
24+
return nil, err
25+
}
26+
return &Context{
27+
ctx: ctx,
28+
}, nil
29+
}
30+
31+
func (c *Context) RunScript(source string, origin string) (*v8go.Value, error) {
32+
return c.ctx.RunScript(source, origin)
33+
}

pkg/scriptcat/gm.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package scriptcat
2+
3+
import (
4+
"rogchap.com/v8go"
5+
)
6+
7+
func globalFunc(opts *Options, name string, callback v8go.FunctionCallback) {
8+
f, err := v8go.NewFunctionTemplate(opts.iso, callback)
9+
if err != nil {
10+
opts.err = err
11+
return
12+
}
13+
if err := opts.global.Set(name, f); err != nil {
14+
opts.err = err
15+
}
16+
}
17+
18+
func GmXmlHttpRequest() Option {
19+
return func(opts *Options) {
20+
globalFunc(opts, "GM_xmlhttpRequest", func(info *v8go.FunctionCallbackInfo) *v8go.Value {
21+
22+
return nil
23+
})
24+
}
25+
}

pkg/scriptcat/isolate.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package scriptcat
2+
3+
import "rogchap.com/v8go"
4+
5+
type Isolate struct {
6+
iso *v8go.Isolate
7+
}
8+
9+
func NewIsolate() (*Isolate, error) {
10+
iso, err := v8go.NewIsolate()
11+
if err != nil {
12+
return nil, err
13+
}
14+
return &Isolate{iso: iso}, nil
15+
}
16+
17+
func (c *Isolate) Run(ctx *Context, source string) (*v8go.Value, error) {
18+
return ctx.RunScript(source, "main.js")
19+
}

pkg/scriptcat/isolate_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package scriptcat
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestIsolate(t *testing.T) {
8+
iso, _ := NewIsolate()
9+
10+
ctx, _ := NewContext(iso, GmXmlHttpRequest())
11+
12+
iso.Run(ctx, "GM_xmlhttpRequest({url:'https://bbs.tampermonkey.net.cn/'})")
13+
14+
}

0 commit comments

Comments
 (0)