From f582cbe42d5fe5cb079d82b457fbf3068b32bf75 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Mon, 2 Oct 2017 23:30:40 +0200 Subject: [PATCH] started with keybinds --- js/conf/Keybinds.js | 146 +++++++++++++++++++++++++++++++++++++++ js/conf/keybinds.js | 57 --------------- js/lib/BrowserDetect.js | 12 ++++ js/lib/StorageManager.js | 21 ++++++ manifest.json | 2 +- 5 files changed, 180 insertions(+), 58 deletions(-) create mode 100644 js/conf/Keybinds.js delete mode 100644 js/conf/keybinds.js create mode 100644 js/lib/BrowserDetect.js create mode 100644 js/lib/StorageManager.js diff --git a/js/conf/Keybinds.js b/js/conf/Keybinds.js new file mode 100644 index 0000000..59fe46b --- /dev/null +++ b/js/conf/Keybinds.js @@ -0,0 +1,146 @@ +// Yeah hi /r/badcode. +// Anyway, because nazi localstorage flat out refuses to store arrays: + +var DEFAULT_KEYBINDINGS = { + 0:{ action: "fitw", + key: 'w', + modifiers: [] + }, + 1:{ + action: "fith", + key: 'e', + modifiers: [] + }, + 2: { + action: "reset", + key: 'r', + modifiers: [] + }, + 3: { + action: "zoom", + key: "z", + modifiers: [] + }, + 4: { + action: "unzoom", + key: "u", + modifiers: [] + }, + 5: { + action: "char", + targetAR: (21/9), + key: "d", + modifiers: [] + }, + 6: { + action: "char", + targetAR: (16/9), + key: "s", + modifiers: [] + }, + 7: { + action: "char", + targetAR: (16/10), + key: "x", + modifiers: [] + }, + 8: { + action: "char", + targetAR: (4/3), + key: "c", + modifiers: [] + }, + 9: { + action: "autoar", + key: "a", + modifiers: [] + } +}; + + +// functions + +var _kbd_setup = function() { + StorageManager.getopt("keybinds"); + +} + + + +var _kbd_setup_stage2 = function(){ + if(Debug.debug || Debug.keyboard) + console.log("uw::keydownSetup | starting keybord shortcut setup"); + $(document).keydown(function (event) { // Tukaj ugotovimo, katero tipko smo pritisnili + + // Tipke upoštevamo samo, če smo v celozaslonskem načinu oz. če ne pišemo komentarja + // v nasprotnem primeru ne naredimo nič. + // We only take actions if we're in full screen or not writing a comment + if( !(inFullScreen || ( + (document.activeElement.getAttribute("role") != "textbox") && + (document.activeElement.getAttribute("type") != "text") + ))){ + if(Debug.debug || Debug.keyboard) + console.log("We're writing a comment or something. Doing nothing"); + return; + } + + if(Debug.debug || Debug.keyboard ){ + // console.log(KEYBINDS); + console.log("we pressed a key: ", event.key , " | keydown: ", event.keydown); + if(event.key == 'p'){ + console.log("uw/keydown: attempting to send message") + var sending = browser.runtime.sendMessage({ + type: "debug", + message: "Test message, please ignore" + }); + sending.then( function(){}, function(){console.log("uw/keydown: there was an error while sending a message")} ); + console.log("uw/keydown: test message sent! (probably)"); + return; + } + } + + for(i in KEYBINDS){ + if(Debug.debug || Debug.keyboard) + console.log("i: ", i, "keybinds[i]:", KEYBINDS[i]); + + if(event.key == KEYBINDS[i].key){ + if(Debug.debug || Debug.keyboard) + console.log("Key matches!"); + //Tipka se ujema. Preverimo še modifierje: + //Key matches. Let's check if modifiers match, too: + var mods = true; + for(var j = 0; j < KEYBINDS[i].modifiers.length; j++){ + if(KEYBINDS[i].modifiers[j] == "ctrl") + mods &= event.ctrlKey ; + else if(KEYBINDS[i].modifiers[j] == "alt") + mods &= event.altKey ; + else if(KEYBINDS[i].modifiers[j] == "shift") + mods &= event.shiftKey ; + } + if(Debug.debug || Debug.keyboard) + console.log("we pressed a key: ", event.key , " | mods match?", mods, "keybinding: ", KEYBINDS[i]); + if(mods){ + event.stopPropagation(); + + console.log("uw::keydown | keys match. Taking action."); + if(KEYBINDS[i].action == "char"){ + changeCSS("char", KEYBINDS[i].targetAR); + return; + } + if(KEYBINDS[i].action == "autoar"){ + manual_autoar(); + return; + } + changeCSS("anything goes", KEYBINDS[i].action); + return; + } + } + } + }); + + document.addEventListener("mozfullscreenchange", function( event ) { + onFullScreenChange(); + inFullScreen = ( window.innerHeight == window.screen.height && window.innerWidth == window.screen.width); + inFullScreen ? onFullscreenOn() : onFullscreenOff(); + }); +} diff --git a/js/conf/keybinds.js b/js/conf/keybinds.js deleted file mode 100644 index c7e23d6..0000000 --- a/js/conf/keybinds.js +++ /dev/null @@ -1,57 +0,0 @@ -// Yeah hi /r/badcode. -// Anyway, because nazi localstorage flat out refuses to store arrays: -var DEFAULT_KEYBINDINGS = { - 0:{ action: "fitw", - key: 'w', - modifiers: [] - }, - 1:{ - action: "fith", - key: 'e', - modifiers: [] - }, - 2: { - action: "reset", - key: 'r', - modifiers: [] - }, - 3: { - action: "zoom", - key: "z", - modifiers: [] - }, - 4: { - action: "unzoom", - key: "u", - modifiers: [] - }, - 5: { - action: "char", - targetAR: (21/9), - key: "d", - modifiers: [] - }, - 6: { - action: "char", - targetAR: (16/9), - key: "s", - modifiers: [] - }, - 7: { - action: "char", - targetAR: (16/10), - key: "x", - modifiers: [] - }, - 8: { - action: "char", - targetAR: (4/3), - key: "c", - modifiers: [] - }, - 9: { - action: "autoar", - key: "a", - modifiers: [] - } -}; diff --git a/js/lib/BrowserDetect.js b/js/lib/BrowserDetect.js new file mode 100644 index 0000000..3223193 --- /dev/null +++ b/js/lib/BrowserDetect.js @@ -0,0 +1,12 @@ +var _bd_usebrowser = "firefox"; + +if(typeof browser === "undefined"){ // This means we're probably not on Firefox. + if(chrome){ + browser = chrome; + _bd_usebrowser = "chrome"; + } +} + +var BrowserDetect = { + usebrowser = _bd_usebrowser; +} diff --git a/js/lib/StorageManager.js b/js/lib/StorageManager.js new file mode 100644 index 0000000..3408c91 --- /dev/null +++ b/js/lib/StorageManager.js @@ -0,0 +1,21 @@ +// setopt, getopt, delopt. Shrani oz. dobi oz. briše stvari iz skladišča +// setopt, getopt, delopt. They set/get/delete stuff from the storage + +var _sm_setopt = function(item){ + browser.storage.local.set(item); +} +var _sm_getopt = function(prop, callback){ + if(BrowserDetect.usebrowser == "chrome") + browser.storage.local.get(prop, callback); + else + browser.storage.local.get(prop).then(callback); +} +var _sm_delopt = function(item){ + browser.storage.local.remove(item); +} + +var StorageManager = { + setopt = _sm_setopt, + getopt = _sm_getopt, + delopt = _sm_delopt +} diff --git a/manifest.json b/manifest.json index 85b42b4..76bb12c 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Ultrawidify-git", - "version": "1.3a1", + "version": "2a1", "icons": { "32":"res/icons/uw-32.png",