Keybinds are now stored to localstorage. The code is a mess tho

This commit is contained in:
Tamius Han 2016-11-16 19:52:09 +01:00
parent d703e6f6cb
commit 8571fc1089
2 changed files with 53 additions and 26 deletions

View File

@ -7,7 +7,7 @@ var zoomStep = 0.05;
var whatdo_persistence = true; var whatdo_persistence = true;
var last_whatdo = "reset"; var last_whatdo = "reset";
var debugmsg = false; var debugmsg = true;
var ctlbar_classnames = ["ytp-chrome-controls"]; var ctlbar_classnames = ["ytp-chrome-controls"];
var serviceArray = [".video-stream" ]; //Youtube var serviceArray = [".video-stream" ]; //Youtube
@ -89,56 +89,82 @@ $("<style>")
") ")
.appendTo("head"); .appendTo("head");
var DEFAULT_KEYBINDINGS = [
{ action: "fit-width", // Yeah hi /r/badcode.
// Anyway, because nazi localstorage flat out refuses to store arrays:
var DEFAULT_KEYBINDINGS = {
1:{ action: "fitw",
key: 'w', key: 'w',
modifiers: [] modifiers: []
}, },
{ 2:{
action: "fit-height", action: "fith",
key: 'e', key: 'e',
modifiers: [] modifiers: []
}, },
{ 3: {
action: "reset", action: "reset",
key: 'r', key: 'r',
modifiers: [] modifiers: []
}, },
{ 4: {
action: "zoom", action: "zoom",
key: "z", key: "z",
modifiers: [] modifiers: []
}, },
{ 5: {
action: "unzoom", action: "unzoom",
key: "u", key: "u",
modifiers: [] modifiers: []
}, },
{ 6: {
action: "char", action: "char",
targetAR: (21/9), targetAR: (21/9),
key: "d", key: "d",
modifiers: [] modifiers: []
}, },
{ 7: {
action: "char", action: "char",
targetAR: (16/9), targetAR: (16/9),
key: "s", key: "s",
modifiers: [] modifiers: []
}, },
{ 8: {
action: "char", action: "char",
targetAR: (16/10), targetAR: (16/10),
key: "x", key: "x",
modifiers: [] modifiers: []
}, },
{ 9: {
action: "char", action: "char",
targetAR: (4/3), targetAR: (4/3),
key: "a", key: "a",
modifiers: [] modifiers: []
} }
]; };
var KEYBINDS = {};
var ask4keybinds = browser.storage.local.get("ultrawidify_keybinds");
ask4keybinds.then( (res) => {
if(res.length == 1 && jQuery.isEmptyObject(res[0])){
if(debugmsg)
console.log("uw::<init keybinds> | No keybindings found. Loading default keybinds as keybinds");
browser.storage.local.set({ultrawidify_keybinds:DEFAULT_KEYBINDINGS});
KEYBINDS = DEFAULT_KEYBINDINGS;
}
else{
KEYBINDS = res[0].ultrawidify_keybinds;
}
console.log("res. ", res[0].ultrawidify_keybinds);
});
$(document).ready(function() { $(document).ready(function() {
if(debugmsg) if(debugmsg)
@ -157,30 +183,31 @@ $(document).ready(function() {
console.log("We're writing a comment or something. Doing nothing"); console.log("We're writing a comment or something. Doing nothing");
return; return;
} }
console.log(KEYBINDS);
console.log("we pressed a key: ", event.key , " | keydown: ", event.keydown); console.log("we pressed a key: ", event.key , " | keydown: ", event.keydown);
for(var i = 0; i < DEFAULT_KEYBINDINGS.length; i++){ for(i in KEYBINDS){
if(event.key == DEFAULT_KEYBINDINGS[i].key){ console.log("i: ", i, "keybinds[i]:", KEYBINDS[i]);
if(event.key == KEYBINDS[i].key){
console.log("Key matches!"); console.log("Key matches!");
//Tipka se ujema. Preverimo še modifierje: //Tipka se ujema. Preverimo še modifierje:
//Key matches. Let's check if modifiers match, too: //Key matches. Let's check if modifiers match, too:
var mods = true; var mods = true;
for(var j = 0; j < DEFAULT_KEYBINDINGS[i].modifiers.length; j++){ for(var j = 0; j < KEYBINDS[i].modifiers.length; j++){
if(DEFAULT_KEYBINDINGS[i].modifiers[j] == "ctrl") if(KEYBINDS[i].modifiers[j] == "ctrl")
mods &= event.ctrlKey ; mods &= event.ctrlKey ;
else if(DEFAULT_KEYBINDINGS[i].modifiers[j] == "alt") else if(KEYBINDS[i].modifiers[j] == "alt")
mods &= event.altKey ; mods &= event.altKey ;
else if(DEFAULT_KEYBINDINGS[i].modifiers[j] == "shift") else if(KEYBINDS[i].modifiers[j] == "shift")
mods &= event.shiftKey ; mods &= event.shiftKey ;
} }
console.log("we pressed a key: ", event.key , " | mods match?", mods, "keybinding: ", DEFAULT_KEYBINDINGS[i]); console.log("we pressed a key: ", event.key , " | mods match?", mods, "keybinding: ", KEYBINDS[i]);
if(mods){ if(mods){
if(DEFAULT_KEYBINDINGS[i].action == "char"){ if(KEYBINDS[i].action == "char"){
changeCSS("char", DEFAULT_KEYBINDINGS[i].targetAR); changeCSS("char", KEYBINDS[i].targetAR);
return; return;
} }
changeCSS("anything goes", DEFAULT_KEYBINDINGS[i].action); changeCSS("anything goes", KEYBINDS[i].action);
return; return;
} }
} }

View File

@ -2,7 +2,7 @@
"manifest_version": 2, "manifest_version": 2,
"name": "Ultrawidify-git", "name": "Ultrawidify-git",
"version": "0.9.9.1", "version": "0.9.9.2",
"icons": { "icons": {
"32":"icons/uw-32.png", "32":"icons/uw-32.png",
@ -21,7 +21,7 @@
], ],
"permissions": [ "permissions": [
"tabs", "activeTab", "*://*.youtube.com/*", "*://youtube.com/*" "storage", "tabs", "activeTab", "*://*.youtube.com/*", "*://youtube.com/*"
], ],
"web_accessible_resources": [ "web_accessible_resources": [