Skip to content

Instantly share code, notes, and snippets.

@azu
Last active July 27, 2023 09:48
Show Gist options
  • Save azu/f6ca3e8d7fdebc729ecf41dccc48c430 to your computer and use it in GitHub Desktop.
Save azu/f6ca3e8d7fdebc729ecf41dccc48c430 to your computer and use it in GitHub Desktop.
Bluesky: auto refresh
// ==UserScript==
// @name Bluesky: auto refresh
// @namespace https://efcl.info
// @match https://bsky.app/*
// @grant none
// @version 1.0
// @author azu
// @description Auto Refresh when notifications is shown
// ==/UserScript==
const origSetInterval = window.setInterval;
// refresh 15sec === refresh fn
const isBskyRefresh = (fn, ms) => {
const source = fn.toString();
return source.includes(".updateSessionState()") && ms === 15e3;
};
const addRefreshCallForPageVisible = (refreshFn) => {
document.addEventListener("visibilitychange", () => {
if (!document.hidden) {
try {
refreshFn();
document.querySelector('[aria-label="Load new posts"]')?.click();
} catch(error){
console.error("[GM] refresh function call is falled", {
error
})
} finally {
console.log("[GM] call auto refresh")
}
}
});
};
window.setInterval = (fn, ms) => {
if(isBskyRefresh(fn,ms)){
addRefreshCallForPageVisible(fn); // when page is shown, call refresh function
}
origSetInterval.call(this, fn, ms);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment