Greasy Fork

9gag NSFW unblock

Unblock NSFW without account

当前为 2021-04-28 提交的版本,查看 最新版本

// ==UserScript==
// @name         9gag NSFW unblock
// @namespace    https://greasyfork.org/en/users/715695-unblock
// @version      0.3.1
// @description  Unblock NSFW without account
// @author       Unblock
// @match        https://9gag.com/*
// @icon         https://icons.duckduckgo.com/ip2/9gag.com.ico
// @grant        none
// ==/UserScript==

'use strict';
const baseUrl = "https://img-9gag-fun.9cache.com/photo/";

function setChild(container, post) {
    container.firstElementChild.replaceWith(post);
}

function tryVideo(id, container) {
    const video = document.createElement("video");
    video.preload = "auto";
    video.poster = baseUrl + id + "_460s.jpg";

    const srcAV1 = document.createElement("source");
    srcAV1.src = baseUrl + id + "_460svav1.mp4";
    srcAV1.type = "video/mp4; codecs=\"av01.0.00M.08, opus\"";
    video.appendChild(srcAV1);

    const srcVP9 = document.createElement("source");
    srcVP9.src = baseUrl + id + "_460svvp9.webm";
    srcVP9.type = "video/mp4; codec=\"vp9, opus\"";
    video.appendChild(srcVP9);

    const srcMP4 = document.createElement("source");
    srcMP4.src = baseUrl + id + "_460sv.mp4";
    srcMP4.type = "video/mp4";
    video.appendChild(srcMP4);

    video.controls = true;
    //  video.autoplay = true;
    //  video.muted = true;
    video.onloadedmetadata = function() {
    //    console.log("NSFW MP4");
        setChild(container, video);
    }
}

function tryImage(id, container) {
    const picture = document.createElement("picture");
    const source = document.createElement("source");
    source.srcset = baseUrl + id + "_460swp.webp";
    source.type = "image/webp";
    picture.appendChild(source);
    const img = document.createElement("img");
    img.src = baseUrl + id + "_460s.jpg";
    img.loading = "lazy";
    picture.appendChild(img);
    setChild(container, picture)
}

var mobile = true;
var desktop = true;

function clearPosts () {
    if (desktop) {
        var nsfw_desktop = document.querySelectorAll("div.nsfw-post");
        for (var post of nsfw_desktop) {
            mobile = false;
            const container = post.parentElement.parentElement.parentElement;
            const article = container.parentElement;
            const id = article.querySelector("a.point.badge-evt").getAttribute("data-entry-id");

            tryImage(id, container);
            tryVideo(id, container);
        }
    }

    if (mobile) {
        var nsfw_mobile = document.querySelectorAll("div.nsfw-mask");
        for (post of nsfw_mobile) {
            desktop = false;
            const container = post.parentElement.parentElement;
            const id = container.parentElement.id.slice(15);

            tryImage(id, container);
            tryVideo(id, container);
        }
    }
}

window.onscroll = function() {clearPosts()}
window.ontouchmove = function() {clearPosts()}
setTimeout(clearPosts, 200);