diff --git a/src/content-scripts/shortcuts.js b/src/content-scripts/shortcuts.js new file mode 100644 index 000000000..f7612aa52 --- /dev/null +++ b/src/content-scripts/shortcuts.js @@ -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; \ No newline at end of file diff --git a/tests/shortcuts.test.js b/tests/shortcuts.test.js new file mode 100644 index 000000000..c14c49634 --- /dev/null +++ b/tests/shortcuts.test.js @@ -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.'); +}); \ No newline at end of file