YouTube Keep Video When Scrolling

Keeps the video in the same place when scrolling on a YouTube page.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Keep Video When Scrolling
// @namespace    http://skoshy.com
// @version      0.2
// @description  Keeps the video in the same place when scrolling on a YouTube page.
// @author       Stefan Koshy
// @match        https://www.youtube.com/watch?*
// @grant        none
// ==/UserScript==
/* jshint -W097 */
'use strict';

var id = 'youtubeKeepVideoWhenScrolling';

var keepVideoStyle = `
    #player .player-api {
      position: fixed;
      padding-top: 12px;
      padding-bottom: 10px;
      background: #f1f1f1;
      margin-top: -1px;
    }
    .watch-non-stage-mode #player .player-api {
      box-shadow: 1px 1px rgba(241,241,241,1),-1px 1px rgba(241,241,241,1);
    }
    .watch-stage-mode #player .player-api {
      padding-top:10px;
      padding-bottom: 0;
      margin-top: 0;
    }
`;

var otherStyle = `
    #`+id+`PinButton {
      box-sizing: border-box;
      width: 32px 
    }

    #`+id+`PinButton img {
      width: 100%;
      filter: invert(1);
      -webkit-filter: invert(1);
      padding: 3px;
      box-sizing: border-box;
    }
`;


function addGlobalStyle(css, cssID) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.id = cssID;
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}

addGlobalStyle(keepVideoStyle, id+'CSS');
addGlobalStyle(otherStyle, id+'OtherCSS');

// add the button to the player
var ytControls = document.querySelector('.ytp-right-controls');
var ytSettingsButton = document.querySelector('.ytp-settings-button');

var pinButton = document.createElement('button');
pinButton.innerHTML = '<img src="http://i.imgur.com/m4vqtk1.png"/>';
pinButton.id = id+'PinButton';
pinButton.className = 'ytp-button';
pinButton.onclick = function() {
    var style = document.querySelector('#'+id+'CSS');
    if (style.disabled === false)
        style.disabled = true;
    else
        style.disabled = false;
};

ytControls.insertBefore(pinButton, ytSettingsButton);