Skip to content

Commit

Permalink
this is more accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
deedeeh committed Jan 8, 2024
1 parent 51ffe61 commit fa9066f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion page/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ html(lang="en-GB")
button(id="copy-link") Copy link
p Powered by Javascript package 
a(href="https://github.com/omrilotan/isbot" rel="noopener noreferrer") isbot
script(src="./script.js")
script(src="./script.ts")
43 changes: 27 additions & 16 deletions page/script.js → page/script.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { isbot, isbotMatch, isbotPattern } from "..";
import { isbot, isbotMatch, isbotPatterns } from "..";

{
const textarea = document.querySelector("textarea");
const output = document.querySelector("output");
const copyLink = document.querySelector('[id="copy-link"]');
let timer;
const textarea = document.querySelector("textarea") as HTMLTextAreaElement;
const output = document.querySelector("output") as HTMLOutputElement;
const copyLink = document.querySelector(
'[id="copy-link"]',
) as HTMLButtonElement;
let timer: ReturnType<typeof setTimeout>;

const url = new URL(window.location.href);
const ua = url.searchParams.get("ua");
Expand All @@ -16,28 +18,37 @@ import { isbot, isbotMatch, isbotPattern } from "..";
textarea.addEventListener("focus", () => textarea.select());
check();

function change({ target: { value } }) {
function change({ target }: Event): void {
const { value } = target as HTMLTextAreaElement;
clearTimeout(timer);
timer = setTimeout(check, 200, value);
}

function append(parent, tag, string) {
function append(
parent: DocumentFragment,
tag: string | null,
string: string | boolean | null,
): void {
if (tag) {
const ele = document.createElement("kbd");
ele.appendChild(document.createTextNode(string));
ele.appendChild(document.createTextNode(`${string}`));
parent.appendChild(ele);
} else {
parent.appendChild(document.createTextNode(string));
parent.appendChild(document.createTextNode(`${string}`));
}
}

function details(ua) {
function details(ua: string): DocumentFragment {
const fragment = document.createDocumentFragment();
const is = isbot(ua);
const found = is && isbotMatch(ua);
const pattern = is && isbotPattern(ua);

if (is) {
const found = isbotMatch(ua) as string;
const patterns = isbotPatterns(ua);
const pattern = patterns.find((pattern: string): boolean =>
new RegExp(pattern, "i").test(found),
) as string;
console.log(patterns, pattern);
append(fragment, null, "I think so, yes\n");
append(fragment, null, "The substring ");
append(fragment, "kbd", found);
Expand All @@ -53,7 +64,7 @@ import { isbot, isbotMatch, isbotPattern } from "..";
return fragment;
}

function check(value = textarea.innerHTML) {
function check(value = textarea.innerHTML): void {
value = value.trim();
while (output.firstChild) {
output.removeChild(output.firstChild);
Expand All @@ -68,7 +79,7 @@ import { isbot, isbotMatch, isbotPattern } from "..";
output.appendChild(details(value));
}

copyLink.addEventListener("click", (event) => {
copyLink.addEventListener("click", (event: Event): void => {
event.preventDefault();
const { protocol, host, pathname } = document.location;
navigator.clipboard.writeText(
Expand All @@ -85,12 +96,12 @@ import { isbot, isbotMatch, isbotPattern } from "..";
dialog.appendChild(document.createTextNode("copied to clipboard"));
document.body.appendChild(dialog);
dialog.showModal();
setTimeout(() => {
setTimeout((): void => {
dialog.addEventListener("transitionend", () => {
dialog.close();
document.body.removeChild(dialog);
});
dialog.style.opacity = 0;
dialog.style.opacity = "0";
}, 1000);
});
}

0 comments on commit fa9066f

Please sign in to comment.