Skip to content

Commit

Permalink
Keep event page alive
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Aug 10, 2024
1 parent 2bf8f12 commit f470903
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
17 changes: 8 additions & 9 deletions source/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,13 @@ chrome.runtime.onConnect.addListener(handlePortListenerErrors(async port => {

console.log('will open socket');
const socket = new WebSocket('ws://localhost:' + WebSocketPort);
let interval; // eslint-disable-line prefer-const -- Ur rong
await Promise.race([
oneEvent(socket, 'open'),
oneEvent(socket, 'error'),
]);

const onSocketClose = () => {
port.postMessage({close: true});
clearInterval(interval);
};

socket.addEventListener('close', onSocketClose);
Expand All @@ -119,17 +117,18 @@ chrome.runtime.onConnect.addListener(handlePortListenerErrors(async port => {
port.onDisconnect.addListener(() => {
socket.removeEventListener('close', onSocketClose);
socket.close();
clearInterval(interval);
});
port.postMessage({ready: true});

interval = setInterval(() => {
// Keep-alive for MV3 https://github.com/fregante/GhostText/issues/317
port.postMessage({ping: true});
console.log('ping');
}, 5000);
}));

// https://github.com/fregante/GhostText/issues/317
chrome.runtime.onMessage.addListener(() => {
// What is my purpose?
// You pass the butter.
// Oh my god.
// Yeah, welcome to the club, pal.
});

function handleMessages({code, count}, {tab}) {
if (code === 'connection-count') {
let text = '';
Expand Down
7 changes: 7 additions & 0 deletions source/ghost-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,10 @@ window.startGT = startGT;
window.stopGT = stopGT;

setTimeout(startGT, 100);

// https://github.com/fregante/GhostText/issues/317
window.gtInterval ??= setInterval(() => {
chrome.runtime.sendMessage({
code: 'Keep alive',
});
}, 5000);

0 comments on commit f470903

Please sign in to comment.