Skip to content

Instantly share code, notes, and snippets.

@andrew-luhring
Created December 30, 2014 22:19
Show Gist options
  • Save andrew-luhring/6110dc88e9841fa143bd to your computer and use it in GitHub Desktop.
Save andrew-luhring/6110dc88e9841fa143bd to your computer and use it in GitHub Desktop.
Copy paste this in your browsers' developer tools console while editing a tumblr theme in the built in tumblr html editor and esc+r will update the preview
(function(){
/**
* inserts a div with text into <div id="log"></div> or whichever element you pass to second param.
* @param {*} - thing - text to log.
* @param {Object} - optional - DOMElement to append to.
*/
function log(thing, _el){
var el = _el || document.getElementById('log')
, div = document.createElement('div');
div.innerText = thing;
el.appendChild(div);
}
/**
* Change the contents of this function to do whatever it is you want it to do on your key command press.
*/
function doThing(){
log(registrar.vals());
// if you're working within the tumblr editor and want esc + r (on a mac) to trigger the Update Preview action:
jQuery('[data-action=update_preview]').not('.disabled').click();
}
/**
* Object that keeps track of key presses.
* @params {Number} - <a href="http://css-tricks.com/snippets/javascript/javascript-keycodes/" target="_blank">keycode</a>. Default key command is ```esc + r```.
* @constructor
*/
function Registrar(_keyA, _keyB){
var that = this;
this.keyA = _keyA || 27;
this.keyB = _keyB || 82;
this.keyAPressed = false;
this.keyBPressed = false;
this.keys = (function(){
return Object.keys(that);
})();
}
/**
* reset registered keys to false
* @return {Object}
*/
Registrar.prototype.clear = function(){
this.keyAPressed = false;
this.keyBPressed = false;
return this;
}
/**
* Helper function to get current values of keys. Not essential, but useful for debugging.
* @return {String}
*/
Registrar.prototype.vals = function(){
var str = '';
for(var i = 0; i < this.keys.length; i++){
if(typeof this[this.keys[i]] === 'boolean'){
str += " " + this[this.keys[i]];
}
}
return str;
};
(function initialize(){
var registrar = new Registrar();
document.addEventListener('keyup', function(e){
var key = e.which;
if( key === registrar.keyA ){
registrar.keyAPressed = true;
} else if( key === registrar.keyB){
registrar.keyBPressed = true;
} else {
registrar.clear();
}
if(registrar.keyAPressed === true && registrar.keyBPressed === true){
doThing();
registrar.clear();
}
})
})();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment