Last active
November 21, 2019 09:38
-
-
Save appetizermonster/0f95597f1ad71b1668bc44812502605c to your computer and use it in GitHub Desktop.
steemit-helper.user.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Steemit Helper | |
// @version 0.1.1 | |
// @description Tag Autocompletion, more is coming. | |
// @author Heejin Lee <monster@teamappetizer.com> | |
// @updateURL https://gist.github.com/appetizermonster/0f95597f1ad71b1668bc44812502605c/raw/steemit-helper.user.js | |
// @downloadURL https://gist.github.com/appetizermonster/0f95597f1ad71b1668bc44812502605c/raw/steemit-helper.user.js | |
// @match https://steemit.com/* | |
// @include https://steemit.com/*/* | |
// @resource selectizeCss https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.4/css/selectize.min.css | |
// @require https://code.jquery.com/jquery-1.12.4.min.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.4/js/standalone/selectize.min.js | |
// @require https://cdn.steemjs.com/lib/latest/steem.min.js | |
// @grant GM_addStyle | |
// @grant GM_getResourceText | |
// ==/UserScript== | |
(function () { | |
'use strict'; | |
const selectizeCss = GM_getResourceText('selectizeCss'); | |
GM_addStyle(selectizeCss); | |
function getUrl() { | |
return document.location.toString(); | |
} | |
var _watchId = 0; | |
function checkUrlAndInjectExtension() { | |
// hack for injection failure | |
clearInterval(_watchId); | |
_watchId = setInterval(function () { | |
var isReady = (document.readyState === 'complete'); | |
if (!isReady) | |
return; // not ready to inject | |
clearInterval(_watchId); | |
var url = getUrl(); | |
_injectTagExtension(url); | |
}, 200); | |
} | |
function _injectTagExtension(url) { | |
if (url !== 'https://steemit.com/submit.html') | |
return; | |
var tagInput = $('input[name=category]'); | |
tagInput.selectize({ | |
delimiter: ' ', | |
searchField: ['value'], | |
maxItems: 5, | |
createFilter: /^[a-z0-9]+-?[a-z0-9]+$/, | |
create: true, | |
persist: true, | |
render: { | |
option: function (data, escape) { | |
var _value = escape(data.value); | |
if (data.top_posts) | |
return '<div class="option">' + _value + ' (' + parseInt(data.top_posts).toLocaleString() + ' posts)</div>'; | |
return '<div class="option">' + _value + '</div>'; | |
}, | |
item: function (data, escape) { | |
return '<div class="item">' + escape(data.value) + '</div>'; | |
} | |
} | |
}); | |
// deliver changes to draft watch | |
tagInput.bind('change', function (e) { | |
tagInput[0].dispatchEvent(new Event('input', { | |
bubbles: true | |
})); | |
}); | |
// fetch tags using steem api | |
steem.api.getTrendingTags('steemit', 500, function (err, res) { | |
if (err) | |
return console.log(err); | |
var tags = res.map(function (obj) { | |
return { | |
value: obj.name, | |
text: obj.name, | |
top_posts: obj.top_posts | |
}; | |
}); | |
tagInput[0].selectize.addOption(tags); | |
}); | |
} | |
checkUrlAndInjectExtension(); | |
// watch for changes | |
var _lastUrl = getUrl(); | |
$('html').bind('DOMNodeInserted', function (e) { | |
var curUrl = getUrl(); | |
if (_lastUrl === curUrl) | |
return; | |
_lastUrl = curUrl; | |
// url has been changed | |
checkUrlAndInjectExtension(); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment