TPPMD.Privatter

Helper Functions For Downloading Media From Privatter site

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://greasyfork.org/scripts/428458-tppmd-privatter/code/TPPMDPrivatter.js?version=946223

// ==UserScript==
// @name         TPPMD.Privatter
// @namespace    TPPMediaDownloader
// @version      0.1
// @description  Helper Functions For Downloading Media From Privatter site
// @author       miravois
// @match        https://privatter.net/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @run-at       document-end
// @require      http://code.jquery.com/jquery-3.4.1.min.js

// ==/UserScript==

//#region START Privatter Helper Functions

function privatterInjectDownloadButton() {
    $('div.navbar-header').append('<button type="button" id="btnDownload" class="btn btn-default">Download</button>');
    $('button[id="btnDownload"]').click(downloadMedia);
}//end privatterInjectDownloadButton

function privatterGetTwitterUsername() {
    let $twitterUsername = $('a[href*="twitter.com"]');
    if ($twitterUsername.length === 0) {
        logError('Failed to find twitter username on page');
    }
    $twitterUsername = $twitterUsername[0];
    let regex = /twitter\.com\/(.*)/;
    let match = regex.exec($twitterUsername.href);
    return match[1];
}//end privatterGetTwitterUsername

function privatterGetMediaType() {
    return /privatter\.net\/(\w)/.exec(window.location.href)[1];
}//end privatterGetMediaType

function privatterGetImageUrls() {
    let imgUrls = [];
    $('img[src*="cloudfront"]').each(function (i, img) {
        // https://d2pqhom6oey9wx.cloudfront.net/img_resize/758192460afae2e78852.png
        imgUrls.push(img.src.replace('img_resize', 'img_original'));
    });
    return imgUrls;
}//end privatterGetImageUrls

function privatterGetImageSeriesId() {
    // https://privatter.net/i/5931960
    const regex = /privatter\.net\/\w\/(\d+)/;
    const match = regex.exec(window.location.href);
    return match[1];
}//end privatterGetImageSeriesId

function privatterGetImageExtension(url) {
    // https://d2pqhom6oey9wx.cloudfront.net/img_original/758192460afae2e78852.png
    const regex = /img_original\/.*\.(.*)/;
    const match = regex.exec(url);
    return match[1];
}//end privatterGetImageExtension

function privatterGetTextId() {
    // https://privatter.net/i/5931960
    const regex = /privatter\.net\/\w\/(\d+)/;
    const match = regex.exec(window.location.href);
    return match[1];
}//end privatterGetTextId

function privatterGetTextTitle() {
    let title = '';
    if ($('h1.lead').length && $('h1.lead').text() && $('h1.lead').text() !== '') {
        title = '-' + $('h1.lead').text();
    }
    return title;
}//end privatterGetTextTitle

function privatterGetTextContent() {
    let text = '';
    $('div#evernote').find('p').each(function (i, $p) {
        if ($p.innerHTML && $p.innerHTML !== '') {
            text += $p.innerHTML.replaceAll('<br>', '\n') + '\n\n\n----------\n\n\n';
        }
    });
    const regex = /<\/?span.*/g;
    if (regex.test(text)) { text = text.replace(regex, ''); }
    return text;
}//end privatterGetTextContent

async function privatterMarkDownloaded() {
    if (!/privatter.net\/u\//.test(window.location.href)) { return; }

    const twitterUsername = privatterGetTwitterUsername();
    const siteUserId = twitterUsername;
    const twitterUserInfo = await getTwitterUserInfo(siteUserId, twitterUsername);
    if (!twitterUserInfo) { return; }
    const twitterUserId = twitterUserInfo.TwitterUserId;

    const $links = $('.tab-pane.active a');
    for (let i = 0; i < $links.length; i++) {
        const $link = $($links[i]);
        const match = /\/(\w)\/(\d+)/.exec($links[i].href);
        const mediaType = match[1] === 'p' ? MediaTypeEnum.Text : MediaTypeEnum.Image;
        const mediaId = match[2];
        const count = await dbGetDownloadRecordCount(twitterUserId, mediaId, mediaType);
        if (count !== 0) {
            $link.css('display', 'inline-block');
            $link.css('width', '100%');
            $link.addClass('tppmdMarkDownloaded');
            if (HideDownloaded) { $link.hide(); }
        }
    }
}//end privatterMarkDownloaded

//#endregion END Privatter Helper Functions