Skip to content

Commit 23f7fa4

Browse files
committed
scriptcat 命令
1 parent ee2df28 commit 23f7fa4

14 files changed

Lines changed: 816 additions & 35 deletions

File tree

cmd/scriptcat/exec.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"github.com/scriptscat/cloudcat/pkg/scriptcat"
5+
"github.com/spf13/cobra"
6+
"io/ioutil"
7+
)
8+
9+
type execCmd struct {
10+
}
11+
12+
func newExecCmd() *execCmd {
13+
return &execCmd{}
14+
}
15+
16+
func (e *execCmd) Commands() []*cobra.Command {
17+
ret := &cobra.Command{
18+
Use: "exec [file] [flags]",
19+
Short: "执行一个脚本猫脚本",
20+
RunE: e.exec,
21+
}
22+
23+
return []*cobra.Command{ret}
24+
}
25+
26+
func (e *execCmd) exec(cmd *cobra.Command, args []string) error {
27+
28+
sc, err := scriptcat.NewScriptCat()
29+
if err != nil {
30+
return err
31+
}
32+
33+
script, err := ioutil.ReadFile(args[0])
34+
if err != nil {
35+
return err
36+
}
37+
38+
return sc.Run(string(script))
39+
}

cmd/scriptcat/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func main() {
9+
10+
rootCmd := &cobra.Command{
11+
Use: "scriptcat",
12+
}
13+
14+
execCmd := newExecCmd()
15+
16+
rootCmd.AddCommand(execCmd.Commands()...)
17+
18+
if err := rootCmd.Execute(); err != nil {
19+
logrus.Fatalln(err)
20+
}
21+
}

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ module github.com/scriptscat/cloudcat
22

33
go 1.15
44

5-
require rogchap.com/v8go v0.6.0
5+
require (
6+
github.com/sirupsen/logrus v1.8.1
7+
github.com/spf13/cobra v1.2.1
8+
github.com/stretchr/testify v1.7.0
9+
rogchap.com/v8go v0.6.0
10+
)

go.sum

Lines changed: 577 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package scriptcat
1+
package executor
22

33
import "rogchap.com/v8go"
44

@@ -7,19 +7,19 @@ type Context struct {
77
opts *Options
88
}
99

10-
func NewContext(iso *Isolate, opt ...Option) (*Context, error) {
11-
global, err := v8go.NewObjectTemplate(iso.iso)
10+
func NewContext(executor *Executor, opt ...Option) (*Context, error) {
11+
global, err := v8go.NewObjectTemplate(executor.iso)
1212
if err != nil {
1313
return nil, err
1414
}
1515
options := &Options{
16-
iso: iso.iso,
16+
iso: executor.iso,
1717
global: global,
1818
}
1919
for _, o := range opt {
2020
o(options)
2121
}
22-
ctx, err := v8go.NewContext(iso.iso, options.global)
22+
ctx, err := v8go.NewContext(executor.iso, options.global)
2323
if err != nil {
2424
return nil, err
2525
}

pkg/executor/executor.go

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

33
import (
44
"testing"
55
)
66

77
func TestIsolate(t *testing.T) {
8-
iso, _ := NewIsolate()
8+
iso, _ := NewExecutor()
99

10-
ctx, _ := NewContext(iso, GmXmlHttpRequest())
10+
ctx, _ := NewContext(iso, GmXmlHttpRequest(nil))
1111

1212
iso.Run(ctx, "GM_xmlhttpRequest({url:'https://bbs.tampermonkey.net.cn/'})")
1313

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
package scriptcat
1+
package executor
22

33
import (
4+
"net/http"
5+
46
"rogchap.com/v8go"
57
)
68

@@ -15,7 +17,7 @@ func globalFunc(opts *Options, name string, callback v8go.FunctionCallback) {
1517
}
1618
}
1719

18-
func GmXmlHttpRequest() Option {
20+
func GmXmlHttpRequest(jar http.CookieJar) Option {
1921
return func(opts *Options) {
2022
globalFunc(opts, "GM_xmlhttpRequest", func(info *v8go.FunctionCallbackInfo) *v8go.Value {
2123

pkg/executor/option.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package executor
2+
3+
import "rogchap.com/v8go"
4+
5+
type Options struct {
6+
iso *v8go.Isolate
7+
global *v8go.ObjectTemplate
8+
err error
9+
}
10+
11+
type Option func(opts *Options)

pkg/scriptcat/isolate.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)