Skip to content

Commit 5e8ae76

Browse files
committed
proj: minor ref
1 parent 54a600b commit 5e8ae76

7 files changed

Lines changed: 72 additions & 30 deletions

File tree

src/components/ScrollToTopButton.vue

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script setup lang="ts">
22
import { ref, onMounted, onUnmounted, computed } from "vue";
33
4+
const CIRCUMFERENCE = 2 * Math.PI * 18;
5+
46
const scrollTop = ref(0);
57
const scrollHeight = ref(0);
68
const clientHeight = ref(0);
@@ -12,15 +14,21 @@ const progress = computed(() => {
1214
return (scrollTop.value / (scrollHeight.value - clientHeight.value)) * 100;
1315
});
1416
15-
const circumference = 2 * Math.PI * 18;
1617
const strokeDashoffset = computed(() => {
17-
return circumference - (progress.value / 100) * circumference;
18+
return CIRCUMFERENCE - (progress.value / 100) * CIRCUMFERENCE;
1819
});
1920
21+
let ticking = false;
22+
2023
function onScroll() {
21-
scrollTop.value = document.documentElement.scrollTop;
22-
scrollHeight.value = document.documentElement.scrollHeight;
23-
clientHeight.value = document.documentElement.clientHeight;
24+
if (ticking) return;
25+
ticking = true;
26+
requestAnimationFrame(() => {
27+
scrollTop.value = document.documentElement.scrollTop;
28+
scrollHeight.value = document.documentElement.scrollHeight;
29+
clientHeight.value = document.documentElement.clientHeight;
30+
ticking = false;
31+
});
2432
}
2533
2634
function scrollToTop() {
@@ -61,7 +69,7 @@ onUnmounted(() => {
6169
r="18"
6270
cx="22"
6371
cy="22"
64-
:stroke-dasharray="circumference"
72+
:stroke-dasharray="CIRCUMFERENCE"
6573
:stroke-dashoffset="strokeDashoffset"
6674
/>
6775
</svg>

src/components/SettingsPanel.vue

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,33 @@ import { isReleasePage } from "../utils/release-sorter";
44
55
const open = ref(false);
66
const onReleasePage = ref(isReleasePage());
7+
const settingsRef = ref<HTMLElement>();
78
8-
let checkInterval: ReturnType<typeof setInterval>;
9+
function checkPage() {
10+
onReleasePage.value = isReleasePage();
11+
}
12+
13+
function onClickOutside(e: MouseEvent) {
14+
if (open.value && settingsRef.value && !settingsRef.value.contains(e.target as Node)) {
15+
open.value = false;
16+
}
17+
}
918
1019
onMounted(() => {
11-
checkInterval = setInterval(() => {
12-
onReleasePage.value = isReleasePage();
13-
}, 1000);
20+
document.addEventListener("turbo:load", checkPage);
21+
window.addEventListener("popstate", checkPage);
22+
document.addEventListener("click", onClickOutside);
1423
});
1524
1625
onUnmounted(() => {
17-
clearInterval(checkInterval);
26+
document.removeEventListener("turbo:load", checkPage);
27+
window.removeEventListener("popstate", checkPage);
28+
document.removeEventListener("click", onClickOutside);
1829
});
1930
</script>
2031

2132
<template>
22-
<div v-if="onReleasePage" class="sp-settings">
33+
<div v-if="onReleasePage" ref="settingsRef" class="sp-settings">
2334
<Transition name="sp-panel">
2435
<div v-if="open" class="sp-panel" @wheel.stop>
2536
<slot />

src/main.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import { initReleaseSorter } from "./utils/release-sorter";
55

66
initReleaseSorter();
77

8-
createApp(App).mount(
9-
(() => {
10-
const app = document.createElement("div");
11-
document.body.append(app);
12-
return app;
13-
})(),
14-
);
8+
const app = document.createElement("div");
9+
document.body.append(app);
10+
createApp(App).mount(app);

src/style.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#app {
2-
all: unset;
2+
position: static;
3+
display: contents;
4+
margin: 0;
5+
padding: 0;
6+
border: none;
7+
background: none;
8+
font: inherit;
9+
color: inherit;
310
}
411

512
.better-gh-matched {

src/utils/release-sorter.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,18 @@ export function detectArch(): Exclude<ArchOption, "auto"> {
120120
const ua = navigator.userAgent.toLowerCase();
121121
if (/arm64|aarch64/.test(ua)) return "arm64";
122122
if (/arm/.test(ua)) return "arm";
123-
if (/x86/.test(ua) || /i386|i686/.test(ua)) return "x86";
123+
if (/x86(?!_64)/.test(ua) || /i386|i686/.test(ua)) return "x86";
124124
return "x64";
125125
}
126126

127+
function resolveOs(os: OsOption): Exclude<OsOption, "auto"> {
128+
return os === "auto" ? detectPlatform() : os;
129+
}
130+
131+
function resolveArch(arch: ArchOption): Exclude<ArchOption, "auto"> {
132+
return arch === "auto" ? detectArch() : arch;
133+
}
134+
127135
export function getSelectedArch(): ArchOption {
128136
const saved = GM_getValue(ARCH_STORAGE_KEY, "auto");
129137
if (ARCH_OPTIONS.includes(saved as ArchOption)) return saved as ArchOption;
@@ -144,15 +152,22 @@ export function setSelectedPkg(pkg: PkgOption): void {
144152
GM_setValue(PKG_STORAGE_KEY, pkg);
145153
}
146154

155+
let keywordCache: Set<string> | null = null;
156+
let keywordCacheKey = "";
157+
158+
const regexCache = new Map<string, RegExp>();
159+
147160
function buildMatchKeywords(): Set<string> {
148161
const os = getSelectedOs();
149162
const arch = getSelectedArch();
150163
const pkg = getSelectedPkg();
164+
const cacheKey = `${os}:${arch}:${pkg}`;
165+
if (keywordCache && keywordCacheKey === cacheKey) return keywordCache;
151166

152-
const resolvedOs = os === "auto" ? detectPlatform() : os;
167+
const resolvedOs = resolveOs(os);
153168
const keywords = new Set(SYSTEM_KEYWORDS[resolvedOs]);
154169

155-
const resolvedArch = arch === "auto" ? detectArch() : arch;
170+
const resolvedArch = resolveArch(arch);
156171
for (const kw of ARCH_KEYWORDS[resolvedArch]) {
157172
keywords.add(kw);
158173
}
@@ -163,6 +178,8 @@ function buildMatchKeywords(): Set<string> {
163178
for (const kw of PKG_KEYWORDS.portable[resolvedOs]) keywords.add(kw);
164179
}
165180

181+
keywordCache = keywords;
182+
keywordCacheKey = cacheKey;
166183
return keywords;
167184
}
168185

@@ -171,15 +188,15 @@ export function getKeywordsPreview(
171188
arch: ArchOption,
172189
pkg: PkgOption,
173190
): string[] {
174-
const resolvedOs = os === "auto" ? detectPlatform() : os;
175-
const resolvedArch = arch === "auto" ? detectArch() : arch;
191+
const resolvedOs = resolveOs(os);
192+
const resolvedArch = resolveArch(arch);
176193

177194
const result = [
178195
...SYSTEM_KEYWORDS[resolvedOs],
179196
...ARCH_KEYWORDS[resolvedArch],
197+
...PKG_KEYWORDS.installer[resolvedOs],
180198
];
181199

182-
result.push(...PKG_KEYWORDS.installer[resolvedOs]);
183200
if (pkg === "all") {
184201
result.push(...PKG_KEYWORDS.portable[resolvedOs]);
185202
} else {
@@ -211,9 +228,12 @@ function extractFilename(anchor: HTMLAnchorElement): string | null {
211228
function countMatches(filename: string, keywords: Set<string>): number {
212229
let count = 0;
213230
for (const keyword of keywords) {
214-
if (new RegExp(`(^|[^a-z0-9])${keyword}([^a-z0-9]|$)`).test(filename)) {
215-
count++;
231+
let re = regexCache.get(keyword);
232+
if (!re) {
233+
re = new RegExp(`(^|[^a-z0-9])${keyword}([^a-z0-9]|$)`);
234+
regexCache.set(keyword, re);
216235
}
236+
if (re.test(filename)) count++;
217237
}
218238
return count;
219239
}

tsconfig.app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
4-
"target": "ES2020",
4+
"target": "ES2022",
55
"useDefineForClassFields": true,
66
"module": "ESNext",
77
"lib": ["ES2020", "DOM", "DOM.Iterable"],

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default defineConfig({
1111
userscript: {
1212
name: "Better Github",
1313
description: "一个提升 GitHub 浏览体验的用户脚本",
14-
icon: "https://vitejs.dev/logo.svg",
14+
icon: "https://github.com/favicon.ico",
1515
namespace: "npm/vite-plugin-monkey",
1616
match: ["https://github.com/*"],
1717
grant: ["GM_addStyle", "GM_getValue", "GM_setValue"],

0 commit comments

Comments
 (0)