How to use GetElementsByClassName method of html Package

Best K6 code snippet using html.GetElementsByClassName

blackjack.go

Source:blackjack.go Github

copy

Full Screen

1package main2import (3 "log"4 "net"5 "net/http"6 "strconv"7 //"fmt"8 "./blackjack"9 "./cards"10 "github.com/zserge/webview"11)12var indexHTML = `13<!doctype html>14<html>15 <head>16 <meta http-equiv="X-UA-Compatible" content="IE=edge">17 </head>18 <body>19 <button id = "start" onclick="external.invoke('start')">Start</button>20 <button id = "hit" onclick="external.invoke('hit')">Hit</button>21 <button id = "stand" onclick="external.invoke('stand')">Stand</button>22 <button id = "double" onclick="external.invoke('double')">Double</button>23 <button id = "split" onclick="external.invoke('split')">Split</button>24 <button id = "settle" onclick="external.invoke('settle')">Settle</button>25 <label id = "turn" >Blank</label>26 <label id = "walletTitle"> Wallet:</label>27 <label id = "betTitle"> Bet:</label>28 <div id="dealer">29 <h2> dealer</h2>30 <p>blank</p> 31 </div>32 <div id = "player1">33 <h2>player1</h2>34 <p>blank</p>35 <p class = "wallet"></p>36 <p class = "bet"></p>37 <p class = "splithand"></p>38 </div>39 <div id = "player2">40 <h2>player2</h2>41 <p>blank</p>42 <p class = "wallet"></p>43 <p class = "bet"></p>44 <p class = "splithand"></p>45 </div>46 <div id = "player3">47 <h2>player3</h2>48 <p>blank</p>49 <p class = "wallet"></p>50 <p class = "bet"></p>51 <p class = "splithand"></p>52 </div>53 54 </body>55 <style> 56 #dealer57 {58 position:absolute;59 left:400px;60 top:50px61 }62 #player163 {64 position:absolute;65 left:100px;66 top:300px67 }68 #player269 {70 position:absolute;71 left:500px;72 top:300px73 }74 #player375 {76 position:absolute;77 left:900px;78 top:300px79 }80 #walletTitle81 {82 position:absolute;83 left:10px;84 top:400px85 }86 #betTitle87 {88 position:absolute;89 left:10px;90 top:440px91 }92 #hit93 {94 visibility:hidden95 }96 #stand97 {98 visibility:hidden99 }100 #double101 {102 visibility:hidden103 }104 #split105 {106 visibility:hidden107 }108 #settle109 {110 visibility:hidden111 } 112 </style> 113 <script>114 115 </script>116</html>117`118var deck = cards.NewDeck(6)119var dealersHand = new(blackjack.Hand)120var player1Hand = new(blackjack.Hand)121var player2Hand = new(blackjack.Hand)122var player3Hand = new(blackjack.Hand)123var player1Split = new(blackjack.Hand)124var player2Split = new(blackjack.Hand)125var player3Split = new(blackjack.Hand)126var turn = "blank"127func startServer() string {128 ln, err := net.Listen("tcp", "127.0.0.1:0")129 if err != nil {130 log.Fatal(err)131 }132 go func() {133 defer ln.Close()134 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {135 w.Write([]byte(indexHTML))136 })137 log.Fatal(http.Serve(ln, nil))138 }()139 return "http://" + ln.Addr().String()140}141func handleRPC(w webview.WebView, data string) {142 switch {143 case data == "stand":144 stand(w)145 case data == "start":146 start(w)147 case data == "hit":148 hit(w)149 case data == "settle":150 settle(w)151 case data == "double":152 double(w)153 case data == "split":154 split(w)155 }156}157func main() {158 url := startServer()159 w := webview.New(webview.Settings{160 Width: 1200,161 Height: 800,162 Title: "blackjack demo",163 Resizable: true,164 URL: url,165 ExternalInvokeCallback: handleRPC,166 })167 defer w.Exit()168 w.Run()169}170func double(w webview.WebView) {171 if turn == "player1's turn" {172 if player1Hand.CanDouble() && player1Hand.CanBet() {173 player1Hand.Double()174 if player1Hand.Score() > 21 {175 w.Dialog(webview.DialogTypeAlert, 0, "Player1", "Player1 bust!\n")176 }177 if player2Hand.Bet != 0 {178 turn = "player2's turn"179 } else if player3Hand.Bet != 0 {180 turn = "player3's turn"181 } else {182 turn = "dealer's turn"183 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)184 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)185 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)186 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)187 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)188 }189 w.Dispatch(func() {190 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)191 w.Eval(`document.getElementById("player1").getElementsByTagName("p")[0].innerHTML = "` + player1Hand.ToStr() + `";`)192 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)193 w.Eval(`document.getElementById("player1").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Bet) + `";`)194 })195 } else {196 w.Dialog(webview.DialogTypeAlert, 0, "Player1", "Player1 can not double!\n")197 }198 } else if turn == "player2's turn" {199 if player2Hand.CanDouble() && player2Hand.CanBet() {200 player2Hand.Double()201 if player2Hand.Score() > 21 {202 w.Dialog(webview.DialogTypeAlert, 0, "Player2", "Player2 bust!\n")203 }204 if player3Hand.Bet != 0 {205 turn = "player3's turn"206 } else {207 turn = "dealer's turn"208 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)209 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)210 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)211 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)212 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)213 }214 w.Dispatch(func() {215 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)216 w.Eval(`document.getElementById("player2").getElementsByTagName("p")[0].innerHTML = "` + player2Hand.ToStr() + `";`)217 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Wallet) + `";`)218 w.Eval(`document.getElementById("player2").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Bet) + `";`)219 })220 } else {221 w.Dialog(webview.DialogTypeAlert, 0, "Player2", "Player2 can not double!\n")222 }223 } else if turn == "player3's turn" {224 if player3Hand.CanDouble() && player3Hand.CanBet() {225 player3Hand.Double()226 if player3Hand.Score() > 21 {227 w.Dialog(webview.DialogTypeAlert, 0, "Player3", "Player3 bust!\n")228 }229 turn = "dealer's turn"230 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)231 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)232 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)233 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)234 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)235 w.Dispatch(func() {236 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)237 w.Eval(`document.getElementById("player3").getElementsByTagName("p")[0].innerHTML = "` + player3Hand.ToStr() + `";`)238 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Wallet) + `";`)239 w.Eval(`document.getElementById("player3").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Bet) + `";`)240 })241 } else {242 w.Dialog(webview.DialogTypeAlert, 0, "Player3", "Player3 can not double!\n")243 }244 } else if turn == "dealer's turn" {245 w.Dialog(webview.DialogTypeAlert, 0, "Dealer", "dealer can not double!\n")246 }247}248func settle(w webview.WebView) {249 if turn == "dealer's turn" {250 for dealersHand.Score() < 17 {251 card := dealersHand.Hit()252 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + card.ToStr() + `";`)253 }254 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML = "` + dealersHand.ToStr() + `";`)255 if player1Hand.HasSplit {256 if player1Hand.Score() == 21 || player1Split.Score() == 21 {257 if dealersHand.Score() != 21 {258 player1Hand.Wallet += player1Hand.Bet * 2259 }260 } else if player1Hand.IsBust == true && player1Split.IsBust == true {261 if dealersHand.Score() > 21 {262 player1Hand.Wallet += player1Hand.Bet263 }264 } else {265 if dealersHand.Score() > 21 {266 player1Hand.Wallet += player1Hand.Bet * 2267 } else if dealersHand.Score() > player1Hand.Score() && dealersHand.Score() > player1Split.Score() {268 } else if dealersHand.Score() < player1Hand.Score() || dealersHand.Score() < player1Split.Score() {269 player1Hand.Wallet += player1Hand.Bet * 2270 } else {271 player1Hand.Wallet += player1Hand.Bet272 }273 }274 } else {275 if player1Hand.Score() == 21 {276 if dealersHand.Score() != 21 {277 player1Hand.Wallet += player1Hand.Bet * 2278 }279 } else if player1Hand.IsBust == true {280 if dealersHand.Score() > 21 {281 player1Hand.Wallet += player1Hand.Bet282 }283 } else {284 if dealersHand.Score() > 21 {285 player1Hand.Wallet += player1Hand.Bet * 2286 } else if dealersHand.Score() > player1Hand.Score() {287 } else if dealersHand.Score() < player1Hand.Score() {288 player1Hand.Wallet += player1Hand.Bet * 2289 } else {290 player1Hand.Wallet += player1Hand.Bet291 }292 }293 }294 if player2Hand.HasSplit {295 if player2Hand.Score() == 21 || player2Split.Score() == 21 {296 if dealersHand.Score() != 21 {297 player2Hand.Wallet += player2Hand.Bet * 2298 }299 } else if player2Hand.IsBust == true && player2Split.IsBust == true {300 if dealersHand.Score() > 21 {301 player2Hand.Wallet += player2Hand.Bet302 }303 } else {304 if dealersHand.Score() > 21 {305 player2Hand.Wallet += player2Hand.Bet * 2306 } else if dealersHand.Score() > player2Hand.Score() && dealersHand.Score() > player2Split.Score() {307 } else if dealersHand.Score() < player2Hand.Score() || dealersHand.Score() < player2Split.Score() {308 player2Hand.Wallet += player2Hand.Bet * 2309 } else {310 player2Hand.Wallet += player2Hand.Bet311 }312 }313 } else {314 if player2Hand.Score() == 21 {315 if dealersHand.Score() != 21 {316 player2Hand.Wallet += player2Hand.Bet * 2317 }318 } else if player2Hand.IsBust == true {319 if dealersHand.Score() > 21 {320 player2Hand.Wallet += player2Hand.Bet321 }322 } else {323 if dealersHand.Score() > 21 {324 player2Hand.Wallet += player2Hand.Bet * 2325 } else if dealersHand.Score() > player2Hand.Score() {326 } else if dealersHand.Score() < player2Hand.Score() {327 player2Hand.Wallet += player2Hand.Bet * 2328 } else {329 player2Hand.Wallet += player2Hand.Bet330 }331 }332 }333 if player3Hand.HasSplit {334 if player3Hand.Score() == 21 || player3Split.Score() == 21 {335 if dealersHand.Score() != 21 {336 player3Hand.Wallet += player3Hand.Bet * 2337 }338 } else if player3Hand.IsBust == true && player3Split.IsBust == true {339 if dealersHand.Score() > 21 {340 player3Hand.Wallet += player3Hand.Bet341 }342 } else {343 if dealersHand.Score() > 21 {344 player3Hand.Wallet += player3Hand.Bet * 2345 } else if dealersHand.Score() > player3Hand.Score() && dealersHand.Score() > player3Split.Score() {346 } else if dealersHand.Score() < player3Hand.Score() || dealersHand.Score() < player3Split.Score() {347 player3Hand.Wallet += player3Hand.Bet * 2348 } else {349 player3Hand.Wallet += player3Hand.Bet350 }351 }352 } else {353 if player3Hand.Score() == 21 {354 if dealersHand.Score() != 21 {355 player3Hand.Wallet += player3Hand.Bet * 2356 }357 } else if player3Hand.IsBust == true {358 if dealersHand.Score() > 21 {359 player3Hand.Wallet += player3Hand.Bet360 }361 } else {362 if dealersHand.Score() > 21 {363 player3Hand.Wallet += player3Hand.Bet * 2364 } else if dealersHand.Score() > player3Hand.Score() {365 } else if dealersHand.Score() < player3Hand.Score() {366 player3Hand.Wallet += player3Hand.Bet * 2367 } else {368 player3Hand.Wallet += player3Hand.Bet369 }370 }371 }372 turn = "isSettled"373 w.Dispatch(func() {374 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)375 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)376 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Wallet) + `";`)377 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Wallet) + `";`)378 })379 w.Eval(`document.getElementById("settle").style.visibility = "hidden";`)380 w.Eval(`document.getElementById("start").style.visibility = "visible";`)381 if player1Hand.HasSplit {382 player1Split.Flush()383 w.Eval(`document.getElementById("player1").getElementsByClassName("splithand")[0].style.visibility = "hidden";`)384 }385 if player2Hand.HasSplit {386 player2Split.Flush()387 w.Eval(`document.getElementById("player2").getElementsByClassName("splithand")[0].style.visibility = "hidden";`)388 }389 if player3Hand.HasSplit {390 player3Split.Flush()391 w.Eval(`document.getElementById("player3").getElementsByClassName("splithand")[0].style.visibility = "hidden";`)392 }393 player1Hand.Flush()394 player2Hand.Flush()395 player3Hand.Flush()396 }397}398func start(w webview.WebView) {399 if turn == "blank" {400 dealersHand = blackjack.NewHand(deck)401 player1Hand = blackjack.NewHand(deck)402 player2Hand = blackjack.NewHand(deck)403 player3Hand = blackjack.NewHand(deck)404 player1Hand.PutBet()405 player2Hand.PutBet()406 player3Hand.PutBet()407 turn = "player1's turn"408 w.Dispatch(func() {409 w.Eval(`document.getElementById("hit").style.visibility = "visible";`)410 w.Eval(`document.getElementById("stand").style.visibility = "visible";`)411 w.Eval(`document.getElementById("double").style.visibility = "visible";`)412 w.Eval(`document.getElementById("split").style.visibility = "visible";`)413 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML = "` + dealersHand.Cards[0].ToStr() + `";`)414 w.Eval(`document.getElementById("player1").getElementsByTagName("p")[0].innerHTML = "` + player1Hand.ToStr() + `";`)415 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)416 w.Eval(`document.getElementById("player1").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Bet) + `";`)417 w.Eval(`document.getElementById("player2").getElementsByTagName("p")[0].innerHTML = "` + player2Hand.ToStr() + `";`)418 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Wallet) + `";`)419 w.Eval(`document.getElementById("player2").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Bet) + `";`)420 w.Eval(`document.getElementById("player3").getElementsByTagName("p")[0].innerHTML = "` + player3Hand.ToStr() + `";`)421 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Wallet) + `";`)422 w.Eval(`document.getElementById("player3").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Bet) + `";`)423 })424 if player1Hand.IsBlackjack() {425 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!")426 if dealersHand.ScoreCard(0) >= 10 {427 w.Dispatch(func() {428 w.Eval(`document.getElementById("dealer").innerHTML = ` + dealersHand.ToStr() + `;`)429 })430 }431 if dealersHand.IsBlackjack() {432 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!The game is a push\n")433 } else {434 //player1Hand.Wallet+=player1Hand.Bet435 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)436 }437 }438 if player2Hand.IsBlackjack() {439 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!")440 if dealersHand.ScoreCard(0) >= 10 {441 w.Dispatch(func() {442 w.Eval(`document.getElementById("dealer").innerHTML = ` + dealersHand.ToStr() + `;`)443 })444 }445 if dealersHand.IsBlackjack() {446 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!The game is a push\n")447 } else {448 //player2Hand.Wallet+=player2Hand.Bet449 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)450 }451 }452 if player3Hand.IsBlackjack() {453 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!")454 if dealersHand.ScoreCard(0) >= 10 {455 w.Dispatch(func() {456 w.Eval(`document.getElementById("dealer").innerHTML = ` + dealersHand.ToStr() + `;`)457 })458 }459 if dealersHand.IsBlackjack() {460 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!The game is a push\n")461 } else {462 //player3Hand.Wallet+=player3Hand.Bet463 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)464 }465 }466 w.Dispatch(func() {467 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)468 w.Eval(`document.getElementById("start").style.visibility = "hidden";`)469 w.Eval(`document.getElementById("settle").style.visibility = "hidden";`)470 })471 } else if turn == "isSettled" {472 dealersHand = blackjack.NewHand(deck)473 if player1Hand.CanBet() {474 player1Hand.Refresh()475 player1Hand.PutBet()476 turn = "player1's turn"477 if player2Hand.CanBet() {478 player2Hand.Refresh()479 player2Hand.PutBet()480 }481 if player3Hand.CanBet() {482 player3Hand.Refresh()483 player3Hand.PutBet()484 }485 } else {486 if player2Hand.CanBet() {487 player2Hand.Refresh()488 player2Hand.PutBet()489 turn = "player2's turn"490 if player3Hand.CanBet() {491 player3Hand.Refresh()492 player3Hand.PutBet()493 }494 } else {495 if player3Hand.CanBet() {496 player3Hand.Refresh()497 player3Hand.PutBet()498 turn = "player3's turn"499 } else {500 w.Dialog(webview.DialogTypeAlert, 0, "Players", "Your wallet is empty! Game Over!\n")501 w.Terminate()502 }503 }504 }505 w.Dispatch(func() {506 w.Eval(`document.getElementById("hit").style.visibility = "visible";`)507 w.Eval(`document.getElementById("stand").style.visibility = "visible";`)508 w.Eval(`document.getElementById("double").style.visibility = "visible";`)509 w.Eval(`document.getElementById("split").style.visibility = "visible";`)510 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML = "` + dealersHand.Cards[0].ToStr() + `";`)511 w.Eval(`document.getElementById("player1").getElementsByTagName("p")[0].innerHTML = "` + player1Hand.ToStr() + `";`)512 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)513 w.Eval(`document.getElementById("player1").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Bet) + `";`)514 w.Eval(`document.getElementById("player2").getElementsByTagName("p")[0].innerHTML = "` + player2Hand.ToStr() + `";`)515 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Wallet) + `";`)516 w.Eval(`document.getElementById("player2").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Bet) + `";`)517 w.Eval(`document.getElementById("player3").getElementsByTagName("p")[0].innerHTML = "` + player3Hand.ToStr() + `";`)518 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Wallet) + `";`)519 w.Eval(`document.getElementById("player3").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Bet) + `";`)520 })521 if player1Hand.IsBlackjack() {522 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!")523 if dealersHand.ScoreCard(0) >= 10 {524 w.Dispatch(func() {525 w.Eval(`document.getElementById("dealer").innerHTML = ` + dealersHand.ToStr() + `;`)526 })527 }528 if dealersHand.IsBlackjack() {529 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!The game is a push\n")530 } else {531 //player1Hand.Wallet+=player1Hand.Bet532 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)533 }534 }535 if player2Hand.IsBlackjack() {536 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!")537 if dealersHand.ScoreCard(0) >= 10 {538 w.Dispatch(func() {539 w.Eval(`document.getElementById("dealer").innerHTML = ` + dealersHand.ToStr() + `;`)540 })541 }542 if dealersHand.IsBlackjack() {543 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!The game is a push\n")544 } else {545 //player2Hand.Wallet+=player2Hand.Bet546 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)547 }548 }549 if player3Hand.IsBlackjack() {550 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!")551 if dealersHand.ScoreCard(0) >= 10 {552 w.Dispatch(func() {553 w.Eval(`document.getElementById("dealer").innerHTML = ` + dealersHand.ToStr() + `;`)554 })555 }556 if dealersHand.IsBlackjack() {557 //w.Dialog(webview.DialogTypeAlert, 0, "Blackjack", "Blackjack!The game is a push\n")558 } else {559 //player3Hand.Wallet+=player3Hand.Bet560 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)561 }562 }563 w.Dispatch(func() {564 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)565 w.Eval(`document.getElementById("start").style.visibility = "hidden";`)566 w.Eval(`document.getElementById("settle").style.visibility = "hidden";`)567 })568 } else {569 w.Dialog(webview.DialogTypeAlert, 0, "Game is not finished", "Game should be settled\n")570 }571}572func hit(w webview.WebView) {573 if turn == "player1's turn" {574 card := player1Hand.Hit()575 w.Dispatch(func() {576 w.Eval(`document.getElementById("player1").getElementsByTagName("p")[0].innerHTML += ",` + card.ToStr() + `";`)577 })578 if player1Hand.Score() > 21 {579 w.Dialog(webview.DialogTypeAlert, 0, "Player1", "Player1 bust!\n")580 player1Hand.IsBust = true581 if player1Hand.HasSplit {582 turn = "player1's Split turn"583 } else if player2Hand.Bet != 0 {584 turn = "player2's turn"585 } else if player3Hand.Bet != 0 {586 turn = "player3's turn"587 } else {588 turn = "dealer's turn"589 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)590 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)591 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)592 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)593 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)594 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)595 }596 }597 w.Dispatch(func() {598 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)599 })600 } else if turn == "player1's Split turn" {601 player1Split.Hit()602 w.Eval(`document.getElementById("player1").getElementsByClassName("splithand")[0].innerHTML = "` + player1Split.ToStr() + `";`)603 if player1Split.Score() > 21 {604 w.Dialog(webview.DialogTypeAlert, 0, "Player1", "Player1 Split bust!\n")605 player1Split.IsBust = true606 if player2Hand.Bet != 0 {607 turn = "player2's turn"608 } else if player3Hand.Bet != 0 {609 turn = "player3's turn"610 } else {611 turn = "dealer's turn"612 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)613 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)614 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)615 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)616 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)617 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)618 }619 }620 w.Dispatch(func() {621 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)622 })623 } else if turn == "player2's turn" {624 card := player2Hand.Hit()625 w.Dispatch(func() {626 w.Eval(`document.getElementById("player2").getElementsByTagName("p")[0].innerHTML += ",` + card.ToStr() + `";`)627 })628 if player2Hand.Score() > 21 {629 w.Dialog(webview.DialogTypeAlert, 0, "Player2", "Player2 bust!\n")630 player2Hand.IsBust = true631 if player1Hand.HasSplit {632 turn = "player2's Split turn"633 } else if player3Hand.Bet != 0 {634 turn = "player3's turn"635 } else {636 turn = "dealer's turn"637 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)638 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)639 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)640 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)641 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)642 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)643 }644 }645 w.Dispatch(func() {646 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)647 })648 } else if turn == "player2's Split turn" {649 player2Split.Hit()650 w.Eval(`document.getElementById("player2").getElementsByClassName("splithand")[0].innerHTML = "` + player2Split.ToStr() + `";`)651 if player2Split.Score() > 21 {652 w.Dialog(webview.DialogTypeAlert, 0, "Player2", "Player2 Split bust!\n")653 player2Split.IsBust = true654 if player3Hand.Bet != 0 {655 turn = "player3's turn"656 } else {657 turn = "dealer's turn"658 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)659 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)660 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)661 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)662 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)663 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)664 }665 }666 w.Dispatch(func() {667 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)668 })669 } else if turn == "player3's turn" {670 card := player3Hand.Hit()671 w.Dispatch(func() {672 w.Eval(`document.getElementById("player3").getElementsByTagName("p")[0].innerHTML += ",` + card.ToStr() + `";`)673 })674 if player3Hand.Score() > 21 {675 w.Dialog(webview.DialogTypeAlert, 0, "Player3", "Player3 bust!\n")676 w.Dispatch(func() {677 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)678 })679 player3Hand.IsBust = true680 if player3Hand.HasSplit {681 turn = "player3's Split turn"682 } else {683 turn = "dealer's turn"684 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)685 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)686 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)687 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)688 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)689 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)690 }691 }692 w.Dispatch(func() {693 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)694 })695 } else if turn == "player3's Split turn" {696 player3Split.Hit()697 w.Eval(`document.getElementById("player3").getElementsByClassName("splithand")[0].innerHTML = "` + player3Split.ToStr() + `";`)698 if player3Split.Score() > 21 {699 w.Dialog(webview.DialogTypeAlert, 0, "Player3", "Player3 Split bust!\n")700 player3Split.IsBust = true701 turn = "dealer's turn"702 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)703 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)704 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)705 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)706 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)707 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)708 }709 w.Dispatch(func() {710 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)711 })712 }713}714func stand(w webview.WebView) {715 if turn == "player1's turn" {716 if player1Hand.HasSplit {717 turn = "player1's Split turn"718 } else if player2Hand.Bet != 0 {719 turn = "player2's turn"720 } else if player3Hand.Bet != 0 {721 turn = "player3's turn"722 } else {723 turn = "dealer's turn"724 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)725 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)726 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)727 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)728 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)729 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)730 }731 w.Dispatch(func() {732 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)733 })734 } else if turn == "player1's Split turn" {735 if player2Hand.Bet != 0 {736 turn = "player2's turn"737 } else if player3Hand.Bet != 0 {738 turn = "player3's turn"739 } else {740 turn = "dealer's turn"741 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)742 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)743 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)744 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)745 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)746 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)747 }748 w.Dispatch(func() {749 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)750 })751 } else if turn == "player2's turn" {752 if player2Hand.HasSplit {753 turn = "player2's Split turn"754 } else if player3Hand.Bet != 0 {755 turn = "player3's turn"756 } else {757 turn = "dealer's turn"758 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)759 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)760 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)761 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)762 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)763 }764 w.Dispatch(func() {765 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)766 })767 } else if turn == "player2's Split turn" {768 if player3Hand.Bet != 0 {769 turn = "player3's turn"770 } else {771 turn = "dealer's turn"772 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)773 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)774 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)775 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)776 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)777 }778 w.Dispatch(func() {779 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)780 })781 } else if turn == "player3's turn" {782 if player1Hand.HasSplit {783 turn = "player3's Split turn"784 } else {785 turn = "dealer's turn"786 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)787 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)788 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)789 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)790 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)791 w.Dispatch(func() {792 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)793 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)794 })795 }796 } else if turn == "player3's Split turn" {797 turn = "dealer's turn"798 w.Eval(`document.getElementById("settle").style.visibility = "visible";`)799 w.Eval(`document.getElementById("hit").style.visibility = "hidden";`)800 w.Eval(`document.getElementById("stand").style.visibility = "hidden";`)801 w.Eval(`document.getElementById("double").style.visibility = "hidden";`)802 w.Eval(`document.getElementById("split").style.visibility = "hidden";`)803 w.Dispatch(func() {804 w.Eval(`document.getElementById("dealer").getElementsByTagName("p")[0].innerHTML += ",` + dealersHand.Cards[1].ToStr() + `";`)805 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)806 })807 }808}809func split(w webview.WebView) {810 if turn == "player1's turn" {811 if player1Hand.CanSplit() {812 player1Split = player1Hand.Split()813 player1Hand.HasSplit = true814 w.Eval(`document.getElementById("player1").getElementsByClassName("splithand")[0].style.visibility = "visible";`)815 w.Dispatch(func() {816 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)817 w.Eval(`document.getElementById("player1").getElementsByTagName("p")[0].innerHTML = "` + player1Hand.ToStr() + `";`)818 w.Eval(`document.getElementById("player1").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Wallet) + `";`)819 w.Eval(`document.getElementById("player1").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player1Hand.Bet) + `";`)820 w.Eval(`document.getElementById("player1").getElementsByClassName("splithand")[0].innerHTML = "` + player1Split.ToStr() + `";`)821 })822 } else {823 w.Dialog(webview.DialogTypeAlert, 0, "Player1", "Player1 can not split!\n")824 }825 } else if turn == "player2's turn" {826 if player2Hand.CanSplit() {827 player2Split = player2Hand.Split()828 player2Hand.HasSplit = true829 w.Eval(`document.getElementById("player2").getElementsByClassName("splithand")[0].style.visibility = "visible";`)830 w.Dispatch(func() {831 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)832 w.Eval(`document.getElementById("player2").getElementsByTagName("p")[0].innerHTML = "` + player2Hand.ToStr() + `";`)833 w.Eval(`document.getElementById("player2").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Wallet) + `";`)834 w.Eval(`document.getElementById("player2").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player2Hand.Bet) + `";`)835 w.Eval(`document.getElementById("player2").getElementsByClassName("splithand")[0].innerHTML = "` + player2Split.ToStr() + `";`)836 })837 } else {838 w.Dialog(webview.DialogTypeAlert, 0, "Player2", "Player2 can not split!\n")839 }840 } else if turn == "player3's turn" {841 if player3Hand.CanSplit() {842 player3Split = player3Hand.Split()843 player3Hand.HasSplit = true844 w.Eval(`document.getElementById("player3").getElementsByClassName("splithand")[0].style.visibility = "visible";`)845 w.Dispatch(func() {846 w.Eval(`document.getElementById("turn").innerHTML = "` + turn + `";`)847 w.Eval(`document.getElementById("player3").getElementsByTagName("p")[0].innerHTML = "` + player3Hand.ToStr() + `";`)848 w.Eval(`document.getElementById("player3").getElementsByClassName("wallet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Wallet) + `";`)849 w.Eval(`document.getElementById("player3").getElementsByClassName("bet")[0].innerHTML = "` + strconv.Itoa(player3Hand.Bet) + `";`)850 w.Eval(`document.getElementById("player3").getElementsByClassName("splithand")[0].innerHTML = "` + player3Split.ToStr() + `";`)851 })852 } else {853 w.Dialog(webview.DialogTypeAlert, 0, "Player3", "Player3 can not split!\n")854 }855 } else {856 w.Dialog(webview.DialogTypeAlert, 0, "Players", "Can not split twice!\n")857 }858}...

Full Screen

Full Screen

art 5.go

Source:art 5.go Github

copy

Full Screen

1package main2import (3 "bytes"4 "errors"5 "fmt"6 "html/template"7 "log"8 "net/http"9 "strings"10 "golang.org/x/net/html"11)12const domain = "https://life.ru"13func getAttribute(node *html.Node, name string) (string, error) {14 for _, attr := range node.Attr {15 if attr.Key == name {16 return attr.Val, nil17 }18 }19 return "", errors.New("No such attribute")20}21func getClassNames(node *html.Node) []string {22 val, err := getAttribute(node, "class")23 if err != nil {24 return nil25 }26 return strings.Split(val, " ")27}28func hasClassName(node *html.Node, name string) bool {29 names := getClassNames(node)30 for _, n := range names {31 if n == name {32 return true33 }34 }35 return false36}37func getElementsByClassName(node *html.Node, name string) []*html.Node {38 var nodes []*html.Node39 if hasClassName(node, name) {40 nodes = append(nodes, node)41 }42 for c := node.FirstChild; c != nil; c = c.NextSibling {43 cn := getElementsByClassName(c, name)44 nodes = append(nodes, cn...)45 }46 return nodes47}48func getElementsByType(node *html.Node, name string) []*html.Node {49 var nodes []*html.Node50 if node.Data == name {51 nodes = append(nodes, node)52 }53 for c := node.FirstChild; c != nil; c = c.NextSibling {54 cn := getElementsByType(c, name)55 nodes = append(nodes, cn...)56 }57 return nodes58}59type item struct {60 Title string61 Preview *preview62 HasPreview bool63 Date string64 Link string65}66type article struct {67 Title string68 Content string69}70type preview struct {71 Url string72 IsVideo bool73}74func parsePreview(node *html.Node) *preview {75 imgs := getElementsByType(node, "img")76 videos := getElementsByType(node, "video")77 if len(imgs) > 0 {78 src, _ := getAttribute(imgs[0], "src")79 return &preview{80 Url: src,81 IsVideo: false,82 }83 }84 src, _ := getAttribute(videos[0], "src")85 return &preview{86 Url: src,87 IsVideo: true,88 }89}90func parseMain(document *html.Node) []item {91 blocks := getElementsByClassName(document, "styles_root__2aHN8")92 var res []item93 for _, b := range blocks {94 previewBlock := getElementsByClassName(b, "styles_imgWrapper__3XFTR")95 fmt.Println(previewBlock)96 var prev *preview97 if len(previewBlock) > 0 {98 prev = parsePreview(previewBlock[0])99 }100 headerBlock := getElementsByClassName(b, "styles_title__VjSwt")[0]101 dateBlock := getElementsByClassName(b, "styles_date__1zS9H")[0]102 link, _ := getAttribute(b, "href")103 i := item{104 Title: headerBlock.FirstChild.FirstChild.Data,105 Date: dateBlock.FirstChild.Data,106 Preview: prev,107 HasPreview: prev != nil,108 Link: link,109 }110 res = append(res, i)111 }112 return res113}114func parseBlock(n *html.Node) string {115 if n.FirstChild == nil {116 return n.Data117 }118 res := ""119 for c := n.FirstChild; c != nil; c = c.NextSibling {120 res += parseBlock(c)121 }122 return res123}124func parseArticle(document *html.Node) article {125 blocks := getElementsByClassName(document, "styles_text__fxCxY") // этот класс только внутри p, а в них нет картинок, поэтому firChild == nil только у текста126 cur := ""127 for _, b := range blocks {128 cur += parseBlock(b)129 }130 titleBlock := getElementsByClassName(document, "styles_title__2F4Y1")[0]131 return article{132 Content: cur,133 Title: titleBlock.FirstChild.Data,134 }135}136const mainTemplate = `<!DOCTYPE html>137<html>138 <head>139 <title>Life.ru</title>140 <meta charset="utf-8" />141 </head>142 <body>143 <h1>Life.ru</h1>144 {{ range .items }}145 {{ if .HasPreview }}146 {{ if .Preview.IsVideo }}147 <h2> <a href="{{ .Link }}?video={{ .Preview.Url }}"> {{ .Title }} </a> </h2>148 <video src="{{ .Preview.Url}}" autoplay loop></video>149 {{ else }}150 <h2> <a href="{{ .Link }}?img={{ .Preview.Url }}"> {{ .Title }} </a> </h2>151 <img src="{{ .Preview.Url}}" />152 {{ end }}153 {{ else }}154 <h2> <a href="{{ .Link }}"> {{ .Title }} </a> </h2>155 {{ end }}156 <br />157 {{ .Date }}158 {{ end }}159 </body>160</html>161`162const articleTemplate = `<!DOCTYPE html>163<html>164 <head>165 <title>{{ .article.Title }}</title>166 <meta charset="utf-8" />167 </head>168 <body>169 <h1>{{ .article.Title }}</h1>170 {{ if .hasImage }}171 <img src="{{ .image }}" /> <br />172 {{ else if .hasVideo }}173 <video src="{{ .video }}" autoplay loop /></video> <br />174 {{ end }}175 {{ .article.Content | html }}176 </body>177</html>178`179func main() {180 http.HandleFunc("/p/", func(rw http.ResponseWriter, r *http.Request) {181 p, _ := http.Get(domain + r.URL.Path)182 document, _ := html.Parse(p.Body)183 article := parseArticle(document)184 t, _ := template.New("").Parse(articleTemplate)185 rw.WriteHeader(200)186 b := bytes.NewBufferString("")187 imgUrl := r.URL.Query().Get("img")188 videoUrl := r.URL.Query().Get("video")189 t.Execute(b, map[string] interface{} {190 "article": article,191 "hasImage": imgUrl != "",192 "image": imgUrl,193 "hasVideo": videoUrl != "",194 "video": videoUrl,195 })196 _, err := rw.Write(b.Bytes())197 if err != nil {198 log.Println(err.Error())199 }200 })201 http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {202 p, _ := http.Get(domain)203 document, _ := html.Parse(p.Body)204 items := parseMain(document)205 /*for n, i := range items {206 fmt.Print(n, ")\t")207 fmt.Println(i.Date)208 }*/209 t, _ := template.New("").Parse(mainTemplate)210 rw.WriteHeader(200)211 b := bytes.NewBufferString("")212 t.Execute(b, map[string] interface{} {213 "items": items,214 })215 _, err := rw.Write(b.Bytes())216 if err != nil {217 log.Println(err.Error())218 }219 fmt.Println()220 })221 http.ListenAndServe(":7001", nil)222}...

Full Screen

Full Screen

homepage.go

Source:homepage.go Github

copy

Full Screen

1package bind2import (3 "fmt"4 "time"5 "github.com/webview/webview"6)7// inject js8var js string = `9document.getElementsByClassName("bili-video-card__info--tit")[0].innerHTML = "%s";10document.getElementsByClassName("bili-video-card__info--author")[0].innerHTML = "%s";11document.getElementsByClassName("bili-video-card__info--date")[0].innerHTML = "· %s";12document.getElementsByClassName("bili-video-card__stats--text")[0].innerHTML = "%s";13document.getElementsByClassName("bili-video-card__stats--text")[1].innerHTML = "%s";14document.getElementsByClassName("bili-video-card__stats__duration")[0].innerHTML = "%s";15document.getElementsByClassName("bili-video-card__cover")[0].getElementsByTagName("source")[0].remove();16document.getElementsByClassName("bili-video-card__cover")[0].getElementsByTagName("img")[0].removeAttribute("loading");17document.getElementsByClassName("bili-video-card__cover")[0].getElementsByTagName("img")[0].removeAttribute("onload")18document.getElementsByClassName("bili-video-card__cover")[0].getElementsByTagName("img")[0].src = "%s";19document.getElementsByClassName("bili-video-card__cover")[0].getElementsByTagName("img")[0].alt = "%s";20`21// OpenBilibiliHomePageBind is a bind function for opening bilibili homepage22func OpenBilibiliHomePageBind() interface{} {23 return func(title, author, date, play, star, duration, cover, alt string) error {24 w := webview.NewWindow(false, nil)25 w.SetSize(1366, 768, webview.HintNone)26 w.Navigate("https://www.bilibili.com/")27 w.Dispatch(func() {28 go func() {29 time.Sleep(time.Second * 4)30 w.Eval(fmt.Sprintf(js, title, author, date, play, star, duration, cover, alt))31 }()32 })33 w.Run()34 return nil35 }36}...

Full Screen

Full Screen

GetElementsByClassName

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, err := html.Parse(os.Stdin)4 if err != nil {5 fmt.Fprintf(os.Stderr, "findlinks1: %v6 os.Exit(1)7 }8 for _, link := range ElementByClassName(doc, "link") {9 fmt.Println(link)10 }11}12func ElementByClassName(n *html.Node, className string) []string {13 if n.Type == html.ElementNode {14 for _, a := range n.Attr {15 if a.Key == "class" && a.Val == className {16 links = append(links, n.Data)17 }18 }19 }20 for c := n.FirstChild; c != nil; c = c.NextSibling {21 links = append(links, ElementByClassName(c, className)...)22 }23}24import (25func main() {26 doc, err := html.Parse(os.Stdin)27 if err != nil {28 fmt.Fprintf(os.Stderr, "findlinks1: %v29 os.Exit(1)30 }31 for _, link := range ElementByClassName(doc, "link") {32 fmt.Println(link)33 }34}35func ElementByClassName(n *html.Node, className string) []string {36 if n.Type == html.ElementNode {37 for _, a := range n.Attr {38 if a.Key == "class" && a.Val == className {39 links = append(links, n.Data)40 }41 }42 }43 for c := n.FirstChild; c != nil; c = c.NextSibling {44 links = append(links, ElementByClassName(c, className)...)45 }46}47import (48func main() {49 doc, err := html.Parse(os.Stdin)50 if err != nil {51 fmt.Fprintf(os.Stderr, "findlinks1: %v52 os.Exit(1)53 }

Full Screen

Full Screen

GetElementsByClassName

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, err := html.Parse(os.Stdin)4 if err != nil {5 fmt.Fprintf(os.Stderr, "findlinks1: %v6 os.Exit(1)7 }8 for _, link := range visit(nil, doc) {9 fmt.Println(link)10 }11}12func visit(links []string, n *html.Node) []string {13 if n.Type == html.ElementNode && n.Data == "a" {14 for _, a := range n.Attr {15 if a.Key == "href" {16 links = append(links, a.Val)17 }18 }19 }20 for c := n.FirstChild; c != nil; c = c.NextSibling {21 links = visit(links, c)22 }23}24import (25func main() {26 doc, err := html.Parse(os.Stdin)27 if err != nil {28 fmt.Fprintf(os.Stderr, "findlinks1: %v29 os.Exit(1)30 }31 for _, link := range visit(nil, doc) {32 fmt.Println(link)33 }34}35func visit(links []string, n *html.Node) []string {36 if n.Type == html.ElementNode && n.Data == "a" {37 for _, a := range n.Attr {38 if a.Key == "href" {39 links = append(links, a.Val)40 }41 }42 }43 for c := n.FirstChild; c != nil; c = c.NextSibling {44 links = visit(links, c)45 }46}47import (48func main() {49 doc, err := html.Parse(os.Stdin)50 if err != nil {51 fmt.Fprintf(os.Stderr, "findlinks1: %v52 os.Exit(1)53 }54 for _, link := range visit(nil, doc) {55 fmt.Println(link)56 }57}58func visit(links []string, n *html.Node) []string {

Full Screen

Full Screen

GetElementsByClassName

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 defer res.Body.Close()7 if res.StatusCode != 200 {8 log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)9 }10 doc, err := goquery.NewDocumentFromReader(res.Body)11 if err != nil {12 log.Fatal(err)13 }14 doc.Find(".w3-example").Each(func(i int, s *goquery.Selection) {15 fmt.Println(strings.TrimSpace(s.Text()))16 })17}

Full Screen

Full Screen

GetElementsByClassName

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 defer res.Body.Close()7 if res.StatusCode != 200 {8 log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)9 }10 doc, err := goquery.NewDocumentFromReader(res.Body)11 if err != nil {12 log.Fatal(err)13 }14 doc.Find(".gb_P").Each(func(i int, s *goquery.Selection) {15 band := s.Find("a").Text()16 title := s.Find("span").Text()17 fmt.Printf("Review %d: %s - %s18 })19}

Full Screen

Full Screen

GetElementsByClassName

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 defer resp.Body.Close()7 body, err := ioutil.ReadAll(resp.Body)8 if err != nil {9 log.Fatal(err)10 }11 doc, err := html.Parse(strings.NewReader(string(body)))12 if err != nil {13 log.Fatal(err)14 }15 elements := GetElementsByClassName(doc, "post-title")16 for _, element := range elements {17 for _, a := range element.Attr {18 if a.Key == "href" {19 fmt.Println(a.Val)20 }21 }22 }23}24func GetElementsByClassName(n *html.Node, class string) []*html.Node {25 if n.Type == html.ElementNode {26 for _, a := range n.Attr {27 if a.Key == "class" && a.Val == class {28 elements = append(elements, n)29 }30 }31 }32 for c := n.FirstChild; c != nil; c = c.NextSibling {33 elements = append(elements, GetElementsByClassName(c, class)...)34 }35}

Full Screen

Full Screen

GetElementsByClassName

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 defer res.Body.Close()7 doc, err := goquery.NewDocumentFromReader(res.Body)8 if err != nil {9 fmt.Println(err)10 }11 doc.Find("div").Each(func(index int, item *goquery.Selection) {12 class, _ := item.Attr("class")13 if class == "gb_9a gbii" {14 fmt.Println(item.Text())15 }16 })17}

Full Screen

Full Screen

GetElementsByClassName

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, err := html.Parse(strings.NewReader(`<html><head><title>My web page</title></head><body><div class="container"><div class="row"><div class="col-md-4">Column 1</div><div class="col-md-4">Column 2</div><div class="col-md-4">Column 3</div></div></div></body></html>`))4 if err != nil {5 fmt.Println(err)6 }7 elements := GetElementsByClassName(doc, "col-md-4")8 for _, element := range elements {9 fmt.Println(element.Data)10 }11}12func GetElementsByClassName(n *html.Node, class string) []*html.Node {13 if n.Type == html.ElementNode {14 for _, a := range n.Attr {15 if a.Key == "class" && a.Val == class {16 elements = append(elements, n)17 }18 }19 }20 for c := n.FirstChild; c != nil; c = c.NextSibling {21 elements = append(elements, GetElementsByClassName(c, class)...)22 }23}

Full Screen

Full Screen

GetElementsByClassName

Using AI Code Generation

copy

Full Screen

1import (2func GetElementsByClassName(n *html.Node, class string) []*html.Node {3 if n.Type == html.ElementNode {4 for _, a := range n.Attr {5 if a.Key == "class" && strings.Contains(a.Val, class) {6 nodes = append(nodes, n)7 }8 }9 }10 for c := n.FirstChild; c != nil; c = c.NextSibling {11 nodes = append(nodes, GetElementsByClassName(c, class)...)12 }13}14func main() {15 if err != nil {16 log.Fatal(err)17 }18 defer resp.Body.Close()19 doc, err := html.Parse(resp.Body)20 if err != nil {21 log.Fatal(err)22 }23 nodes := GetElementsByClassName(doc, "w3-table-all")24 for _, node := range nodes {25 fmt.Println(node)26 }27}28import (29func GetElementsByTagName(n *html.Node, tag string) []*html.Node {30 if n.Type == html.ElementNode && n.Data == tag {31 nodes = append(nodes, n)32 }33 for c := n.FirstChild; c != nil; c = c.NextSibling {34 nodes = append(nodes, GetElementsByTagName(c, tag)...)35 }36}37func main() {38 if err != nil {39 log.Fatal(err)40 }41 defer resp.Body.Close()42 doc, err := html.Parse(resp.Body)43 if err != nil {44 log.Fatal(err)45 }46 nodes := GetElementsByTagName(doc, "table")47 for _, node := range nodes {48 fmt.Println(node)49 }50}51import (

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful