My Technical Notes

Friday, 11 March 2016

Javascript Bookmarklet to Stylize Org mode output


// use https://ted.mielczarek.org/code/mozilla/bookmarklet.html to crunchinate
javascript:(function() {
    // start jqueryify the page
    var script = document.createElement('script');
    script.src = "https://code.jquery.com/jquery-2.2.1.min.js";
    document.getElementsByTagName('head')[0].appendChild(script);
    // end jqueryify the page

    // start style definitions
    var tagStyle = { 'color': 'blue', 'background-color' : 'greenyellow' };
    var font = 'calibri'; // change to the outlook style later
    // end style definitions

    function tableStyle(color) {
        return 'padding-left: 1.5em; border: 1px dotted ' + color + '; border-top: none; border-right: none; border-bottom: none';
    }

    // indentation requires the text to be in a table
    // https://litmus.com/help/email-clients/outlookcom-margins/
    function applyModifications() {
        console.log('start applyModifications');
        
        jQuery(document).ready(function($){
            $('.tag').css(tagStyle);
            $('#content *').css('font-family', font);
            $('#content > h1, #postamble').remove(); // remove the header
            $('.outline-3').wrap('<table><tr><td style="' + tableStyle('blue') + '"></td></tr></table>');  // wrap .outline-3
            $('.outline-4').wrap('<table><tr><td style="' + tableStyle('red') + '"></td></tr></table>');  // wrap .outline-4
        });
    }

    function deferUntil(predicate, fn) {
        if (predicate()) {
            fn();
        } else {
            setTimeout(function() { deferUntil(predicate, fn) }, 50);
        }
    }

    deferUntil(function() {
        return window.jQuery !== undefined
    }, applyModifications);
})();

No comments: