Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Youtube Special Comment Sticker
- // @namespace http://tampermonkey.net/
- // @version 0.1
- // @description try to take over the world!
- // @author You
- // @include https://www.youtube.com/live_chat*
- // @grant none
- // ==/UserScript==
- (() => {
- let IS_DARK = document.querySelector('html').hasAttribute('dark');
- let canScroll = true;
- let chat;
- let itemScroller;
- let scrollButton;
- let sticky;
- let stickyItems;
- let stickyItemsContainer;
- let toggleSticky;
- let toggleStickyText;
- let scrollSticky = () => {
- if (canScroll) {
- stickyItems.scrollTop = stickyItems.scrollHeight;
- }
- }
- let isSpecial = (authorElement) => {
- return authorElement.classList.contains('moderator') || authorElement.classList.contains('owner');
- };
- let stickItem = (node) => {
- let authorElement = node.querySelector('#author-name');
- if (authorElement && isSpecial(authorElement)) {
- let stickyItem = document.createElement('div');
- let authorPhoto = document.createElement('div');
- let content = document.createElement('div');
- let timestamp = document.createElement('span');
- let author = document.createElement('div');
- let authorName = document.createElement('span');
- content.id = 'content';
- stickyItem.classList.add('sticky-item');
- content.classList.add('style-scope', 'yt-live-chat-text-message-renderer');
- timestamp.classList.add('timestamp');
- author.classList.add('author');
- authorName.classList.add('author-name');
- authorElement.classList.contains('owner') ? authorName.classList.add('owner') : null;
- authorPhoto.innerHTML = node.querySelector('#author-photo').outerHTML.replace(/(\<|\/)(yt\-img\-shadow)/g, '$1div');
- timestamp.innerText = node.querySelector('#timestamp').innerText;
- authorName.innerText = authorElement.innerText;
- author.append(authorName);
- content.append(timestamp);
- content.append(author);
- content.append(node.querySelector('#message').cloneNode(true));
- stickyItem.append(authorPhoto.firstChild);
- stickyItem.append(content);
- stickyItems.append(stickyItem);
- if (sticky.style.display === 'none') {
- chat = document.querySelector('#chat');
- itemScroller = chat.querySelector('#item-scroller');
- sticky.style.display = 'inherit';
- chat.setAttribute('style', `max-height: ${chat.offsetHeight - sticky.offsetHeight}px;`);
- itemScroller.scrollTop = itemScroller.scrollHeight;
- }
- scrollSticky();
- }
- };
- let monitor = () => {
- let chatItems = document.querySelector('#items.style-scope.yt-live-chat-item-list-renderer');
- let observer = new MutationObserver((mutations) => {
- mutations.forEach((mutation) => {
- mutation.addedNodes.forEach(stickItem);
- });
- });
- chatItems.querySelectorAll('yt-live-chat-text-message-renderer').forEach(stickItem);
- observer.observe(chatItems, { childList: true });
- };
- let setUpCss = () => {
- let style = document.createElement('style');
- document.head.appendChild(style);
- style.sheet.insertRule('#sticky {position: relative;}');
- style.sheet.insertRule('#sticky #show-hide-button {border-bottom: #e0e0e0 solid 1px; font-size: 11px;}');
- style.sheet.insertRule('#sticky #show-hide-button div {font-size: 11px;}');
- style.sheet.insertRule('#sticky #show-hide-button div a {color: #11111199; display: flex;}');
- style.sheet.insertRule('#sticky #show-hide-button div a:hover {color: #030303;}');
- style.sheet.insertRule('#sticky #show-hide-button div a #button {border-radius: 0; margin: 0; padding: 8px 24px; width: 100%;}');
- style.sheet.insertRule('#sticky #show-hide-button div a #button #text {font-weight: 500;}');
- style.sheet.insertRule('#sticky #chat-header {box-sizing: border-box; display: flex; border-bottom: #e0e0e0 solid 1px; background-color: #fffffffa; height: 150px; padding: 0 0 0 4px; transition: all .15s cubic-bezier(0.0, 0.0, 0.2, 1);}');
- style.sheet.insertRule('#sticky yt-icon-button {z-index: 0; transition: all .15s cubic-bezier(0.0, 0.0, 0.2, 1); visibility: hidden;}');
- style.sheet.insertRule('#sticky #primary-content {height: inherit; overflow-x: hidden;}');
- style.sheet.insertRule('#sticky #primary-content::-webkit-scrollbar {content: "";}');
- style.sheet.insertRule('#sticky #primary-content::-webkit-scrollbar-track {background-color: #f1f1f1;}');
- style.sheet.insertRule('#sticky #primary-content::-webkit-scrollbar-thumb {border: 2px solid #f1f1f1; min-height: 30px;}');
- style.sheet.insertRule('#sticky #primary-content .sticky-item {padding: 4px; font-size: 13px; display: flex;}');
- style.sheet.insertRule('#sticky #primary-content .sticky-item #author-photo {display: inline-table;}');
- style.sheet.insertRule('#sticky #primary-content .sticky-item #img {max-width: inherit;}');
- style.sheet.insertRule('#sticky #primary-content .timestamp {display: inline; color: #11111166; font-size: 11px; margin-right: 8px;}');
- style.sheet.insertRule('#sticky #primary-content .author {display: inline-flex; margin-right: 8px;}');
- style.sheet.insertRule('#sticky #primary-content .author .author-name {color: #5f84f1; font-weight: 500;}');
- style.sheet.insertRule('#sticky #primary-content .author .author-name.owner {color: #000000de; background-color: #ffd600; padding: 2px 4px; border-radius: 2px;}');
- style.sheet.insertRule('#sticky #primary-content #message {display: inline; line-height: 17px;}');
- style.sheet.insertRule('#sticky.dark #show-hide-button {border-color: #303030; background-color: #181818;}');
- style.sheet.insertRule('#sticky.dark #show-hide-button div a {color: #ffffffb3;}');
- style.sheet.insertRule('#sticky.dark #show-hide-button div a:hover {color: #ffffff;}');
- style.sheet.insertRule('#sticky.dark #chat-header {border-color: #303030; background-color: #202020;}');
- style.sheet.insertRule('#sticky.dark #primary-content::-webkit-scrollbar-track {background-color: #0f0f0f;}');
- style.sheet.insertRule('#sticky.dark #primary-content::-webkit-scrollbar-thumb {background-color: hsla(0, 0%, 53.3%, .2); border-color: #0f0f0f;');
- style.sheet.insertRule('#sticky.dark #primary-content .timestamp {color: #ffffff8a;}');
- style.sheet.insertRule('#sticky.dark #primary-content .author .author-name {color: #5e84f1;}');
- style.sheet.insertRule('#sticky.dark #primary-content .author .author-name.owner {color: #111111;}');
- style.sheet.insertRule('#sticky.hide-timestamps #primary-content .timestamp {display: none;}');
- };
- let setUpSticky = () => {
- let hideTimestamp = document.querySelector('yt-live-chat-renderer').hasAttribute('hide-timestamps');
- let stickyAnchor = document.querySelector('yt-live-chat-app');
- sticky = document.createElement('div');
- sticky.id = 'sticky';
- sticky.setAttribute('class', `style-scope ytd-watch-flexy ${IS_DARK ? 'dark' : ''} ${hideTimestamp ? 'hide-timestamps': ''}`);
- sticky.style.display = 'none';
- sticky.innerHTML = '<div id="show-hide-button" class="style-scope ytd-live-chat-frame"><div class="style-scope ytd-live-chat-frame" use-keyboard-focused="" is-paper-button="" button-renderer="true"><a class="yt-simple-endpoint style-scope ytd-toggle-button-renderer" tabindex="-1"><paper-button id="button" class="style-scope ytd-toggle-button-renderer" role="button" tabindex="0" animated="" elevation="0" aria-disabled="false"><yt-formatted-string id="text" class="style-scope ytd-toggle-button-renderer"></yt-formatted-string><paper-ripple class="style-scope paper-button"><div id="background" class="style-scope paper-ripple"></div><div id="waves" class="style-scope paper-ripple"></div></paper-ripple></paper-button></a></div></div>'
- sticky.innerHTML += '<div id="chat-header" role="heading" class="style-scope yt-live-chat-renderer"><div id="primary-content" class="style-scope yt-live-chat-header-renderer"></div>';
- sticky.innerHTML += '<yt-icon-button id="show-more" class="style-scope yt-live-chat-item-list-renderer" style="transform: translateY(42px);"><button id="button" class="style-scope yt-icon-button"><button id="button" class="style-scope yt-icon-button" aria-label="More comments below"><yt-icon icon="down_arrow" class="style-scope yt-live-chat-item-list-renderer"><svg viewBox="0 0 24 24" preserveAspectRatio="xMidYMid meet" focusable="false" class="style-scope yt-icon" style="pointer-events: none; display: block; width: 100%; height: 100%;"><g class="style-scope yt-icon"><path d="M20 12l-1.41-1.41L13 16.17V4h-2v12.17l-5.58-5.59L4 12l8 8 8-8z" class="style-scope yt-icon"></path></g></svg></yt-icon></button></button><paper-ripple class="style-scope yt-icon-button circle"><div id="background" class="style-scope paper-ripple" style="opacity: 0.006272;"></div><div id="waves" class="style-scope paper-ripple"></div></paper-ripple></yt-icon-button>'
- stickyItemsContainer = sticky.querySelector('#chat-header');
- toggleSticky = sticky.querySelector('#show-hide-button');
- toggleStickyText = toggleSticky.querySelector('#text');
- scrollButton = sticky.querySelector('yt-icon-button');
- scrollButton.querySelector('#button').style.height = 'auto';
- stickyAnchor.parentNode.insertBefore(sticky, stickyAnchor);
- stickyAnchor.style.position = 'relative';
- toggleStickyText.innerText = 'Hide Sticky';
- sticky.querySelectorAll('#title, #view-selector, #action-buttons, #overflow').forEach(element => element.parentNode.removeChild(element));
- stickyItems = sticky.querySelector('#primary-content');
- };
- let monitorDarkTheme = () => {
- let observer = new MutationObserver(() => {
- sticky.classList.toggle('dark');
- });
- observer.observe(document.querySelector('html'), {
- attributes: true,
- attributeFilter: ['dark']
- });
- };
- let monitorTimestampToggle = () => {
- let observer = new MutationObserver(() => {
- sticky.classList.toggle('hide-timestamps');
- scrollSticky();
- });
- observer.observe(document.querySelector('yt-live-chat-renderer'), {
- attributes: true,
- attributeFilter: ['hide-timestamps']
- });
- };
- let setUpEvents = () => {
- stickyItems.addEventListener('scroll', () => {
- setTimeout(() => {
- canScroll = stickyItems.scrollHeight - stickyItems.scrollTop === stickyItems.clientHeight;
- scrollButton.style.transform = `translateY(${canScroll ? 42 : 0}px)`;
- scrollButton.style.visibility = canScroll ? 'hidden' : 'visible';
- });
- });
- scrollButton.addEventListener('click', () => {
- canScroll = true;
- stickyItems.scrollTop = stickyItems.scrollHeight;
- });
- toggleSticky.addEventListener('click', () => {
- if (stickyItemsContainer.offsetHeight > 0) {
- chat.setAttribute('style', `max-height: ${chat.offsetHeight + 150}px;`);
- toggleStickyText.innerText = 'Show Sticky';
- stickyItemsContainer.setAttribute('style', 'border: none;');
- stickyItemsContainer.style.height = '0px';
- } else {
- chat.setAttribute('style', `max-height: ${chat.offsetHeight - 150}px;`);
- itemScroller.scrollTop += 150;
- toggleStickyText.innerText = 'Hide Sticky';
- stickyItemsContainer.setAttribute('style', '');
- stickyItemsContainer.style.height = '150px';
- }
- });
- };
- let initMonitoring = () => {
- setUpCss();
- setUpSticky();
- monitor();
- monitorDarkTheme();
- monitorTimestampToggle();
- setUpEvents();
- };
- let init = () => {
- if (window.top.document.readyState === 'complete') {
- initMonitoring();
- } else {
- window.top.addEventListener('load', initMonitoring);
- }
- };
- init();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement