How to use GetElementsByTagName method of html Package

Best K6 code snippet using html.GetElementsByTagName

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

document.go

Source:document.go Github

copy

Full Screen

...456 return json.Unmarshal(d.Content(), v)457}458//--------- html-document methods ---------------459func (d *Document) Title() string {460 if ee := d.GetElementsByTagName("title"); len(ee) > 0 {461 return ee[0].InnerText()462 }463 return ""464}465func (d *Document) GetElementsByTagName(name string) HTMLElements {466 return getElementsByTagName(d, name)467}468// Forms gets collections of html-tags <form>469func (d *Document) Forms() HTMLElements {470 return d.GetElementsByTagName("form")471}472// Links gets collections of html-tags <a>473func (d *Document) Links() HTMLElements {474 return d.GetElementsByTagName("a")475}476// Frames gets collections of html-tags <frame>, <iframe>477func (d *Document) Frames() HTMLElements {478 return append(d.GetElementsByTagName("iframe"), d.GetElementsByTagName("frame")...)479}480// Images gets collections of html-tags <img>481func (d *Document) Images() HTMLElements {482 return d.GetElementsByTagName("img")483}484// Scripts gets collections of html-tags <script>485func (d *Document) Scripts() HTMLElements {486 return d.GetElementsByTagName("script")487}488// MetaTags gets collections of html-tags <meta>489func (d *Document) MetaTags() HTMLElements {490 return d.GetElementsByTagName("meta")491}492// MetaDescription gets html-document description from <meta name="description">493func (d *Document) MetaDescription() string {494 if e := d.MetaTags().FilterByAttrValue("name", "description").First(); e != nil {495 return e.Attributes["content"]496 }497 return ""498}499func (d *Document) MetaCSRFToken() string {500 if e := d.MetaTags().FilterByAttrValue("name", "csrf-token").First(); e != nil {501 return e.Attributes["content"]502 }503 return ""504}505// MetaIcon gets image-url from meta-info for html-document506func (d *Document) MetaIcon() string {507 linkTags := d.GetElementsByTagName("link").FilterByAttr("href")508 for _, relVal := range []string{"icon", "shortcut icon", "apple-touch-icon"} {509 if tags := linkTags.FilterByAttrValue("rel", relVal); len(tags) > 0 {510 if tags := tags.FilterByAttrValue("type", "image/ico"); len(tags) > 0 {511 return tags[0].Attributes["href"]512 }513 return tags[0].Attributes["href"]514 }515 }516 return ""517}518// MetaImage gets image-url from meta-info for html-document519func (d *Document) MetaImage() string {520 if tag := d.GetElementsByTagName("link").FilterByAttrValue("rel", "image").FilterByAttr("href").First(); tag != nil {521 return tag.Attributes["href"]522 }523 if tag := d.GetElementsByTagName("link").FilterByAttrValue("rel", "image_src").FilterByAttr("href").First(); tag != nil {524 return tag.Attributes["href"]525 }526 if tag := d.GetElementsByTagName("meta").FilterByAttrValue("property", "og:image").FilterByAttr("content").First(); tag != nil {527 return tag.Attributes["content"]528 }529 return ""530}...

Full Screen

Full Screen

getElementsByTagname.go

Source:getElementsByTagname.go Github

copy

Full Screen

1// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.2// License: https://creativecommons.org/licenses/by-nc-sa/4.0/3// See page 133.4package main5import (6 "fmt"7 "net/http"8 "os"9 "golang.org/x/net/html"10)11func main() {12 if len(os.Args) < 3 {13 fmt.Println("usage: getElementsByTagName url names")14 os.Exit(1)15 }16 if err := getElementsByTagName(os.Args[1], os.Args[2:]); err != nil {17 fmt.Printf("getElementsByTagName failed: %v\n", err)18 os.Exit(1)19 }20}21func getElementsByTagName(url string, name []string) error {22 resp, err := http.Get(url)23 if err != nil {24 return err25 }26 defer resp.Body.Close()27 if resp.StatusCode != http.StatusOK {28 return fmt.Errorf("Get Failed: %s", resp.Status)29 }30 doc, err := html.Parse(resp.Body)31 if err != nil {32 return err33 }34 node := ElementsByTagName(doc, name...)35 if node == nil {36 fmt.Printf("No element with names given %v found\n", name)37 } else {38 fmt.Printf("Found %d elements with names given %v\n", len(node), name)39 }40 images := ElementsByTagName(doc, "img")41 headings := ElementsByTagName(doc, "h1", "h2", "h3", "h4")42 fmt.Printf("found %d images, %d headings", len(images), len(headings))43 return nil44}45//!+forEachNode46// forEachNode calls the functions pre(x) and post(x) for each node47// x in the tree rooted at n. Both functions are optional.48// pre is called before the children are visited (preorder) and49// post is called after (postorder).50func forEachNode(n *html.Node, pre, post func(n *html.Node)) {51 if pre != nil {52 pre(n)53 }54 for c := n.FirstChild; c != nil; c = c.NextSibling {55 forEachNode(c, pre, post)56 }57 if post != nil {58 post(n)59 }60}61//!-forEachNode62func ElementsByTagName(doc *html.Node, name ...string) []*html.Node {63 var node []*html.Node64 var nameMap = make(map[string]bool, len(name))65 for _, n := range name {66 nameMap[n] = true67 }68 forEachNode(doc, func(n *html.Node) {69 if n.Type == html.ElementNode && nameMap[n.Data] {70 node = append(node, n)71 }72 }, nil)73 return node74}...

