Skip to content

Instantly share code, notes, and snippets.

@Pictor13
Last active December 3, 2020 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pictor13/78043d23a393884a94593537f770ea20 to your computer and use it in GitHub Desktop.
Save Pictor13/78043d23a393884a94593537f770ea20 to your computer and use it in GitHub Desktop.
Youtube: press "Y" to watch /embed video version. Remove distractions, cookies/privacy requests (you can µBlock all now), useless comments, dopamine-dealers video-suggestions, and all that useless stuff that tracks you.
// ==UserScript==
// @name Youtube Fucookies
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Fuck off Youtube attempts to get your cookies. Press "Y" for going to /embed page and just watch the damn thing.
// @author Igor Pellegrini
// @include /^https?\:\/\/.*youtube\..*\/.*\?.*v=[^&]+.*$/
// @grant GM_log
// ==/UserScript==
(function() {
'use strict';
const annoyanceCssSelectors = [
'ytd-consent-bump-lightbox',
'iron-overlay-backdrop',
'ytd-popup-container',
'paper-dialog',
'#dialog'
]
const DEFAULT_TIMEOUT = 400
const CLICK_EVENT = new MouseEvent("click", {
bubbles: true,
cancelable: false
});
// global state
let hideTimeout = DEFAULT_TIMEOUT
let silenced = false
function goToEmbedYoutube() {
// are we on youtube?
const youtubeLocation = window.location.href.match(/https:\/\/.*youtube.com\/.*\?.*v=([^&]+)/)
const youtubeId = youtubeLocation[1] || null
if (youtubeId) {
// if yes, get the id and show the embed, and get rid of the fucking request for cookies
const embedUrl = window.location.origin + '/embed/' + youtubeId
window.location.href = embedUrl
}
}
function hideAnnoyances() {
// hide the page annoying elements
const hideElements = Array.from(
document.querySelectorAll(annoyanceCssSelectors.join(','))
)
.filter(n => n.style.display !== 'none')
hideElements
.map(n => { n.style.display = 'none' })
//.map(n => n.remove())
if (hideElements.length) console.debug(hideElements)
return hideElements.length
}
function silence() {
const skipButtons = Array.from(document.querySelectorAll('.ytp-ad-skip-ad-slot,.ytp-ad-skip-button'))
if (skipButtons.length > 0) {
if (!silenced) {
silenced = true
console.debug('Dispatch keydown="m"')
document.querySelector('.ytp-unmute-icon').dispatchEvent(CLICK_EVENT)
// document.querySelector('.ytp-unmute-icon').dispatchEvent(new KeyboardEvent('keydown',{'key':'m'}));
}
// chrome.tabs.query({url: []}, function (tabs) {
// for (var i = 0; i < tabs.length; i++) {
// var mutedInfo = tabs[i].mutedInfo;
// if (mutedInfo) chrome.tabs.update(tabs[i].id, {"muted": true})
// }
// })
} else if (silenced) {
// restore audio
silenced = false
console.debug('Dispatch keydown="m"')
document.querySelector('.ytp-unmute-icon').dispatchEvent(CLICK_EVENT)
// document.querySelector('.ytp-unmute-icon').dispatchEvent(new KeyboardEvent('keydown',{'key':'m'}));
}
return silenced ? 1 : 0
}
function skipAd() {
const skipButton = document.querySelector('.ytp-ad-skip-button')
if (skipButton) {
skipButton.click()
}
return skipButton ? 1 : 0
}
function fuckOff() {
// sum the number of performed operations
const done = hideAnnoyances() + skipAd() + silence()
// reduce the frequency of the checks, when there's nothing to remove
if (done > 0) {
hideTimeout = DEFAULT_TIMEOUT
} else {
// hideTimeout = hideTimeout*2
}
console.info('YT fucked off ' + done + ' times. Next check in ' + hideTimeout + 'ms')
if (hideTimeout / DEFAULT_TIMEOUT < 5) {
// monitor the page, until the timeout is too big (assume that everything was done)
// TODO: what about ADs in the middle of the video?
setTimeout(fuckOff, hideTimeout)
}
}
/////////////////////////////////////////////////////////////////////////////////
// prevent any iframe from being displayed
const element = document.createElement('style')
const hideRules = 'display: none !important; visibility: hidden; height: 0; width: 0; position: absolute; position: -9999999px;'
// Append style element to head
document.head.appendChild(element)
element.sheet.insertRule('iframe { '+ hideRules +' }')
annoyanceCssSelectors.map(selector => element.sheet.insertRule(selector + ' { '+ hideRules +' }'))
// allow to display the embed version, by pressing 'Y'
window.onkeyup = function(e){
if (e.target.value || e.target.tagName === 'TEXTAREA') {
// skip when typing into an input
return false;
}
var pressed = "";
if(e.shiftKey){
pressed += " + Shift";
}else if(e.ctrlKey){
pressed += " + Ctrl";
} //and so on
pressed += e.keyCode;
console.log(pressed);
if (pressed == 89 /* Y button */) {
goToEmbedYoutube();
}
}
fuckOff()
})();
@Pictor13
Copy link
Author

Pictor13 commented Dec 3, 2020

  • prevent to intercept Y (for embed URL) when typing inside an input control
  • hide by default all the page IFRAMEs
  • periodically check the page for
    • elements to remove
    • skip buttons to click
    • try to mute video when an AD is visualized

TODO:

  • make video silencing work, when there are ADs, and restore it when ADs end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment