Greasy Fork

9gag NSFW unblock

Unblock NSFW without account

当前为 2020-12-11 提交的版本,查看 最新版本

// ==UserScript==
// @name         9gag NSFW unblock
// @version      0.1.1
// @description  Unblock NSFW without account
// @author       Unblockit
// @match        https://9gag.com/*
// @grant        none
// @namespace http://tampermonkey.net/
// ==/UserScript==

'use strict';

function setChild(container, post) {
    container.children[0].replaceWith(post);
}

function tryMP4(id, container) {
    const source = "https://img-9gag-fun.9cache.com/photo/" + id + "_460sv.mp4";
    const video = document.createElement("video");
    video.controls = true;
    //  video.autoplay = true;
    //  video.muted = true;
    video.src = source;
    video.onloadedmetadata = function() {
        console.log("NSFW MP4");
        setChild(container, video);
    }
    video.onerror = function() {
        console.log("not an MP4");
        tryJPG(id, container);
    }
}

function tryJPG(id, container) {
    const source = "https://img-9gag-fun.9cache.com/photo/" + id + "_700b.jpg";
    const picture = document.createElement("img");
    picture.src = source;
    setChild(container, picture);
    picture.onload = function() {
        console.log("NSFW JPG");
    }
    picture.onerror = function() {
        console.log("not an JPG");
    }
}

var mobile = true;
var desktop = true;

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

            //tryJPG(id, container);
            tryMP4(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);

            //tryJPG(id, container);
            tryMP4(id, container);
        }
    }
}

var time = setInterval(clearPost, 1000)