// ==UserScript== // @name YouTube Click To Play // @name:ja YouTube Click To Play // @name:zh-CN YouTube Click To Play // @namespace knoa.jp // @description It disables autoplay and enables click to play. // @description:ja 自動再生を無効にし、クリックで再生するようにします。 // @description:zh-CN 它禁用自动播放,并启用点击播放。 // @include https://www.youtube.com/* // @noframes // @run-at document-start // @grant none // @version 1.1.2 // @downloadURL none // ==/UserScript== (function(){ const SCRIPTID = 'YouTubeClickToPlay'; const SCRIPTNAME = 'YouTube Click To Play'; const DEBUG = false;/* [update] 1.1.2 fix for background ads. [bug] [todo] [possible] t=n 指定があればむしろサムネではなくその時点の映像にしてあげる? 0秒で常にサムネに戻る仕様?(seekingイベントでよい) channel/ と watch/ は個別に設定可能とか => channelだけで動作する別スクリプトがある document.hidden でのみ作動するオプションとか [research] シアターモードの切り替えで再生してしまう件(そこまで気にしなくてもいい気もする) [memo] 本スクリプト仕様: サムネになってほしい: チャンネルホーム, ビデオページ 再生してほしい: LIVE, 広告, 途中広告からの復帰 要確認: 各ページの行き来, 再生で即停止しないこと, シアターモードの切り替え, 背面タブでの起動 (YouTubeによるあっぱれなユーザー体験の追究のおかげで、初回読み込み時に限り再生開始済みのvideo要素が即出現する) YouTube仕様: 画面更新(URL Enter, S-Reload, Reload に本質的な差異なし) 新規タブ(開いた直後, 読み込み完了後, title変更後 に本質的な差異なし) video: body ... video ... loadstart ... で必ず play() されるのでダミーと入れ替えておけばよい。 video要素は #player-api 内に出現した後に ytd-watch-flexy 内に移動する。その際に play() されるようだ。 t=123 のような時刻指定があると seeking 後にもう一度 play() される。 thumbnail は t=4 以下だとなぜか消えてしまう。(seekじゃなくてadvanceだとみなされるせい?) channel: body ... video ... loadstart で即 pause() 可能。(playは踏まれない) 画面遷移(動画 <=> LIVE <=> チャンネル) video: yt-navigate-start ... loadstart で即 pause() 可能。(playは踏まれない) 広告 冒頭広告: .ad-showing 依存だが判定できる。 広告明け: 少しだけ泥臭いが、そのURLで一度でも本編が再生されていれば広告明けとみなす。 広告が入ると広告のサムネイルがセットされた状態になるので、独自にセットしなければならない。 Firefoxなどのブラウザが動画の自動再生をくい止めた場合に備えて広告のサムネイルになると推測。 参考: Channelトップの動画でのみ機能するスクリプト https://greasyfork.org/ja/scripts/399862-kill-youtube-channel-video-autoplay */ if(window === top && console.time) console.time(SCRIPTID); const MS = 1, SECOND = 1000*MS, MINUTE = 60*SECOND, HOUR = 60*MINUTE, DAY = 24*HOUR, WEEK = 7*DAY, MONTH = 30*DAY, YEAR = 365*DAY; const THUMBNAILURL = 'https://i.ytimg.com/vi/{id}/maxresdefault.jpg'; const FLAGNAME = SCRIPTID.toLowerCase(); const site = { get: { moviePlayer: () => $('#movie_player'), spinner: () => $('.ytp-spinner'), video: () => $(`video:not([data-${FLAGNAME}])`), videoId: (url) => (new URL(url)).searchParams.get('v'), startTime: () => { /* t=1h0m0s or t=3600 */ let t = (new URL(location.href)).searchParams.get('t'); if(t === null) return; let [h, m, s] = t.match(/^(?:([0-9]+)h)?(?:([0-9]+)m)?(?:([0-9]+)s?)?$/).slice(1).map(n => parseInt(n || 0)); return 60*60*h + 60*m + s; }, }, is: { immediate: (video) => $('#player-api', player => player.contains(video)), live: () => $('.ytp-time-display.ytp-live') !== null, ad: () => $('#movie_player.ad-showing') !== null, list: () => (new URL(location)).searchParams.get('list') !== null, autoplay: () => $('ytd-watch-next-secondary-results-renderer paper-toggle-button.ytd-compact-autoplay-renderer', button => button.checked), }, views: { channel: { url: /^https:\/\/www\.youtube\.com\/(channel|c|user)\//, get: { thumbnailOverlayImage: () => $('.ytp-cued-thumbnail-overlay-image'), thumbnailURL: () => THUMBNAILURL.replace('{id}', view.get.videoId()), videoId: () => $('a.ytp-title-link[href]', a => site.get.videoId(a.href)), }, }, watch: { url: /^https:\/\/www\.youtube\.com\/watch\?/, get: { thumbnailOverlayImage: () => $('.ytp-cued-thumbnail-overlay-image'), thumbnailURL: () => THUMBNAILURL.replace('{id}', view.get.videoId()), videoId: () => site.get.videoId(location.href), upnextId: () => $('ytd-compact-autoplay-renderer a[href]', a => site.get.videoId(a.href)), playlistAutoplayInsertBefore: () => $('ytd-watch-flexy #playlist-actions #save-button'), autoplayLabel: () => $('#upnext + #autoplay'), }, }, }, }; let elements = {}, flags = {}, view; const core = { initialize: function(){ elements.html = document.documentElement; elements.html.classList.add(SCRIPTID); core.findVideo(); core.addStyle('style'); }, findVideo: function(){ const found = function(video){ //log(video); if(video.dataset[FLAGNAME]) return; video.dataset[FLAGNAME] = 'found'; core.listenNavigation(); core.listenVideo(video); }; /* if a video already exists */ let video = site.get.video(); if(video) found(video); /* unavoidably observate body for immediate catch */ observe(document.documentElement, function(records){ let video = site.get.video(); if(video) found(video); }, {childList: true, subtree: true}); }, listenNavigation: function(){ /* listen navigation (observe URL changes) */ if(flags.listeningNavigation !== undefined) return; flags.listeningNavigation = true; let listener = function(e){ //log(e.type, location.href); delete flags.upnextId;/* reset the upnext video id */ delete flags.shownAd;/* reset the shown ad status */ delete flags.playedOnce;/* reset the played once status */ view = core.getView(site.views); if(view && view.key === 'watch'){ flags.upnextId = view.get.upnextId(); if(site.is.list()) core.setPlaylistAutoplay(); else core.getAutoplayLabel(); } }; document.addEventListener('yt-navigate-start', listener);/* click a link */ window.addEventListener('popstate', listener);/* browser back or foward */ listener({type: 'theVeryFirst'});/* at the very first */ }, listenVideo: function(video){ let shouldStop = function(video){ if(site.is.live()) return log('this is a live and should not stop playing'); if(site.is.ad()) return log('this is an ad and should not stop playing.'), flags.shownAd = true;/* shown ad on the current location */ if(site.is.list() && flags.autoplayOnPlaylist) return log('this is on the playlist and should not stop playing.'); if(site.is.autoplay() && flags.upnextId === view.get.videoId()) return log('site is set to autoplay and should not stop playing.'); if(flags.playedOnce) return log('the ad has just closed and the video should continue playing.'); return true; }; /* for the very immediate time */ //log(video.currentSrc, 'paused:' + video.paused, 'currentTime:' + video.currentTime); if(shouldStop()){ core.stopAutoplay(video); core.stopImmediateAutoplay(video); } /* the video element just changes its src attribute on any case */ video.addEventListener('loadstart', function(e){ //log(e.type, video.currentSrc, 'paused:' + video.paused, 'currentTime:' + video.currentTime, flags.shownAd ? 'shownAd' : '', flags.playedOnce ? 'playedOnce' : ''); if(shouldStop()){ core.stopAutoplay(video); /* ads just finished and the video is starting */ if(!site.is.ad() && flags.shownAd && !flags.playedOnce) video.addEventListener('canplay', function(e){ //log(e.type); core.imitateUnstarted(video); }, {once: true}); } }); /* memorize played status for restarting playing or not on after ads */ video.addEventListener('playing', function(e){ //log(e.type, 'currentTime:' + video.currentTime); if(!site.is.ad() && !flags.playedOnce) return flags.playedOnce = true;/* played once on the current location */ }); }, stopAutoplay: function(video){ //log(); video.autoplay = false; video.pause(); }, stopImmediateAutoplay: function(video){ let count = 0, isImmediate = site.is.immediate(video), startTime = site.get.startTime(); //log('isImmediate:' + isImmediate, 'startTime:' + startTime, 'currentTime:' + video.currentTime); if(isImmediate) count++;/* for the very first view of the YouTube which plays a video automatically for immediate user experience */ if(startTime) count++;/* for starting again from middle after seeking with query like t=123 */ if(count){ video.originalPlay = video.play; video.play = function(){ //log('(play)', 'count:' + count, site.is.ad() ? 'ad' : '', 'currentTime:' + video.currentTime); if(site.is.ad()) return video.originalPlay(); if(--count === 0) video.play = video.originalPlay; let spinner = site.get.spinner(); if(spinner) spinner.style.display = 'none'; }; } /* I don't know why but on t < 5, it'll surely be paused but player UI is remained playing. So... */ if(startTime && startTime < 5) video.addEventListener('seeked', function(e){ //log(e.type, 'currentTime:' + video.currentTime); if(flags.shownAd) return;/*will imitate by canplay event listener*/ core.imitateUnstarted(video); }, {once: true}); }, imitateUnstarted: function(video){ //log(); let player = site.get.moviePlayer(); core.setThumbnail(video); player.classList.add('imitated-unstarted-mode'); video.addEventListener('play', function(e){ //log(e.type, 'now imitated-unstarted-mode', player.classList.contains('imitated-unstarted-mode')); video.addEventListener('play', function(e){ //log(e.type, 'removing imitated-unstarted-mode', player.classList.contains('imitated-unstarted-mode')); player.classList.remove('imitated-unstarted-mode'); }, {once: true}); }, {once: true}); video.play(); video.pause(); }, setThumbnail: function(video){ //log(view); /* normally it will automatically be set, but it won't after ads */ if(view === undefined) return; core.getTarget(view.get.thumbnailOverlayImage).then(thumbnail => { thumbnail.style.backgroundImage = `url(${view.get.thumbnailURL()})`; }); }, setPlaylistAutoplay: function(){ //log(); if(flags.autoplayOnPlaylist !== undefined) return; flags.autoplayOnPlaylist = Storage.read('autoplayOnPlaylist'); core.getTarget(view.get.playlistAutoplayInsertBefore).then(insertBefore => { let autoplaySwitch = createElement(html.autoplaySwitch(flags.autoplayLabel || Storage.read('autoplayLabel'))), button = autoplaySwitch.querySelector('paper-toggle-button'); if(flags.autoplayOnPlaylist) button.checked = true; /* YouTube listens tap event for toggling playlist collapse */ autoplaySwitch.addEventListener('tap', function(e){ //log(e, button, button.checked ? 'checked' : ''); if(button.checked) flags.autoplayOnPlaylist = true; else flags.autoplayOnPlaylist = false; Storage.save('autoplayOnPlaylist', flags.autoplayOnPlaylist); e.stopPropagation(); }); insertBefore.parentNode.insertBefore(autoplaySwitch, insertBefore); }); }, getAutoplayLabel: function(){ //log(); /* get the label everytime for catching language change, it's not such a heavy task' */ core.getTarget(view.get.autoplayLabel).then(autoplayLabel => { if(autoplayLabel.textContent === flags.autoplayLabel) return; flags.autoplayLabel = autoplayLabel.textContent; Storage.save('autoplayLabel', flags.autoplayLabel); }); }, getView: function(views){ Object.keys(views).forEach(key => views[key].key = key); let key = Object.keys(views).find(key => views[key].url.test(location.href)); if(key === undefined) return log('Doesn\'t match any views:', location.href); else return views[key]; }, getTarget: function(selector, retry = 10, interval = 1*SECOND){ const key = selector.name; const get = function(resolve, reject){ let selected = selector(); if(selected && selected.length > 0) selected.forEach((s) => s.dataset.selector = key);/* elements */ else if(selected instanceof HTMLElement) selected.dataset.selector = key;/* element */ else if(--retry) return log(`Not found: ${key}, retrying... (${retry})`), setTimeout(get, interval, resolve, reject); else return reject(new Error(`Not found: ${selector.name}, I give up.`)); elements[key] = selected; resolve(selected); }; return new Promise(function(resolve, reject){ get(resolve, reject); }); }, getTargets: function(selectors, retry = 10, interval = 1*SECOND){ return Promise.all(Object.values(selectors).map(selector => core.getTarget(selector, retry, interval))); }, addStyle: function(name = 'style'){ if(html[name] === undefined) return; let style = createElement(html[name]()); document.head.appendChild(style); if(elements[name] && elements[name].isConnected) document.head.removeChild(elements[name]); elements[name] = style; }, }; const html = { /* YouTube itself will append the button structure to paper-toggle-button, it's so fragile!! */ autoplaySwitch: (label = 'AUTOPLAY') => `