Skip to content
Open
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
35 changes: 35 additions & 0 deletions src/content-scripts/shortcuts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const shortcuts = {
init: function() {
window.addEventListener('keydown', this.handleKeydown.bind(this));
},

isInternetUnstable: false,

handleKeydown: function(event) {
if (this.isInternetUnstable) {
console.warn('Shortcuts are disabled due to unstable internet connection.');
return;
}
// Existing keydown handling logic here
},

handleConnectivityChange: function() {
const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;

if (connection) {
connection.addEventListener('change', () => {
this.isInternetUnstable = connection.downlink < 0.5 || connection.rtt > 300;
if (this.isInternetUnstable) {
console.warn('Internet connection seems unstable. Keyboard shortcuts disabled.');
} else {
console.log('Internet connection is stable. Keyboard shortcuts enabled.');
}
});
}
}
};

shortcuts.init();
shortcuts.handleConnectivityChange();

export default shortcuts;
30 changes: 30 additions & 0 deletions tests/shortcuts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import shortcuts from '../src/content-scripts/shortcuts';

let originalConnection;
beforeAll(() => {
originalConnection = navigator.connection;
Object.defineProperty(navigator, 'connection', { value: { downlink: 1, rtt: 50 }, writable: true });
});

afterAll(() => {
Object.defineProperty(navigator, 'connection', { value: originalConnection, writable: true });
});

test('should disable shortcuts if internet connection becomes unstable', () => {
const mockEvent = new KeyboardEvent('keydown', { key: 'a' });

// Simulate stable connection
navigator.connection.downlink = 1;
navigator.connection.rtt = 50;
shortcuts.handleConnectivityChange();
global.console.warn = jest.fn();
document.dispatchEvent(mockEvent);
expect(console.warn).not.toHaveBeenCalled();

// Simulate unstable connection
navigator.connection.downlink = 0.3;
navigator.connection.rtt = 400;
shortcuts.handleConnectivityChange();
document.dispatchEvent(mockEvent);
expect(console.warn).toHaveBeenCalledWith('Shortcuts are disabled due to unstable internet connection.');
});