edmilsonmaia / DDP Cláudio Humberto

// ==UserScript==
// @name         DDP Cláudio Humberto
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Formats the Cláudio Humberto page at Diário do Poder
// @author       edmilsonmaia
// @match        https://diariodopoder.com.br/coluna-claudio-humberto/
// @grant        none
// @require      http://code.jquery.com/jquery-latest.js
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    $(document).ready(function() {
        var headerElement = $('.image_frame.image_item.no_link.scale-with-grid.no_border');
        var starterButton = $('<input id="ddpScrapperButton" type="button" value="DDPScrapper" />');
        //console.log(headerElement);
        headerElement.after(starterButton);

        $('#ddpScrapperButton').click(ddpScrapper);
    });

})();

function ddpScrapper() {
    var newDocString = '';

    newDocString += '<html><head><meta charset="UTF-8"><title>DDPScrapper</title>';
    newDocString += '<style>';
    newDocString += '* {font-family:Verdana; font-size: 12; margin-top: 0px; margin-bottom: 0px; text-align: justify;}';
    newDocString += 'h3 {text-transform: uppercase; font-weight: bold; overflow: hidden;}';
    newDocString += '.publicado {font-size: 10; color: #404040;';
    newDocString += '</style>';
    newDocString += '</head><body>';

    var previousDate = '';
    /*
    var dates = new Set();
    $('.date_label').each(function() {
        dates.add($(this).text());
    });
    console.log(dates);
    */

    var postItems = $('.post-item');
    postItems.each(function(postItem) {
        var date = $(this).find('.date_label').text();
        var title = $(this).find('.entry-title').first().find('a').text();
        var text = $(this).find('.post-excerpt').text();

        if(date != previousDate) {
            previousDate = date;
            newDocString += '<br /><h3>' + date + '</h3><br />';
        }

        newDocString += '<h3>' + title + '</h3>';
        //newDocString += '<span class="publicado">' + date + '</span>';
        newDocString += '<div>' + text + '</div>';
    });

    newDocString += '</body></html>';

    var newDoc = document.open("text/html", "replace");
    newDoc.write(newDocString);
    newDoc.close();

}