// ==UserScript==
// @author Mr. Nope
// @version 1.4
// @name PornHub Plus
// @description A kinder PornHub. Because you're worth it.
// @namespace Nope
// @date 2019-02-22
// @include *pornhub.com/*
// @run-at document-start
// @grant none
// @license Public Domain
// @icon http://www.techthisoutnews.com/wp-content/uploads/2018/03/porn.jpg
// @grant GM_addStyle
// ==/UserScript==
(() => {
const OPTIONS = {
openWithoutPlaylist: JSON.parse(localStorage.getItem('plus_openWithoutPlaylist')) || false,
showOnlyVerified: JSON.parse(localStorage.getItem('plus_showOnlyVerified')) || false,
redirectToVideos: JSON.parse(localStorage.getItem('plus_redirectToVideos')) || false,
autoresizePlayer: JSON.parse(localStorage.getItem('plus_autoresizePlayer')) || false,
hideWatchedVideos: JSON.parse(localStorage.getItem('plus_hideWatchedVideos')) || false,
durationFilter: JSON.parse(localStorage.getItem('plus_durationFilter')) || { max: 0, min: 0 }
}
/* Styles - Shared between all "Plus" userscripts */
const sharedStyles = `
/* Our own elements */
.plus-buttons {
background: rgba(27, 27, 27, 0.9);
box-shadow: 0px 0px 12px rgba(20, 111, 223, 0.9);
font-size: 12px;
position: fixed;
bottom: 10px;
padding: 10px 22px 8px 24px;
right: 0;
z-index: 100;
transition: all 0.3s ease;
/* Negative margin-right calculated later based on width of buttons */
}
.plus-buttons:hover {
box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.3);
}
.plus-buttons .plus-button {
margin: 10px 0;
padding: 6px 15px;
border-radius: 4px;
font-weight: 700;
display: block;
position: relative;
text-align: center;
vertical-align: top;
cursor: pointer;
border: none;
text-decoration: none;
}
.plus-buttons a.plus-button {
background: rgb(221, 221, 221);
color: rgb(51, 51, 51);
}
.plus-buttons a.plus-button:hover {
background: rgb(187, 187, 187);
color: rgb(51, 51, 51);
}
.plus-buttons a.plus-button.plus-button-isOn {
background: rgb(20, 111, 223);
color: rgb(255, 255, 255);
}
.plus-buttons a.plus-button.plus-button-isOn:hover {
background: rgb(0, 91, 203);
color: rgb(255, 255, 255);
}
.plus-hidden {
display: none !important;
}
`;
/* Styles - Color theme */
const themeStyles = `
.plus-buttons {
box-shadow: 0px 0px 12px rgba(255, 153, 0, 0.85);
}
.plus-buttons:hover {
box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.3);
}
.plus-buttons a.plus-button {
background: rgb(47, 47, 47);
color: rgb(172, 172, 172);
}
.plus-buttons a.plus-button:hover {
background: rgb(79, 79, 79);
color: rgb(204, 204, 204);
}
.plus-buttons a.plus-button.plus-button-isOn {
background: rgb(255, 153, 0);
color: rgb(0, 0, 0);
}
.plus-buttons a.plus-button.plus-button-isOn:hover {
background: rgb(255, 153, 0);
color: rgb(255, 255, 255);
}
`;
/* Styles - General site-specific */
const generalStyles = `
/* Hide elements */
.abovePlayer,
.streamatesModelsContainer,
#headerUpgradePremiumBtn,
#headerUploadBtn,
#PornhubNetworkBar,
#js-abContainterMain,
#hd-rightColVideoPage > :not(#relatedVideosVPage) {
display: none !important;
}
/* Show all playlists without scrolling in "add to" */
.slimScrollDiv {
height: auto !important;
}
#scrollbar_watch {
max-height: unset !important;
}
/* Hide premium video from related videos sidebar */
#relateRecommendedItems li:nth-of-type(5) {
display: none !important;
}
/* Prevent animating player size change on each page load */
#main-container .video-wrapper #player.wide {
transition: none !important;
}
`;
/**
* Run after page has loaded
*/
window.addEventListener('DOMContentLoaded', () => {
/**
* References to video element and container if they exist on the page
*/
const player = document.querySelector('#player');
const video = document.querySelector('video');
/**
* Creation of option buttons
*/
const scrollButton = document.createElement('a');
const scrollButtonText = document.createElement('span');
const verifiedButton = document.createElement('a');
const verifiedButtonText = document.createElement('span');
const verifiedButtonState = getButtonState(OPTIONS.showOnlyVerified);
const hideWatchedButton = document.createElement('a');
const hideWatchedButtonText = document.createElement('span');
const hideWatchedButtonState = getButtonState(OPTIONS.hideWatchedVideos);
const redirectToVideosButton = document.createElement('a');
const redirectToVideosButtonText = document.createElement('span');
const redirectToVideosButtonState = getButtonState(OPTIONS.redirectToVideos);
const autoresizeButton = document.createElement('a');
const autoresizeButtonText = document.createElement('span');
const autoresizeButtonState = getButtonState(OPTIONS.autoresizePlayer);
const durationShortButton = document.createElement('a');
const durationShortButtonText = document.createElement('span');
const durationShortButtonState = getButtonState(!OPTIONS.durationFilter.min);
const openWithoutPlaylistButton = document.createElement('a');
const openWithoutPlaylistButtonText = document.createElement('span');
const openWithoutPlaylistButtonState = getButtonState(OPTIONS.openWithoutPlaylist);
/**
* Returns an `on` or `off` CSS class name based on the boolean evaluation
* of the `state` parameter, as convenience method when setting UI state.
*/
function getButtonState(state) {
return state ? 'plus-button-isOn' : 'plus-button-isOff';
}
scrollButtonText.textContent = "Scroll to top";
scrollButtonText.classList.add('text');
scrollButton.appendChild(scrollButtonText);
scrollButton.classList.add('plus-button');
scrollButton.addEventListener('click', () => {
window.scrollTo({ top: 0 });
});
verifiedButtonText.textContent = 'Show only verified';
verifiedButtonText.classList.add('text');
verifiedButton.appendChild(verifiedButtonText);
verifiedButton.classList.add(verifiedButtonState, 'plus-button');
verifiedButton.addEventListener('click', () => {
OPTIONS.showOnlyVerified = !OPTIONS.showOnlyVerified;
localStorage.setItem('plus_showOnlyVerified', OPTIONS.showOnlyVerified);
if (OPTIONS.showOnlyVerified) {
verifiedButton.classList.replace('plus-button-isOff', 'plus-button-isOn');
} else {
verifiedButton.classList.replace('plus-button-isOn', 'plus-button-isOff');
}
filterVideos();
});
hideWatchedButtonText.textContent = 'Hide watched videos';
hideWatchedButtonText.classList.add('text');
hideWatchedButton.appendChild(hideWatchedButtonText);
hideWatchedButton.classList.add(hideWatchedButtonState, 'plus-button');
hideWatchedButton.addEventListener('click', () => {
OPTIONS.hideWatchedVideos = !OPTIONS.hideWatchedVideos;
localStorage.setItem('plus_hideWatchedVideos', OPTIONS.hideWatchedVideos);
if (OPTIONS.hideWatchedVideos) {
hideWatchedButton.classList.replace('plus-button-isOff', 'plus-button-isOn');
} else {
hideWatchedButton.classList.replace('plus-button-isOn', 'plus-button-isOff');
}
filterVideos();
});
redirectToVideosButtonText.textContent = 'Redirect profiles to uploads';
redirectToVideosButtonText.classList.add('text');
redirectToVideosButton.appendChild(redirectToVideosButtonText);
redirectToVideosButton.classList.add(redirectToVideosButtonState, 'plus-button');
redirectToVideosButton.addEventListener('click', () => {
OPTIONS.redirectToVideos = !OPTIONS.redirectToVideos;
localStorage.setItem('plus_redirectToVideos', OPTIONS.redirectToVideos);
if (OPTIONS.redirectToVideos) {
redirectToVideosButton.classList.replace('plus-button-isOff', 'plus-button-isOn');
} else {
redirectToVideosButton.classList.replace('plus-button-isOn', 'plus-button-isOff');
}
});
durationShortButtonText.textContent = 'Show short videos (< 8 min)';
durationShortButtonText.classList.add('text');
durationShortButton.appendChild(durationShortButtonText);
durationShortButton.classList.add(durationShortButtonState, 'plus-button');
durationShortButton.addEventListener('click', () => {
OPTIONS.durationFilter.min = OPTIONS.durationFilter.min ? 0 : 8;
localStorage.setItem('plus_durationFilter', JSON.stringify(OPTIONS.durationFilter));
if (!OPTIONS.durationFilter.min) {
durationShortButton.classList.replace('plus-button-isOff', 'plus-button-isOn');
filterVideos();
} else {
durationShortButton.classList.replace('plus-button-isOn', 'plus-button-isOff');
filterVideos();
}
});
autoresizeButtonText.textContent = 'Auto-resize player';
autoresizeButtonText.classList.add('text');
autoresizeButton.appendChild(autoresizeButtonText);
autoresizeButton.classList.add(autoresizeButtonState, 'plus-button');
autoresizeButton.addEventListener('click', () => {
OPTIONS.autoresizePlayer = !OPTIONS.autoresizePlayer;
localStorage.setItem('plus_autoresizePlayer', OPTIONS.autoresizePlayer);
if (OPTIONS.autoresizePlayer) {
autoresizeButton.classList.replace('plus-button-isOff', 'plus-button-isOn');
} else {
autoresizeButton.classList.replace('plus-button-isOn', 'plus-button-isOff');
}
});
openWithoutPlaylistButtonText.textContent = 'Open playlist videos without slider';
openWithoutPlaylistButtonText.classList.add('text');
openWithoutPlaylistButton.appendChild(openWithoutPlaylistButtonText);
openWithoutPlaylistButton.classList.add(openWithoutPlaylistButtonState, 'plus-button');
openWithoutPlaylistButton.addEventListener('click', () => {
OPTIONS.openWithoutPlaylist = !OPTIONS.openWithoutPlaylist;
localStorage.setItem('plus_openWithoutPlaylist', OPTIONS.openWithoutPlaylist);
if (OPTIONS.openWithoutPlaylist) {
openWithoutPlaylistButton.classList.replace('plus-button-isOff', 'plus-button-isOn');
} else {
openWithoutPlaylistButton.classList.replace('plus-button-isOn', 'plus-button-isOff');
}
updatePlaylistLinks();
});
/**
* Order option buttons in a container and add it to the page
*/
const buttons = document.createElement('div');
buttons.classList.add('plus-buttons');
buttons.appendChild(scrollButton);
buttons.appendChild(autoresizeButton);
buttons.appendChild(verifiedButton);
buttons.appendChild(hideWatchedButton);
buttons.appendChild(redirectToVideosButton);
buttons.appendChild(openWithoutPlaylistButton);
buttons.appendChild(durationShortButton);
document.body.appendChild(buttons);
/**
* General UI related functions
*/
/**
* Does the CSS changes necessary to enable cinema mode. In case this runs
* immediately on page load, we wait a couple of seconds for the player
* controls to load before telling the cinema mode button to appear active.
*/
function toggleCinemaMode() {
// Add and remove some CSS classes that control cinema view
player.classList.remove('original');
player.classList.add('wide');
document.querySelector('#hd-rightColVideoPage').classList.add('wide');
// Make sure cinema mode button appears as active
setTimeout(() => {
document.querySelector('.mhp1138_cinema').classList.add('mhp1138_active');
}, 2000);
}
/*
* Clicking a video on a playlist page opens it without the playlist at the
* top if the option is enabled.
*/
function updatePlaylistLinks() {
if (OPTIONS.openWithoutPlaylist) {
document.querySelectorAll('#videoPlaylist li a').forEach(link => {
link.href = link.href.replace('pkey', 'nopkey');
});
} else {
document.querySelectorAll('#videoPlaylist li a').forEach(link => {
link.href = link.href.replace('nopkey', 'pkey');
});
}
}
/**
* Allow scrolling the page when mouse hovers playlists in "add to", by
* cloning the playlist scroll container to remove the listeners that
* `preventDefault()`.
*/
function fixScrollContainer(container) {
if (container) {
container.parentNode.replaceChild(container.cloneNode(true), container);
}
}
/**
* Video thumbnail box related functions
*/
/**
* Checks if video box links to a video made by a verified member
*/
function videoIsVerified(box) {
return box.innerHTML.includes('Video of verified member');
}
/**
* Checks if the video box has a "watched" label on it (the video has
* already been viewed)
*/
function videoIsWatched(box) {
return box.querySelector('.watchedVideoText');
}
/**
* Checks if video box links to a video that is within the selected duration
* range, if one has been selected in options.
*/
function videoIsWithinDuration(box) {
// Parse integer minutes from video duration text
const mins = parseInt(box.querySelector('.duration').textContent.split(":")[0]);
const minMins = OPTIONS.durationFilter.min;
const maxMins = OPTIONS.durationFilter.max;
// If either max or min duration has been selected
if (minMins || maxMins) {
// If any max duration is set (otherwise defaults to 0 for no max)
const hasMaxDuration = !!maxMins;
// True if the video is shorther than we want (min defaults to 0)
const isBelowMin = mins < minMins;
// True if a max duration is set and the video exceeds it
const isAboveMax = hasMaxDuration && (mins > maxMins);
return !isBelowMin && !isAboveMax
}
}
/**
* Resets video thumbnail box to its original visible state.
*/
function resetVideo(box) {
showVideo(box);
}
/**
* Shows the video thumbnail box.
*/
function showVideo(box) {
box.classList.remove('plus-hidden');
}
/**
* Hides the video thumbnail box.
*/
function hideVideo(box) {
box.classList.add('plus-hidden');
}
/**
* Does the required checks to filter out unwanted video boxes according to
* options. Each box is reset to it's original visible state, then it's
* checked against relevant options to determine if it should be hidden or
* stay visible.
*/
function filterVideos() {
document.querySelectorAll('li.videoblock.videoBox').forEach(box => {
const state = {
verified: videoIsVerified(box),
watched: videoIsWatched(box),
inDurationRange: videoIsWithinDuration(box)
}
const shouldHide =
(OPTIONS.showOnlyVerified && !state.verified) ||
(OPTIONS.hideWatchedVideos && state.watched) ||
!state.inDurationRange;
// Reset the box to its original visible state so we can focus only on
// what to hide instead of also on what to unhide
resetVideo(box);
if (shouldHide) {
hideVideo(box);
}
});
}
/**
* Initialize video pages (with a valid video element)
*/
if (/^https:\/\/www\.pornhub\.com\/view_video.php/.test(window.location.href) && player) {
if (OPTIONS.autoresizePlayer) {
toggleCinemaMode();
}
// Let us scroll the page despite the mouse pointer hovering over "Add to"
// playlist area
const scrollContainer = document.querySelector('#scrollbar_watch');
if (scrollContainer) {
fixScrollContainer(scrollContainer);
}
}
/**
* Initialize for any page that contains a video box
*/
if (document.querySelector('.videoBox')) {
setTimeout(() => {
filterVideos();
}, 1000);
}
/**
* Initialize profile pages, channel pages, user pages, star pages
*/
if (
/^https:\/\/www\.pornhub\.com\/pornstar\/([^\/]+)$/.test(window.location.href) ||
/^https:\/\/www\.pornhub\.com\/model\/([^\/]+)$/.test(window.location.href) ||
/^https:\/\/www\.pornhub\.com\/users\/([^\/]+)$/.test(window.location.href) ||
/^https:\/\/www\.pornhub\.com\/channels\/([^\/]+)$/.test(window.location.href)) {
// Redirect profile and channel pages straight to their video uploads
// page if the setting is enabled, except if we just came from the video
// page (don't loop back).
if (OPTIONS.redirectToVideos && !/.+\/videos\/upload.*/.test(document.referrer)) {
window.location.href = window.location.href + '/videos/upload';
}
}
/*
* Add styles
*/
GM_addStyle(sharedStyles);
GM_addStyle(themeStyles);
GM_addStyle(generalStyles);
/*
* Add dynamic styles
*/
const dynamicStyles = `
.plus-buttons {
margin-right: -${buttons.getBoundingClientRect().width - 23}px;
}
.plus-buttons:hover {
margin-right: 0;
}
`;
GM_addStyle(dynamicStyles);
});
})();