Twitch - Skip content warnings

Automatically skip content warnings before playing a stream on Twitch

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Twitch - Skip content warnings
// @namespace    https://twitch-tools.rootonline.de/
// @version      1.2
// @description  Automatically skip content warnings before playing a stream on Twitch
// @author       @CommanderRoot
// @match        https://www.twitch.tv/*
// @run-at       document-start
// @grant        GM_addStyle
// ==/UserScript==

// Hide overlay warning in the top left when you hover over the stream
GM_addStyle('.disclosure-card{display: none !important;}');

function getCookie(name) {
	const value = '; ' + document.cookie;
	const parts = value.split('; ' + name + '=');
	if (parts.length === 2) return parts.pop().split(';').shift();
}

function getcurrentUserID() {
	const userCookie = getCookie('twilight-user');
	console.log(userCookie);
	if (typeof userCookie !== 'undefined') {
		let user = JSON.parse(decodeURIComponent(userCookie));
		if (typeof user === 'object' && typeof user['id'] !== 'undefined') {
			return user['id'].toString();
		}
	}
}

(function () {
	'use strict';

	const localStorageKey = 'content-classification-labels-acknowledged';
	const acknowledgedDuration = Date.now() + (365 * 24 * 60 * 60 * 1000); // Now + 365 days
	const acknowledgedMaxAge = Date.now() + (1 * 24 * 60 * 60 * 1000); // Now + 1 day

	try {
		// Get current setting
		let currentSetting = window.localStorage.getItem(localStorageKey);
		if (currentSetting !== null) {
			currentSetting = JSON.parse(currentSetting);
		} else {
			currentSetting = {};
		}

		const currentUserID = getcurrentUserID();

		let targetSetting = JSON.parse(JSON.stringify(currentSetting));
		if (typeof targetSetting['loggedIn'] === 'undefined') {
			targetSetting['loggedIn'] = {};
		}
		if (typeof targetSetting['loggedOut'] === 'undefined') {
			targetSetting['loggedOut'] = {
				'DrugsIntoxication': acknowledgedDuration,
				'Gambling': acknowledgedDuration,
				'MatureGame': acknowledgedDuration,
				// "Significant Profanity or Vulgarity" (selectable but doesn't trigger anything atm)
				'SexualThemes': acknowledgedDuration,
				'ViolentGraphic': acknowledgedDuration,
			};
		} else {
			if (typeof targetSetting['loggedOut']['DrugsIntoxication'] === 'undefined' || targetSetting['loggedOut']['DrugsIntoxication'] < acknowledgedMaxAge) {
				targetSetting['loggedOut']['DrugsIntoxication'] = acknowledgedDuration;
			}
			if (typeof targetSetting['loggedOut']['Gambling'] === 'undefined' || targetSetting['loggedOut']['Gambling'] < acknowledgedMaxAge) {
				targetSetting['loggedOut']['Gambling'] = acknowledgedDuration;
			}
			if (typeof targetSetting['loggedOut']['MatureGame'] === 'undefined' || targetSetting['loggedOut']['MatureGame'] < acknowledgedMaxAge) {
				targetSetting['loggedOut']['MatureGame'] = acknowledgedDuration;
			}
			if (typeof targetSetting['loggedOut']['SexualThemes'] === 'undefined' || targetSetting['loggedOut']['SexualThemes'] < acknowledgedMaxAge) {
				targetSetting['loggedOut']['SexualThemes'] = acknowledgedDuration;
			}
			if (typeof targetSetting['loggedOut']['ViolentGraphic'] === 'undefined' || targetSetting['loggedOut']['ViolentGraphic'] < acknowledgedMaxAge) {
				targetSetting['loggedOut']['ViolentGraphic'] = acknowledgedDuration;
			}
		}

		if (typeof currentUserID === 'string' && currentUserID !== '') {
			if (typeof targetSetting['loggedIn'][currentUserID] === 'undefined') {
				targetSetting['loggedIn'][currentUserID] = [];
			}
			if (!targetSetting['loggedIn'][currentUserID].includes('DrugsIntoxication')) {
				targetSetting['loggedIn'][currentUserID].push('DrugsIntoxication');
			}
			if (!targetSetting['loggedIn'][currentUserID].includes('Gambling')) {
				targetSetting['loggedIn'][currentUserID].push('Gambling');
			}
			if (!targetSetting['loggedIn'][currentUserID].includes('MatureGame')) {
				targetSetting['loggedIn'][currentUserID].push('MatureGame');
			}
			if (!targetSetting['loggedIn'][currentUserID].includes('SexualThemes')) {
				targetSetting['loggedIn'][currentUserID].push('SexualThemes');
			}
			if (!targetSetting['loggedIn'][currentUserID].includes('ViolentGraphic')) {
				targetSetting['loggedIn'][currentUserID].push('ViolentGraphic');
			}
		}

		// Set new setting
		window.localStorage.setItem(localStorageKey, JSON.stringify(targetSetting));
	} catch (e) {
		// Browser blocks localStorage access so we can't get or set any setting
		return;
	}

})();