Avoid auto-disconnect in Firefox (#324)

This commit is contained in:
Federico Brigante 2024-08-10 17:27:07 +07:00 committed by GitHub
parent 2bf8f12880
commit c5658d03e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 9 deletions

View File

@ -97,7 +97,6 @@ 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'),
@ -105,7 +104,6 @@ chrome.runtime.onConnect.addListener(handlePortListenerErrors(async port => {
const onSocketClose = () => {
port.postMessage({close: true});
clearInterval(interval);
};
socket.addEventListener('close', onSocketClose);
@ -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/pull/324
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 = '';

View File

@ -349,3 +349,10 @@ window.startGT = startGT;
window.stopGT = stopGT;
setTimeout(startGT, 100);
// https://github.com/fregante/GhostText/pull/324
window.gtInterval ??= setInterval(() => {
chrome.runtime.sendMessage({
code: 'Keep alive',
});
}, 5000);