btnI at the beginning

This commit is contained in:
Nato Boram 2021-10-25 09:33:18 -04:00
parent 43d6f41f1d
commit 98d434061e
No known key found for this signature in database
GPG Key ID: 478E3C64BF88AFFA
2 changed files with 21 additions and 34 deletions

View File

@ -42,11 +42,12 @@
<div class="container"> <div class="container">
<form class="row g-3 text-center mt-5"> <form class="row g-3 text-center mt-5">
<!-- Google --> <!-- Google -->
<label class="display-1 form-label fw-normal" for="input"> <label class="display-1 form-label fw-normal mb-0" for="input">
<span class="text-primary">G</span><span class="text-danger">o</span <span class="text-primary">G</span><span class="text-danger">o</span
><span class="text-warning">o</span><span class="text-primary">g</span ><span class="text-warning">o</span><span class="text-primary">g</span
><span class="text-success">l</span><span class="text-danger">e</span> ><span class="text-success">l</span><span class="text-danger">e</span>
</label> </label>
<small class="text-muted mt-0">Let me Google that for you</small>
<!-- Input --> <!-- Input -->
<div <div
@ -70,27 +71,22 @@
name="search" name="search"
placeholder="Search" placeholder="Search"
required required
title="Search"
type="text" type="text"
/> />
</div> </div>
<!-- Actions --> <!-- Actions -->
<div> <div>
<button <button class="btn btn-light" id="search" type="submit">
class="btn btn-light"
id="search"
onclick="onClickSearch()"
type="submit"
>
Google Search Google Search
</button> </button>
<button <button
class="btn btn-light" class="btn btn-light"
id="lucky" id="lucky"
onclick="onClickLucky()" name="lucky"
title="Open the first search result" title="Open the first search result"
type="button" type="submit"
value="true"
> >
I'm feeling lucky I'm feeling lucky
</button> </button>

View File

@ -1,6 +1,6 @@
"use strict" "use strict"
/** @type {{search?: string; lucky?: boolean;}} */ /** @type {{search: string; lucky?: boolean;}} */
const query = window.location.search const query = window.location.search
.substr(1) .substr(1)
.split("&") .split("&")
@ -40,11 +40,9 @@ window.addEventListener("load", async () => {
await setMessage("Come on", "Was it really that hard?", "alert-success") await setMessage("Come on", "Was it really that hard?", "alert-success")
await new Promise(resolve => setTimeout(resolve, 3000)) await new Promise(resolve => setTimeout(resolve, 3000))
window.location.href = encodeURI( window.location.href = `https://www.google.com/search?${
`https://www.google.com/search?q=${query.search}${ query.lucky ? "btnI&" : ""
query.lucky ? "&btnI" : "" }q=${query.search}`
}`
)
}) })
function makeCursor() { function makeCursor() {
@ -55,6 +53,11 @@ function makeCursor() {
return cursor return cursor
} }
/**
* Move the cursor to the targeted element
* @param {HTMLSpanElement} cursor
* @param {HTMLButtonElement} target
*/
async function move(cursor, target) { async function move(cursor, target) {
return new Promise(resolve => { return new Promise(resolve => {
const diffX = const diffX =
@ -93,6 +96,12 @@ async function write() {
} }
} }
/**
* Set the message box under the search buttons.
* @param {string} heading
* @param {string} content
* @param {string} type
*/
async function setMessage(heading, content, type = "alert-primary") { async function setMessage(heading, content, type = "alert-primary") {
const message = document.getElementById("message") const message = document.getElementById("message")
@ -108,21 +117,3 @@ async function setMessage(heading, content, type = "alert-primary") {
message.classList.remove("opacity-0") message.classList.remove("opacity-0")
await new Promise(resolve => setTimeout(resolve, 300)) await new Promise(resolve => setTimeout(resolve, 300))
} }
// eslint-disable-next-line no-unused-vars
function onClickSearch() {
onClickButton(false)
}
// eslint-disable-next-line no-unused-vars
function onClickLucky() {
onClickButton(true)
}
function onClickButton(lucky = false) {
if (!input.validity.valid) return
const url = new URL(window.location.href)
url.searchParams.set("search", input.value.trim())
if (lucky) url.searchParams.set("lucky", "true")
window.location.href = url.href
}