mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-27 13:29:34 +07:00
Updates a nunber of dependencies in a single PR. These were suggested by dependabot but updating them individually would mean the reposistory would be in a broken state until everything had merged. I'm not personally a huge fan of lowercasing doctype, but Prettier has taken an opinionated stance here and there is not a way to disable it: https://github.com/prettier/prettier/issues/15096
58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
const TEN_SECONDS_MS = 10 * 1000;
|
|
let webSocket = null;
|
|
|
|
// Make sure the Glitch demo server is running
|
|
fetch('https://chrome-extension-websockets.glitch.me/', { mode: 'no-cors' });
|
|
|
|
// Toggle WebSocket connection on action button click
|
|
// Send a message every 10 seconds, the ServiceWorker will
|
|
// be kept alive as long as messages are being sent.
|
|
chrome.action.onClicked.addListener(async () => {
|
|
if (webSocket) {
|
|
disconnect();
|
|
} else {
|
|
connect();
|
|
keepAlive();
|
|
}
|
|
});
|
|
|
|
function connect() {
|
|
webSocket = new WebSocket('wss://chrome-extension-websockets.glitch.me/ws');
|
|
|
|
webSocket.onopen = () => {
|
|
chrome.action.setIcon({ path: 'icons/socket-active.png' });
|
|
};
|
|
|
|
webSocket.onmessage = (event) => {
|
|
console.log(event.data);
|
|
};
|
|
|
|
webSocket.onclose = () => {
|
|
chrome.action.setIcon({ path: 'icons/socket-inactive.png' });
|
|
console.log('websocket connection closed');
|
|
webSocket = null;
|
|
};
|
|
}
|
|
|
|
function disconnect() {
|
|
if (webSocket) {
|
|
webSocket.close();
|
|
}
|
|
}
|
|
|
|
function keepAlive() {
|
|
const keepAliveIntervalId = setInterval(
|
|
() => {
|
|
if (webSocket) {
|
|
console.log('ping');
|
|
webSocket.send('ping');
|
|
} else {
|
|
clearInterval(keepAliveIntervalId);
|
|
}
|
|
},
|
|
// It's important to pick an interval that's shorter than 30s, to
|
|
// avoid that the service worker becomes inactive.
|
|
TEN_SECONDS_MS
|
|
);
|
|
}
|