Full Screen

Full Screen

GetElementsByTagName

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}

Full Screen

Full Screen

GetElementsByTagName

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Print("error")5 }6 doc.Find("a").Each(func(index int, item *goquery.Selection) {7 url, _ := item.Attr("href")8 fmt.Println(url)9 })10}

Full Screen

Full Screen

GetElementsByTagName

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 doc, err := html.Parse(resp.Body)8 if err != nil {9 log.Fatal(err)10 }11 visitNode(doc)12}13func visitNode(n *html.Node) {14 if n.Type == html.ElementNode && n.Data == "a" {15 for _, a := range n.Attr {16 if a.Key == "href" {17 fmt.Println(a.Val)18 }19 }20 }21 for c := n.FirstChild; c != nil; c = c.NextSibling {22 visitNode(c)23 }24}

Full Screen

Full Screen

GetElementsByTagName

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 panic(err)5 }6 for _, n := range nodes {7 fmt.Println(htmlquery.InnerText(n))8 }9}

Full Screen

Full Screen

GetElementsByTagName

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 if resp.StatusCode != http.StatusOK {8 log.Fatal(resp.Status)9 }10 doc, err := html.Parse(resp.Body)11 if err != nil {12 log.Fatal(err)13 }14 var f func(*html.Node)15 f = func(n *html.Node) {16 if n.Type == html.ElementNode && n.Data == "a" {17 for _, a := range n.Attr {18 if a.Key == "href" {19 link, err := resp.Request.URL.Parse(a.Val)20 if err != nil {21 }22 if link.Host != resp.Request.URL.Host {23 }24 fmt.Println(link)25 }26 }27 }28 for c := n.FirstChild; c != nil; c = c.NextSibling {29 f(c)30 }31 }32 f(doc)33}

Full Screen

Full Screen

GetElementsByTagName

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Print("Enter the URL: ")4 fmt.Scan(&url)5 }6 resp, err := http.Get(url)7 if err != nil {8 fmt.Println("ERROR: ", err)9 }10 defer resp.Body.Close()11 body, err := ioutil.ReadAll(resp.Body)12 if err != nil {13 fmt.Println("ERROR: ", err)14 }15 htmlStr := string(body)16 doc, err := html.Parse(strings.NewReader(htmlStr))17 if err != nil {18 fmt.Println("ERROR: ", err)19 }

Full Screen

Full Screen

GetElementsByTagName

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Enter the URL of the web page you want to parse")4 fmt.Scan(&url)5 fmt.Println("Enter the tag name you want to search")6 fmt.Scan(&tag)7 response, err := http.Get(url)8 if err != nil {9 log.Fatal(err)10 }11 defer response.Body.Close()12 if response.StatusCode != 200 {13 log.Fatalf("status code error: %d %s", response.StatusCode, response.Status)14 }15 doc, err := goquery.NewDocumentFromReader(response.Body)16 if err != nil {17 log.Fatal(err)18 }19 doc.Find(tag).Each(func(index int, item *goquery.Selection) {20 fmt.Println(strings.TrimSpace(item.Text()))21 })22}

Full Screen

Full Screen

GetElementsByTagName

Using AI Code Generation

copy

Full Screen

1import (2type htmlPage struct {3}4func (h *htmlPage) getHTML() string {5 resp, err := http.Get(h.url)6 if err != nil {7 fmt.Println("Error: ", err)8 }9 defer resp.Body.Close()10 body, err := ioutil.ReadAll(resp.Body)11 return string(body)12}13func (h *htmlPage) getElementsByTagName(tagName string) {14 htmlString := h.getHTML()15 doc, err := html.Parse(strings.NewReader(htmlString))16 if err != nil {17 fmt.Println("Error: ", err)18 }19 var f func(*html.Node)20 f = func(n *html.Node) {21 if n.Type == html.ElementNode && n.Data == tagName {22 fmt.Println(n.Data)23 }24 for c := n.FirstChild; c != nil; c = c.NextSibling {25 f(c)26 }27 }28 f(doc)29}30func main() {

Full Screen

Full Screen

GetElementsByTagName

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 defer response.Body.Close()7 contents, err := ioutil.ReadAll(response.Body)8 if err != nil {9 fmt.Println(err)10 }11 str := string(contents)12 doc, err := html.Parse(strings.NewReader(str))13 if err != nil {14 fmt.Println(err)15 }16 elements := GetElementsByTagName(doc, "a")17 for _, element := range elements {18 fmt.Println(element)19 }20}21func GetElementsByTagName(n *html.Node, tagName string) []*html.Node {22 if n == nil {23 }24 if n.Type == html.ElementNode && n.Data == tagName {25 return []*html.Node{n}26 }27 elements := GetElementsByTagName(n.FirstChild, tagName)28 elements = append(elements, GetElementsByTagName(n.NextSibling, tagName)...)29}

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