Matemaks Paywall

Ukrywa div.center.platny_dostep_info i dodaje przycisk YouTube oraz PDF przy każdym zadaniu z atrybutem yt

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

Advertisement:

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

Advertisement:

// ==UserScript==
// @name         Matemaks Paywall
// @version      1.1
// @description  Ukrywa div.center.platny_dostep_info i dodaje przycisk YouTube oraz PDF przy każdym zadaniu z atrybutem yt
// @match        *://www.matemaks.pl/*
// @license      MIT
// @grant        none
// @namespace https://greasyfork.org/users/1618843
// ==/UserScript==

(function () {
    'use strict';

    // ─── Konfiguracja ─────────────────────────────────────────────────────────
    const PDF_BASE_URL = 'https://www.matemaks.pl/notatki'; // <-- zmień na właściwy URL

    // ─── 1. Ukryj blokady dostępu ─────────────────────────────────────────────
    function hidePaywalls() {
        document.querySelectorAll('.center.platny_dostep_info').forEach(el => {
            el.style.display = 'none';
        });
    }

    // ─── 2. Styl przycisku ────────────────────────────────────────────────────
    function applyBtnStyle(btn, bgColor = '#fcfbf1') {
        Object.assign(btn.style, {
            display:    'inline-flex',
            alignItems: 'center',
            gap:        '6px',
            padding:    '2px 9px',
            margin:     '0',
            cursor:     'pointer',
            border:     '1px solid #ccc',
            background: bgColor,
            boxSizing:  'border-box',
            font:       'inherit',
        });
        btn.addEventListener('mouseenter', () => btn.style.background = '#fcfcd3');
        btn.addEventListener('mouseleave', () => btn.style.background = bgColor);
    }

    // ─── 3. Dodaj przyciski YT + PDF ─────────────────────────────────────────
    function addButtons() {
        document.querySelectorAll('.zadanie[yt]').forEach(el => {
            const ytId = el.getAttribute('yt');
            if (!ytId) return;

            // Nie dodawaj przycisków drugi raz
            if (el.querySelector('.yt-btn-injected')) return;

            // Pomiń jeśli zadanie ma już .buttons
            if (el.querySelector('.buttons')) return;

            const sek    = el.getAttribute('sek');
            const dataId = el.getAttribute('data-id') || el.getAttribute('id') || '';

            // ── Przycisk YouTube ──────────────────────────────────────────────
            const ytBtn = document.createElement('button');
            ytBtn.className = 'yt-btn-injected';
            ytBtn.innerHTML = `
                <img src="/grafika/ikony/film.svg" style="width:16px;height:16px;display:block;" onerror="this.style.display='none'">
                <span>Film</span>
            `;
            applyBtnStyle(ytBtn);

            // ── Przycisk PDF ──────────────────────────────────────────────────
            const pdfBtn = document.createElement('button');
            pdfBtn.className = 'pdf-btn-injected';

            // Numer z wiodącymi zerami, np. data-id="3740" → z3740n.pdf
            const paddedId  = dataId.padStart(4, '0');
            const pdfUrl    = `${PDF_BASE_URL}/z${paddedId}n.pdf`;

            pdfBtn.innerHTML = `
                <img src="/grafika/ikony/pisemne.svg" style="width:16px;height:16px;display:block;" onerror="this.style.display='none'">
                <span>Pdf</span>
            `;
            applyBtnStyle(pdfBtn);

            pdfBtn.addEventListener('click', () => {
                window.open(pdfUrl, '_blank');
            });

            // ── Wrapper wyśrodkowujący oba przyciski ──────────────────────────
            const btnWrapper = document.createElement('div');
            Object.assign(btnWrapper.style, {
                display:        'flex',
                justifyContent: 'center',
                width:          '100%',
                margin:         '8px auto',
            });
            // Usuń prawy border pierwszego przycisku żeby nie było podwójnej linii
            ytBtn.style.borderRight = 'none';
            btnWrapper.appendChild(ytBtn);
            btnWrapper.appendChild(pdfBtn);

            // ── Embed wrapper (YT) ────────────────────────────────────────────
            const embedWrapper = document.createElement('div');
            embedWrapper.className = 'yt-embed-wrapper';
            Object.assign(embedWrapper.style, {
                display:     'none',
                marginTop:   '8px',
                width:       '100%',
                aspectRatio: '16 / 9',
            });

            ytBtn.addEventListener('click', () => {
                const visible = embedWrapper.style.display !== 'none';
                if (visible) {
                    embedWrapper.style.display = 'none';
                    embedWrapper.innerHTML = '';
                } else {
                    const startSek = sek && sek !== '0' ? `&start=${sek}` : '';
                    const iframe = document.createElement('iframe');
                    iframe.src             = `https://www.youtube.com/embed/${ytId}?autoplay=1${startSek}`;
                    iframe.allow           = 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture';
                    iframe.allowFullscreen = true;
                    Object.assign(iframe.style, {
                        width:   '100%',
                        height:  '100%',
                        border:  'none',
                        display: 'block',
                    });
                    embedWrapper.innerHTML = '';
                    embedWrapper.appendChild(iframe);
                    embedWrapper.style.display = 'block';
                }
            });

            el.appendChild(btnWrapper);
            el.appendChild(embedWrapper);
        });
    }

    // ─── 4. Obserwuj dynamiczne zmiany DOM ────────────────────────────────────
    function run() {
        hidePaywalls();
        addButtons();
    }

    const observer = new MutationObserver(run);
    observer.observe(document.body, { childList: true, subtree: true });

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', run);
    } else {
        run();
    }

})();