Skip to content

Commit 6da5224

Browse files
committed
allow load scripts on page change
1 parent f74f67f commit 6da5224

File tree

7 files changed

+27
-7
lines changed

7 files changed

+27
-7
lines changed

layout/partial/comments.pug

-3
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,13 @@ mixin make_comments
8585
if theme.valine.enable == true
8686
a#comments
8787
#vcomments(style="margin: 0 30px")
88-
script Anatolo.comment.load();
8988

9089
if theme.gitment
9190
if theme.gitment.enable == true
9291
a#comments
9392
#gitment_container(style="padding: 10px")
94-
script Anatolo.comment.load();
9593

9694
if theme.gitalk
9795
if theme.gitalk.enable == true
9896
a#comments
9997
#gitalk_container(style="padding: 10px")
100-
script Anatolo.comment.load();

src/anatolo/anatolo.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { darkLightToggle } from './dark-light-toggle';
44
import { site } from './site';
55

66
async function getPageTitle() {
7-
return (await site.thisPage()).title ?? document.querySelector('title')?.textContent;
7+
return (await site.thisPage())?.title ?? document.querySelector('title')?.textContent ?? '';
88
}
99

1010
// Classes for interacting with elements in static HTML

src/anatolo/comment.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ let config: CommentConfig | null = null;
66

77
export async function load(retry = 3) {
88
if (!config) return;
9+
const id = (await Anatolo.getPageTitle()).slice(0, 50);
10+
if (!id) return;
911
if (config.valine?.enable && (window as any).Valine) {
1012
new (window as any).Valine({
1113
el: '#vcomments',
@@ -21,7 +23,6 @@ export async function load(retry = 3) {
2123
avatar: config.valine.avatar,
2224
});
2325
}
24-
const id = (await Anatolo.getPageTitle()).slice(0, 50);
2526
if (config.gitment?.enable && (window as any).Gitment) {
2627
var git_ment = {
2728
id,
@@ -50,7 +51,7 @@ export async function load(retry = 3) {
5051
}
5152
}
5253

53-
router.onPageChange(load);
54+
router.onPageChange(() => load().catch(() => {}));
5455

5556
export function setConfig(conf: CommentConfig) {
5657
config = conf;

src/anatolo/load-comment.ts

Whitespace-only changes.

src/anatolo/router.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { make_friends_list } from '@/utils/main';
22
import { AnatoloRef } from './ref';
33
import { site } from './site';
4+
import { loadScript } from '@/utils/load-script';
45

56
type RouterState = { url: string; body: string; title: string; scrollY?: number };
67

@@ -212,6 +213,7 @@ export class AnatoloRouter {
212213

213214
handlePage() {
214215
this.scrollToHash();
216+
this.reloadScript();
215217
this.pageChangeFns.forEach((fn) => {
216218
try {
217219
fn();
@@ -229,6 +231,16 @@ export class AnatoloRouter {
229231
onPageChange(fn: () => void) {
230232
this.pageChangeFns.push(fn);
231233
}
234+
235+
reloadScript() {
236+
const body_scripts = document.querySelector('main-outlet')?.getElementsByTagName('script');
237+
for (const script of body_scripts ?? []) {
238+
loadScript({
239+
url: script.src,
240+
script: script.textContent,
241+
});
242+
}
243+
}
232244
}
233245

234246
export const router = new AnatoloRouter();

src/anatolo/site.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ export const site = {
3131

3232
async thisPage() {
3333
await site.data();
34-
return urlMap.get(site.thisPageUrl()) as WebPage;
34+
return urlMap.get(site.thisPageUrl());
3535
},
3636
};

src/utils/load-script.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export async function loadScript(config: { url?: string | null; script?: string | null }) {
2+
if (config.url) {
3+
config.script = await fetch(config.url).then((r) => r.text());
4+
}
5+
if (!config.script) {
6+
return;
7+
}
8+
const runner = new Function(config.script);
9+
runner();
10+
}

0 commit comments

Comments
 (0)