Skip to content

Commit c5658d0

Browse files
authored
Avoid auto-disconnect in Firefox (#324)
1 parent 2bf8f12 commit c5658d0

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

source/background.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,13 @@ chrome.runtime.onConnect.addListener(handlePortListenerErrors(async port => {
9797

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

106105
const onSocketClose = () => {
107106
port.postMessage({close: true});
108-
clearInterval(interval);
109107
};
110108

111109
socket.addEventListener('close', onSocketClose);
@@ -119,17 +117,18 @@ chrome.runtime.onConnect.addListener(handlePortListenerErrors(async port => {
119117
port.onDisconnect.addListener(() => {
120118
socket.removeEventListener('close', onSocketClose);
121119
socket.close();
122-
clearInterval(interval);
123120
});
124121
port.postMessage({ready: true});
125-
126-
interval = setInterval(() => {
127-
// Keep-alive for MV3 https://github.com/fregante/GhostText/issues/317
128-
port.postMessage({ping: true});
129-
console.log('ping');
130-
}, 5000);
131122
}));
132123

124+
// https://github.com/fregante/GhostText/pull/324
125+
chrome.runtime.onMessage.addListener(() => {
126+
// What is my purpose?
127+
// You pass the butter.
128+
// Oh my god.
129+
// Yeah, welcome to the club, pal.
130+
});
131+
133132
function handleMessages({code, count}, {tab}) {
134133
if (code === 'connection-count') {
135134
let text = '';

source/ghost-text.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,10 @@ window.startGT = startGT;
349349
window.stopGT = stopGT;
350350

351351
setTimeout(startGT, 100);
352+
353+
// https://github.com/fregante/GhostText/pull/324
354+
window.gtInterval ??= setInterval(() => {
355+
chrome.runtime.sendMessage({
356+
code: 'Keep alive',
357+
});
358+
}, 5000);

0 commit comments

Comments
 (0)