Better wykop

Display high resolution images on wykop.pl

目前為 2020-03-21 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Better wykop
// @namespace    http://tampermonkey.net/
// @version      0.1.5
// @description  Display high resolution images on wykop.pl
// @author       You
// @match        *://*.wykop.pl/*
// @grant        none
// ==/UserScript==

function debounce(func, wait, immediate = false) {
    let timeout;
    return function () {
        const context = this, args = arguments;
        const later = function () {
            timeout = null;
            if (!immediate) { func.apply(context, args); }
        };
        const callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) { func.apply(context, args); }
    };
};

const removeLowResolutionFromUrl = (url) => url
    .replace(',q40', '')
    .replace(',w400', '')
    .replace(',w113h64', '')
    .replace(',w207h139', '')
    .replace(',w300h223', '')

const updateImages = () => {
    console.log('łogiń')
    const images = document.querySelectorAll('img');

    if (images) {
        images.forEach((image) => {
            const src = image.getAttribute('src')
            const dataOriginal = image.getAttribute('data-original')

            if (!image.classList.contains('lazy')) {
                image.style['max-width'] = `${image.width}px`

                image.setAttribute(
                    src ? 'src' : 'data-original',
                    removeLowResolutionFromUrl(src ? src : dataOriginal || '')
                )
            }
        })
    }
}

const debouncedUpdateImages = debounce(function () {
    updateImages()
}, 250);

(() => {
    document.onreadystatechange = () => {
        if (document.readyState === 'complete') {
            window.addEventListener('scroll', debouncedUpdateImages);
            setTimeout(() => {
                updateImages()
            }, 500)
        }
    };
})()