perception / TRS Post Numbers

// ==UserScript==
// @name         TRS Post Numbers
// @namespace    https://forum.therightstuff.biz/
// @version      0.3
// @description  Add post numbers to posts on the TRS forum
// @author       perception
// @match        https://forum.therightstuff.biz/topic/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var observer;
    var observerOptions = { childList: true, subtree: true };
    var addDigits = function(n){
        $(n).find('li[component=post]').each(function(i, el){
            var pid = $(el).data('pid');
            var p = $(" <span><i class=\"fa fa-hashtag\"></i> "+pid+"</span>").addClass('btn btn-xs btn-danger hidden-xs');
            $(el).find('a.permalink').after(p);
        });
    };
    var mutationHandler = function(mutations) {
        mutations.forEach(function(m) {
            if (m.type != 'childList') return;
            m.addedNodes.forEach(function(n) {
                if (n.nodeType != Node.ELEMENT_NODE) return;
                observer.disconnect();
                if (!n.isContentEditable) {
                    addDigits(n);
                }
                observer.observe(document.body, observerOptions);
            });
        });
    };
    addDigits(document.body);
    observer = new MutationObserver(mutationHandler);
    observer.observe(document.body, observerOptions);
})();