diff --git a/README.md b/README.md index 90ea9a4..bbe9fe8 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,12 @@ **Kiwi Browser, Yandex Browser, and other chrome-compatible browsers** +## Usage +You can toggle fullscreen mode by +* click extension icon +* using keyboard combination Alt + M + + ### Install from Chrome Web Store * [Chrome Web Store: Fullscreen](https://chrome.google.com/webstore/detail/fullscreen/lbpgkagpackldbkfookmmdpfaolnoged) diff --git a/init.js b/init.js new file mode 100644 index 0000000..4819f20 --- /dev/null +++ b/init.js @@ -0,0 +1,16 @@ +function injectCode(src) { + const script = document.createElement('script'); + // This is why it works! + script.src = src; + script.onload = function() { + console.log("inpage script injected"); + this.remove(); + }; + + // This script runs before the element is created, + // so we add the script to instead. + document.documentElement.appendChild(script); +} + + +injectCode(chrome.runtime.getURL('/inpage.js')); \ No newline at end of file diff --git a/inpage.js b/inpage.js new file mode 100644 index 0000000..97be54c --- /dev/null +++ b/inpage.js @@ -0,0 +1,41 @@ +var altKeyPressed = false; + +window.addEventListener('keydown', function(event) { + if (event.keyCode === 18) { + // The "Alt" key was pressed + altKeyPressed = true; + } else if (event.keyCode === 77 && altKeyPressed) { + // The "M" key was pressed while the "Alt" key was down + console.log('The "Alt + M" key combination was pressed'); + if (document.fullscreenElement) { + // The current state is fullscreen + console.log('Currently in fullscreen mode'); + var elem = document; + var rfs = + elem.exitFullscreen + || elem.webkitExitFullScreen + || elem.mozExitFullScreen + || elem.msExitFullScreen; + rfs.call(elem); + } else { + // The current state is not fullscreen + console.log('Not currently in fullscreen mode'); + var elem = document.documentElement; + var rfs = + elem.requestFullscreen + || elem.webkitRequestFullScreen + || elem.mozRequestFullScreen + || elem.msRequestFullScreen; + rfs.call(elem); + } + } +}); + +window.addEventListener('keyup', function(event) { + if (event.keyCode === 18) { + // The "Alt" key was released + altKeyPressed = false; + } +}); + +console.log('Keyboard listener added for Alt+M') \ No newline at end of file diff --git a/manifest.json b/manifest.json index 4850374..537c7eb 100644 --- a/manifest.json +++ b/manifest.json @@ -3,10 +3,35 @@ "name": "Fullscreen", "version": "0.0.3", "description": "Fullscreen extension for device without keyboard", - "permissions": ["activeTab", "scripting"], + "permissions": ["activeTab", "scripting", "tabs"], "action": { "default_title": "Click to fullscreen" }, + "content_scripts": [ + { + "matches": [ + "file://*/*", + "http://*/*", + "https://*/*" + ], + "js": [ + "init.js" + ], + "run_at": "document_start", + "all_frames": true + } + ], + "web_accessible_resources": [ + { + "resources": [ + "inpage.js" + ], + "matches": [ + "https://*/*", + "http://*/*" + ] + } + ], "background": { "service_worker": "main.js"