action.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /**
  2. * 浏览器行为封装
  3. * 基础动作
  4. */
  5. package main
  6. import (
  7. "context"
  8. "errors"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/chromedp/chromedp"
  13. "github.com/yuin/gopher-lua"
  14. )
  15. const (
  16. selector_type_id = 0
  17. selector_type_query = 1
  18. selector_type_search = 2
  19. selector_type_jspath = 3
  20. selector_type_query_all = 4
  21. execute_return_type_string = 0
  22. execute_return_type_list = 1
  23. execute_return_type_table = 2
  24. )
  25. // findTab 根据标题、url找tab
  26. func (b *Browser) findTabContext(tabTitle, tabUrl string, timeoutInt64 int64) (ctx context.Context, err error) {
  27. if timeoutInt64 == 0 {
  28. timeoutInt64 = 5000
  29. }
  30. timeout := time.Duration(timeoutInt64) * time.Millisecond
  31. if tabTitle == "*" && tabUrl == "*" {
  32. return b.Ctx, nil
  33. } else if tabTitle == "" && tabUrl == "" {
  34. ctx, _ = context.WithTimeout(b.Ctx, timeout)
  35. return ctx, nil
  36. } else {
  37. ts, err := chromedp.Targets(b.Ctx)
  38. if err != nil {
  39. return nil, err
  40. }
  41. for _, t := range ts {
  42. if (tabTitle != "" && strings.Contains(t.Title, tabTitle)) ||
  43. (tabUrl != "" && strings.Contains(t.URL, tabUrl)) {
  44. // log.Printf("find tab param<title,url>: %s %s found %s %s", tabTitle, tabUrl,
  45. // t.Title, t.URL)
  46. newCtx, _ := chromedp.NewContext(b.Ctx, chromedp.WithTargetID(t.TargetID))
  47. ctx, _ = context.WithTimeout(newCtx, timeout)
  48. return ctx, nil
  49. }
  50. }
  51. }
  52. return nil, errors.New("can't find tab")
  53. }
  54. // BindLuaState
  55. func (b *Browser) BindLuaState(state *lua.LState) {
  56. //执行暂停
  57. state.SetGlobal("browser_sleep", state.NewFunction(func(l *lua.LState) int {
  58. timeout := l.ToInt64(-1)
  59. if timeout == 0 {
  60. timeout = 5
  61. }
  62. time.Sleep(time.Duration(timeout) * time.Millisecond)
  63. return 0
  64. }))
  65. //关闭tabl页
  66. state.SetGlobal("browser_closetabs", state.NewFunction(func(l *lua.LState) int {
  67. tabTitle := l.ToString(-3)
  68. tabUrl := l.ToString(-2)
  69. timeout := l.ToInt64(-1)
  70. if timeout == 0 {
  71. timeout = 5
  72. }
  73. b.CloseTabs(tabTitle, tabUrl, timeout)
  74. return 0
  75. }))
  76. //注册打开地址
  77. state.SetGlobal("browser_navagite", state.NewFunction(func(l *lua.LState) int {
  78. tabTitle := l.ToString(-5)
  79. tabUrl := l.ToString(-4)
  80. isNewTab := l.ToBool(-3)
  81. timeout := l.ToInt64(-2)
  82. targetUrl := l.ToString(-1)
  83. if err := b.Navigate(tabTitle, tabUrl, isNewTab, targetUrl, timeout); err != nil {
  84. l.Push(lua.LString(err.Error()))
  85. } else {
  86. l.Push(lua.LString("ok"))
  87. }
  88. return 1
  89. }))
  90. //执行浏览器端js
  91. state.SetGlobal("browser_executejs", state.NewFunction(func(l *lua.LState) int {
  92. tabTitle := l.ToString(-5)
  93. tabUrl := l.ToString(-4)
  94. timeout := l.ToInt64(-3)
  95. returnType := l.ToInt(-2)
  96. script := l.ToString(-1)
  97. switch returnType {
  98. case execute_return_type_string: //返回string
  99. var ret string
  100. if err := b.ExecuteJS(tabTitle, tabUrl, script, &ret, timeout); err == nil {
  101. l.Push(lua.LString("ok"))
  102. l.Push(lua.LString(ret))
  103. } else {
  104. l.Push(lua.LString("err"))
  105. l.Push(lua.LString(err.Error()))
  106. }
  107. case execute_return_type_list: //返回list
  108. var ret = make([]interface{}, 0, 0)
  109. var tmp = make(map[string]interface{})
  110. if err := b.ExecuteJS(tabTitle, tabUrl, script, &ret, timeout); err == nil {
  111. for i, v := range ret {
  112. tmp[strconv.Itoa(i)] = v
  113. }
  114. l.Push(lua.LString("ok"))
  115. l.Push(MapToTable(tmp))
  116. } else {
  117. l.Push(lua.LString("err"))
  118. l.Push(lua.LString(err.Error()))
  119. }
  120. case execute_return_type_table: //返回table
  121. var ret = make(map[string]interface{})
  122. if err := b.ExecuteJS(tabTitle, tabUrl, script, &ret, timeout); err == nil {
  123. l.Push(lua.LString("ok"))
  124. l.Push(MapToTable(ret))
  125. } else {
  126. l.Push(lua.LString("err"))
  127. l.Push(lua.LString(err.Error()))
  128. }
  129. }
  130. return 2
  131. }))
  132. //按键
  133. state.SetGlobal("browser_keysend", state.NewFunction(func(l *lua.LState) int {
  134. tabTitle := l.ToString(-6)
  135. tabUrl := l.ToString(-5)
  136. timeout := l.ToInt64(-4)
  137. selectorType := l.ToInt(-3)
  138. selector := l.ToString(-2)
  139. words := l.ToString(-1)
  140. err := b.KeySend(tabTitle, tabUrl, selector, words, selectorType, timeout)
  141. if err != nil {
  142. l.Push(lua.LString(err.Error()))
  143. } else {
  144. l.Push(lua.LString("ok"))
  145. }
  146. return 1
  147. }))
  148. //点击
  149. state.SetGlobal("browser_click", state.NewFunction(func(l *lua.LState) int {
  150. tabTitle := l.ToString(-5)
  151. tabUrl := l.ToString(-4)
  152. timeout := l.ToInt64(-3)
  153. selectorType := l.ToInt(-2)
  154. selector := l.ToString(-1)
  155. err := b.Click(tabTitle, tabUrl, selector, selectorType, timeout)
  156. if err != nil {
  157. l.Push(lua.LString(err.Error()))
  158. } else {
  159. l.Push(lua.LString("ok"))
  160. }
  161. return 1
  162. }))
  163. //browser_history_back
  164. state.SetGlobal("browser_history_back", state.NewFunction(func(l *lua.LState) int {
  165. tabTitle := l.ToString(-3)
  166. tabUrl := l.ToString(-2)
  167. timeout := l.ToInt64(-1)
  168. err := b.GoHistoryBack(tabTitle, tabUrl, timeout)
  169. if err != nil {
  170. l.Push(lua.LString(err.Error()))
  171. } else {
  172. l.Push(lua.LString("ok"))
  173. }
  174. return 1
  175. }))
  176. state.SetGlobal("browser_waitvisible", state.NewFunction(func(l *lua.LState) int {
  177. tabTitle := l.ToString(-5)
  178. tabUrl := l.ToString(-4)
  179. timeout := l.ToInt64(-3)
  180. selectorType := l.ToInt(-2)
  181. selector := l.ToString(-1)
  182. err := b.WaitVisible(tabTitle, tabUrl, selector, selectorType, timeout)
  183. if err != nil {
  184. l.Push(lua.LString(err.Error()))
  185. } else {
  186. l.Push(lua.LString("ok"))
  187. }
  188. return 1
  189. }))
  190. //点击
  191. state.SetGlobal("browser_downloadfile", state.NewFunction(func(l *lua.LState) int {
  192. tabTitle := l.ToString(-7)
  193. tabUrl := l.ToString(-6)
  194. timeout := l.ToInt64(-5)
  195. selectorType := l.ToInt(-4)
  196. selector := l.ToString(-3)
  197. filename := l.ToString(-2)
  198. save2dir := l.ToString(-1)
  199. err := b.DownloadFile(tabTitle, tabUrl, timeout, selector, selectorType, filename, save2dir)
  200. if err != nil {
  201. l.Push(lua.LString(err.Error()))
  202. } else {
  203. l.Push(lua.LString("ok"))
  204. }
  205. return 1
  206. }))
  207. //关闭tabl页
  208. state.SetGlobal("browser_closetabs_without", state.NewFunction(func(l *lua.LState) int {
  209. tabTitle := l.ToString(-3)
  210. tabUrl := l.ToString(-2)
  211. timeout := l.ToInt64(-1)
  212. if timeout == 0 {
  213. timeout = 5
  214. }
  215. b.CloseTabsWithout(tabTitle, tabUrl, timeout)
  216. return 0
  217. }))
  218. //browser_screenshot 网页局部截图
  219. state.SetGlobal("browser_screenshot", state.NewFunction(func(l *lua.LState) int {
  220. tabTitle := l.ToString(-6)
  221. tabUrl := l.ToString(-5)
  222. timeout := l.ToInt64(-4)
  223. selectorType := l.ToInt(-3)
  224. selector := l.ToString(-2)
  225. filename := l.ToString(-1)
  226. if timeout == 0 {
  227. timeout = 5
  228. }
  229. if err := b.Screenshot(tabTitle, tabUrl, timeout, selectorType, selector, filename); err != nil {
  230. l.Push(lua.LString(err.Error()))
  231. } else {
  232. l.Push(lua.LString("ok"))
  233. }
  234. return 1
  235. }))
  236. //browser_print2pdf 整个网页生成pdf
  237. state.SetGlobal("browser_print2pdf", state.NewFunction(func(l *lua.LState) int {
  238. tabTitle := l.ToString(-4)
  239. tabUrl := l.ToString(-3)
  240. timeout := l.ToInt64(-2)
  241. filename := l.ToString(-1)
  242. if timeout == 0 {
  243. timeout = 5
  244. }
  245. if err := b.PrintToPDF(tabTitle, tabUrl, timeout, filename); err != nil {
  246. l.Push(lua.LString(err.Error()))
  247. } else {
  248. l.Push(lua.LString("ok"))
  249. }
  250. return 1
  251. }))
  252. state.SetGlobal("browser_tabs", state.NewFunction(func(l *lua.LState) int {
  253. tabTitle := l.ToString(-3)
  254. tabUrl := l.ToString(-2)
  255. timeout := l.ToInt64(-1)
  256. if timeout == 0 {
  257. timeout = 500
  258. }
  259. var tmp = make(map[string]interface{})
  260. tabs, err := b.GetBrowserTabs(tabTitle, tabUrl, timeout)
  261. //fmt.Println(err, tabs)
  262. if err == nil {
  263. for i, v := range tabs {
  264. tmp[strconv.Itoa(i)] = v
  265. }
  266. l.Push(lua.LString("ok"))
  267. l.Push(MapToTable(tmp))
  268. } else {
  269. l.Push(lua.LString("err"))
  270. l.Push(lua.LString(err.Error()))
  271. }
  272. return 2
  273. }))
  274. state.SetGlobal("browser_send_img_chatbot", state.NewFunction(func(l *lua.LState) int {
  275. mentioned := l.ToString(-3)
  276. uri := l.ToString(-2)
  277. img := l.ToString(-1)
  278. err := SendImage2ChatBot(uri, img, mentioned)
  279. if err != nil {
  280. l.Push(lua.LString("err"))
  281. l.Push(lua.LString(err.Error()))
  282. } else {
  283. l.Push(lua.LString("ok"))
  284. l.Push(lua.LString("ok"))
  285. }
  286. return 2
  287. }))
  288. state.SetGlobal("browser_send_text_chatbot", state.NewFunction(func(l *lua.LState) int {
  289. mentioned := l.ToString(-3)
  290. uri := l.ToString(-2)
  291. text := l.ToString(-1)
  292. err := SendImage2ChatBot(uri, text, mentioned)
  293. if err != nil {
  294. l.Push(lua.LString("err"))
  295. l.Push(lua.LString(err.Error()))
  296. } else {
  297. l.Push(lua.LString("ok"))
  298. l.Push(lua.LString("ok"))
  299. }
  300. return 2
  301. }))
  302. state.SetGlobal("browser_reload", state.NewFunction(func(l *lua.LState) int {
  303. tabTitle := l.ToString(-3)
  304. tabUrl := l.ToString(-2)
  305. timeout := l.ToInt64(-1)
  306. err := b.Reload(tabTitle, tabUrl, timeout)
  307. if err != nil {
  308. l.Push(lua.LString("err"))
  309. } else {
  310. l.Push(lua.LString("ok"))
  311. }
  312. return 1
  313. }))
  314. state.SetGlobal("browser_back", state.NewFunction(func(l *lua.LState) int {
  315. tabTitle := l.ToString(-3)
  316. tabUrl := l.ToString(-2)
  317. timeout := l.ToInt64(-1)
  318. err := b.Back(tabTitle, tabUrl, timeout)
  319. if err != nil {
  320. l.Push(lua.LString("err"))
  321. } else {
  322. l.Push(lua.LString("ok"))
  323. }
  324. return 1
  325. }))
  326. state.SetGlobal("browser_forward", state.NewFunction(func(l *lua.LState) int {
  327. tabTitle := l.ToString(-3)
  328. tabUrl := l.ToString(-2)
  329. timeout := l.ToInt64(-1)
  330. err := b.Forward(tabTitle, tabUrl, timeout)
  331. if err != nil {
  332. l.Push(lua.LString("err"))
  333. } else {
  334. l.Push(lua.LString("ok"))
  335. }
  336. return 1
  337. }))
  338. }