Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid auto-disconnect in Firefox #324

Merged
merged 2 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/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 = '';
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/pull/324
window.gtInterval ??= setInterval(() => {
chrome.runtime.sendMessage({
code: 'Keep alive',
});
}, 5000);