Skip to content

Instantly share code, notes, and snippets.

@appetizermonster
Last active May 31, 2017 00:34
Show Gist options
  • Save appetizermonster/3cd46b2187e6fd344f75ae05c9d6f8b8 to your computer and use it in GitHub Desktop.
Save appetizermonster/3cd46b2187e6fd344f75ae05c9d6f8b8 to your computer and use it in GitHub Desktop.
A Tampermonkey Script, Trello with magic
// ==UserScript==
// @name Trello with magic
// @version 0.0.7
// @description Something magical for Trello
// @author Heejin Lee <[email protected]>
// @updateURL https://gist.github.com/appetizermonster/3cd46b2187e6fd344f75ae05c9d6f8b8/raw/trello-with-magic.user.js
// @downloadURL https://gist.github.com/appetizermonster/3cd46b2187e6fd344f75ae05c9d6f8b8/raw/trello-with-magic.user.js
// @include https://trello.com/*
// @include https://trello.com/b/*/*
// @include https://trello.com/c/*/*
// @resource toastCss https://cdnjs.cloudflare.com/ajax/libs/jquery-toast-plugin/1.3.1/jquery.toast.min.css
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery-toast-plugin/1.3.1/jquery.toast.min.js
// @grant GM_addStyle
// @grant GM_getResourceText
// ==/UserScript==
(function () {
'use strict';
// Toast
const toastCss = GM_getResourceText('toastCss');
GM_addStyle(toastCss);
function toast(msg) {
$.toast({
text: msg,
stack: false
});
}
function toasti(msg) {
$.toast({
text: msg,
hideAfter: 800,
loader: false
});
}
// Shortcut System
const shortcutCallbacks = {};
let lastPressTime = 0;
let lastComposition = '';
function registerShortcut(combination, callback) {
shortcutCallbacks[combination] = callback;
}
function processShortcutEvent(e) {
if (!checkProperUrl())
return;
if (!e.key)
return;
const elapsed = Date.now() - lastPressTime;
const shouldRecomposite = (elapsed >= 500);
if (shouldRecomposite)
lastComposition = ''; // Reset composition
lastComposition += e.key;
lastPressTime = Date.now();
let hasMatchedShortcut = false;
for (const shortcut in shortcutCallbacks) {
if (lastComposition.toLowerCase().endsWith(shortcut.toLowerCase())) {
const callback = shortcutCallbacks[shortcut];
e.preventDefault();
callback();
return;
}
hasMatchedShortcut = shortcut.startsWith(lastComposition);
}
if (hasMatchedShortcut)
e.preventDefault();
}
function checkProperUrl() {
const re = /^https?:\/\/trello.com\/[bc]\//i;
const url = document.location.href;
return re.test(url);
}
function initShortcut() {
document.addEventListener('keydown', processShortcutEvent);
}
// Card functions
function moveCardTo(positionSelector, doneCallback) {
const activeCard = $('.active-card');
if (activeCard.length !== 1)
return;
activeCard.find('span.list-card-operation').click();
$('a.js-move-card').click();
positionSelector($('.js-select-position').children()).attr('selected', 'selected');
$('input[value="Move"]').click();
const cardName = activeCard.find('a.js-card-name')[0].outerText;
const msg = `"${cardName}" has been moved`;
toasti(msg);
}
function moveCardToTop() {
moveCardTo((x) => x.first());
}
function moveCardToBottom() {
moveCardTo((x) => x.last());
}
// App
initShortcut();
registerShortcut('zt', moveCardToTop);
registerShortcut('zb', moveCardToBottom);
toast('Trello with magic');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment