Skip to content

Instantly share code, notes, and snippets.

@tobek
Created January 26, 2017 00:06
Show Gist options
  • Save tobek/eeb9c517becdb9ef6cf7397fbc890137 to your computer and use it in GitHub Desktop.
Save tobek/eeb9c517becdb9ef6cf7397fbc890137 to your computer and use it in GitHub Desktop.
Habitica Tag Visibility Userscript
// ==UserScript==
// @name Habitica Tag Visibility
// @namespace tobek-habitica
// @version 0.1
// @description Makes tags visible in the name of the task, without having to hover over the tags icon. Tags are shown as "[tag1][tag2][...] Task name...". This is a total hack and would be much better as part of official Habitica codebase but that's more work and discussion than the 10m I spent on this.
// @author Tobek
// @match https://habitica.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var interval;
var hiddenProp = 'hidden';
if (hiddenProp in document)
document.addEventListener('visibilitychange', visChange);
else if ((hiddenProp = 'mozHidden') in document)
document.addEventListener('mozvisibilitychange', visChange);
else if ((hiddenProp = 'webkitHidden') in document)
document.addEventListener('webkitvisibilitychange', visChange);
else if ((hiddenProp = 'msHidden') in document)
document.addEventListener('msvisibilitychange', visChange);
function visChange() {
if (interval) {
clearInterval(interval);
}
if (! document[hiddenProp]) {
tagStuff();
// @TODO Maybe should use MutationObserver instead?
interval = setInterval(tagStuff, 5000);
}
}
function tagStuff() {
// console.time('ran tag stuff in');
var tagTooltip, tagString, $this;
$('.task').each(function() {
$this = $(this);
tagTooltip = $(this).find('.glyphicon-tags').attr('tooltip');
$this.find('.toby-tag-string').remove();
if (! tagTooltip) {
return;
}
tagString = '<b class="toby-tag-string">[' + tagTooltip.split(', ').join('][') + ']</b>';
$this.find('.task-text p').html(tagString + ' ' + $this.find('.task-text p').html());
});
// console.timeEnd('ran tag stuff in');
}
if (window.jQuery) {
$(visChange);
}
else {
var jQueryWaitInterval = setInterval(function() {
if (window.jQuery) {
clearInterval(jQueryWaitInterval);
$(visChange);
}
}, 100);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment