Reddit - Toggle Custom CSS

Persistently disable/re-enable subreddit-specific styles via a userscript command

2018-08-08 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Reddit - Toggle Custom CSS
// @description  Persistently disable/re-enable subreddit-specific styles via a userscript command
// @author       chocolateboy
// @namespace    https://github.com/chocolateboy/userscripts
// @include      http://reddit.com/r/*
// @include      https://reddit.com/r/*
// @include      http://*.reddit.com/r/*
// @include      https://*.reddit.com/r/*
// @require      https://code.jquery.com/jquery-3.2.1.min.js
// @require      https://cdn.rawgit.com/eclecto/jQuery-onMutate/79bbb2b8caccabfc9b9ade046fe63f15f593fef6/src/jquery.onmutate.min.js
// @version      1.1.0
// @run-at       document-start
// @grant        GM_addStyle
// @grant        GM_deleteValue
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// ==/UserScript==

// inspired by: http://userscripts-mirror.org/scripts/show/109818

const SUBREDDIT = location.pathname.match(/\/r\/(\w+)/)[1]
const CUSTOM_CSS = 'link[ref^="applied_subreddit_"]'

function toggle () {
    const oldDisableCss = GM_getValue(SUBREDDIT, false)
    const disableCss = !oldDisableCss

    $(CUSTOM_CSS).prop('disabled', disableCss)

    if (disableCss) {
        GM_setValue(SUBREDDIT, true)
    } else {
        GM_deleteValue(SUBREDDIT)
    }
}

// NOTE we need to disable the display rather than setting its visibility to
// hidden as the latter doesn't hide the background (which leads to a flash of
// styled content (FOSC) on subreddits with a custom background color and/or
// image)
//
// XXX hide the html element rather than the body element as the latter still
// results in a FOSC on some subreddits e.g. /r/firefox
function hidePage () {
    GM_addStyle('html { display: none !important }')
}

const disableCss = GM_getValue(SUBREDDIT, false)

if (disableCss) {
    $(document).onCreate('head', hidePage)

    // https://wiki.greasespot.net/DOMContentLoaded#Workaround
    $(document).on('DOMContentLoaded', () => {
        $(CUSTOM_CSS).prop('disabled', true)
        GM_addStyle('html { display: initial !important }')
    })
}

GM_registerMenuCommand('Toggle Custom CSS', toggle)