Skip to content

Commit

Permalink
Adding 2 custom events to support userscripts better
Browse files Browse the repository at this point in the history
new_post_hook
-fires 1 event with the new post element for every new post that a user receives. Very useful so userscripts that want to manipulate new posts don't have to mess around with mutationobserver.

site_fully_loaded
-event fires one time when the site fully loaded and finished all js. currently the usual methods to wait for the site to finish loading dont work on kch. for example the (You)s get added after the DOMloaded signal is already sent. I think currently the only way is to set a shitty timer for the script to wait.

There are 4 userscripts that I know of in use right now which could benefit from these 2 events
  • Loading branch information
IUCPROD committed May 29, 2021
1 parent 90426b8 commit f2a954d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app.ts
Expand Up @@ -49,10 +49,13 @@ async function init() {
initPosts();
}
}

initUI();
initAuth();
initProfiles();

// Fire off site fully loaded event for userscripts
document.dispatchEvent(new CustomEvent('site_fully_loaded', {bubbles:true}));
}

init().catch((err) => {
Expand Down
3 changes: 3 additions & 0 deletions ts/client/index.ts
Expand Up @@ -42,6 +42,9 @@ export function insertPost(data: PostData) {
if (options.scrollToBottom && atBottom && !isHoverActive()) {
scrollToBottom();
}
// Fire event to signal userscripts that a new post was added
document.dispatchEvent(new CustomEvent('new_post_hook', {bubbles:true,
detail: {post: view.el}}));
}

export function init() {
Expand Down

0 comments on commit f2a954d

Please sign in to comment.