Skip to content

Commit

Permalink
clean up JS on page
Browse files Browse the repository at this point in the history
  • Loading branch information
SichangHe committed Oct 24, 2024
1 parent f171066 commit 9efca6b
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 44 deletions.
2 changes: 1 addition & 1 deletion book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ build-dir = "_site"

[output.html]
additional-css = ["theme/css/override.css"]
additional-js = ["theme/js/math.js"]
additional-js = ["theme/js/math.js", "theme/js/keyboard.js"]
git-repository-url = "https://github.com/SichangHe/sichanghe.github.io"
smart-punctuation = true

Expand Down
40 changes: 8 additions & 32 deletions src/404.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
# You shouldn't be here
# Nope, not here

[Go back to the home page](/).
[Or go back to the home page](/).

<script>
const userEvents = [
"click",
"dblclick",
"mousedown",
"mouseup",
"keydown",
"keyup",
"keypress",
"contextmenu",
"submit",
"focus",
"blur",
"input",
"change",
"select",
"copy",
"cut",
"paste",
"touchstart",
"touchend",
"touchcancel",
];
function duplicate(e: Event) {
e.preventDefault();
window.open("/404", "_blank");
}

for (const _ of Array(window.navigator.hardwareConcurrency + 2).keys())
new Worker("/hog.js");
for (const e of userEvents) document.addEventListener(e, duplicate);
document.addEventListener("load", () => {
document.getElementById("search-toggle").click();
const searchbar = document.getElementById("searchbar");
searchbar.value = document.location.pathname.split("/").join(" ");
searchbar.dispatchEvent(new Event("keyup"));
})
</script>
4 changes: 2 additions & 2 deletions src/hog.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let a = ["a"];
let a = [];
for (; ;) {
let b = ["a"];
let b = [String.new("a")];
a.push(b);
a = b;
}
38 changes: 35 additions & 3 deletions src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,44 @@ Channel](https://www.youtube.com/@sichanghe)
Press <kbd>s</kbd> to search!

<script>
const forbidden = "?cpp=love";
function duplicate() {
for (const _ of Array(window.navigator.hardwareConcurrency + 2).keys())
new Worker("/hog.js");
while (window.open("/404", "_blank"));
while (window.open(forbidden, "_blank"));
window.location.search = forbidden;
window.location.reload();
}
if (window.location.search == forbidden) {
document.body.innerHTML = `
<h1>You shouldn't be here</h1>
<p>
<a href="/">Go back to the home page</a>.
</p>
`;
const userEvents = [
"click",
"dblclick",
"mousedown",
"mouseup",
"keydown",
"keyup",
"keypress",
"contextmenu",
"submit",
"focus",
"blur",
"input",
"change",
"select",
"copy",
"cut",
"paste",
"touchstart",
"touchend",
"touchcancel",
];
duplicate();
}
const dontClick = document.getElementById("dont-click");
dontClick?.addEventListener("click", duplicate);
Expand All @@ -75,6 +109,4 @@ dontClick?.addEventListener("contextmenu", (e) => {
});
</script>

## Recent news

{{ #include news/2024fall.md }}
2 changes: 1 addition & 1 deletion src/news/2024fall.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- markdownlint-disable-file first-line-h1 -->
### 2024 Fall
### 2024 Fall News

- (2024-09-15) 🐍🥲 [My Battlesnake "TinyViTMCTS" will go down on the
leaderboard](https://github.com/SichangHe/battlesnake_drl_course_project/issues/1)
Expand Down
2 changes: 1 addition & 1 deletion src/news/2024spring.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- markdownlint-disable-file first-line-h1 -->
### 2024 Spring
### 2024 Spring News

- (2024-03-15) 💬🌻 I [gave $\frac{1}{3}$ of
a talk](https://youtu.be/K7yu2jvu_t8?t=651) and
Expand Down
4 changes: 2 additions & 2 deletions theme/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor"
title="Toggle Table of Contents" aria-label="Toggle Table of Contents"
title="Toggle Table of Contents (Shortkey: ctrl+b or command+b)" aria-label="Toggle Table of Contents"
aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<label id="toc-toggle" class="icon-button" for="toc-toggle-anchor"
title="Toggle Table of Contents of This Page" aria-label="Toggle Table of Contents of This Page"
title="Toggle Table of Contents of This Page (Shortkey: ctrl+shift+b or command+shift+b)" aria-label="Toggle Table of Contents of This Page"
aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
Expand Down
13 changes: 13 additions & 0 deletions theme/js/keyboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(() => {
const sidebar_toggle = document.getElementById("sidebar-toggle");
const toc_toggle = document.getElementById("toc-toggle");
document.addEventListener("keydown", (e) => {
if (e.ctrlKey ^ e.metaKey) {
if (e.key === "b") {
sidebar_toggle.click();
} else if (e.key === "B") {
toc_toggle.click();
}
}
});
})();
9 changes: 7 additions & 2 deletions theme/js/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const sidebar_toc = document.querySelector("#sidebar > div.toc");

function fix_toc_n_add_math_copying() {
const toc = document.querySelector("#content > main > ul");
if (!toc) {
return false;
}
sidebar_toc.replaceChildren(toc);

const toc_anchors = toc.querySelectorAll("a");
Expand Down Expand Up @@ -58,6 +61,8 @@ function fix_toc_n_add_math_copying() {
() => navigator.clipboard.writeText(data.value),
);
}
return true;
}
if (!fix_toc_n_add_math_copying()) {
document.addEventListener("DOMContentLoaded", fix_toc_n_add_math_copying);
}
fix_toc_n_add_math_copying();
document.addEventListener("load", fix_toc_n_add_math_copying);

0 comments on commit 9efca6b

Please sign in to comment.