最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how make youtube subtitles selectable? - Stack Overflow

programmeradmin3浏览0评论

when activating youtube subtitles, if you try to select a word in the caption of subtitles witch identified in the DOM by id="caption-window-1" , the first thing noticed is that : it is not selectable, so to make it selectable, i run this script in the js console using the developer tool on chrome browser:

(function() {
var caption= document.getElementById("caption-window-1");
caption.style.cursor="text";
caption.style["-webkit-user-select"]="all";
            })();

its turn perfectly, but the caption still moving when click on it (you can try it by your self on YouTube)

so i can't select easily a word in the caption, it still moving whatever i do.

I want to disable the caption movement on onclick event any idea to fix that using javascript ?

when activating youtube subtitles, if you try to select a word in the caption of subtitles witch identified in the DOM by id="caption-window-1" , the first thing noticed is that : it is not selectable, so to make it selectable, i run this script in the js console using the developer tool on chrome browser:

(function() {
var caption= document.getElementById("caption-window-1");
caption.style.cursor="text";
caption.style["-webkit-user-select"]="all";
            })();

its turn perfectly, but the caption still moving when click on it (you can try it by your self on YouTube)

so i can't select easily a word in the caption, it still moving whatever i do.

I want to disable the caption movement on onclick event any idea to fix that using javascript ?

Share Improve this question asked Oct 20, 2016 at 23:05 JoloJolo 511 silver badge2 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 4

Run this script in the console:

setInterval(make_subtitles_selectable, 250);
function make_subtitles_selectable(){
    elem=document.querySelector("span.ytp-caption-segment:not([selectable='true']");
    if(elem!=null){
        elem.style.userSelect="text";
        elem.style.cursor="text";
        elem.setAttribute("selectable", "true");
    }
    elem=document.querySelector("#caption-window-1:not([selectable='true']");
    if(elem!=null){
        elem.addEventListener("mousedown", function (event) {
            event.stopPropagation();
        }, true);
        elem.setAttribute("selectable", "true");
    }
}

thx to people above I made a little adjustments to be able select multiple words like a text. here script for tampermonkey/etc..

// ==UserScript==
// @name         make youtube caption selectable
// @namespace    none
// @version      0.2
// @description  Use this script if you want be able to select youtube captions/subtitles!
// @author       You
// @match        https://www.youtube./watch?*
// @grant        none
// @require      none
// ==/UserScript==

!function() {
  setInterval(make_subtitles_selectable, 250);
  function make_subtitles_selectable(){
    elem=document.querySelector("div.caption-window");
    if(elem!=null){
        elem.addEventListener("mousedown", function (event) {
            event.stopPropagation();
        }, true);
        elem.setAttribute("draggable", "false");
        elem.style.userSelect="text";
        elem.style.cursor="text";
        elem.setAttribute("selectable", "true");
    }
    elem=document.querySelector("span.ytp-caption-segment:not([selectable='true']");
    if(elem!=null){
        elem.style.userSelect="text";
        elem.style.cursor="text";
        elem.setAttribute("selectable", "true");
    }
    elem=document.querySelector("#caption-window-1:not([selectable='true']");
    if(elem!=null){
        elem.addEventListener("mousedown", function (event) {
            event.stopPropagation();
        }, true);
        elem.setAttribute("selectable", "true");
        elem.setAttribute("draggable", "false");
    }
}
}()

The problem is that the textfield is draggable to allow the user position the field anywhere on the screen so it conflicts with what you're trying to do. You could instead just record and push the text to an array and parse it after the video is done.

The snippet is not very elegant but should get you started:

var wordsAll = [];
var wordsPrev = [];
var previousLine = '';
function checkText() {
  var caption= document.getElementById("caption-window-1");
  requestAnimationFrame(checkText);
  if ( caption === null ) return;
  var wordsNow = caption.textContent.split(' ');
  if ( wordsPrev.length > 0 && wordsPrev.length < wordsNow.length ) {
    for ( var i=0,l= wordsNow.length; i<l; i++ ) {
      wordsAll.push(wordsPrev[i]);
    }
  }
  wordsPrev = wordsNow.slice(l);
}
checkText(); 

Then just do something like console.log(wordsAll.join(' '))

Hope that helps!

You need to install capturing event listeners that call stopPropagation on the event to intercept it before the youtube javascript can process them and supress the default action, i.e. the text selection

.

that is what i'm trying to do :

screenshot

Three years since this question active. Personally I think I have solved this question

https://gist.github./iamwwc/8bf9d1ea3aa38120a9ed07014cd5d73e

The script has done following things

  1. add 'user-select:none' css to subtitle make it selectable
  2. add a event listener on player, stopPropagation when click, touchstart, mouseover, mousedown event occurred

Througn this script, my translate select function now works

If you want subtitle to moveable, and move out of player, you can try the following script

https://gist.github./iamwwc/341b0513f9b22b1067a479225f3d6626

Hope my idea can help you a lot, to make a great tool for translator to work

发布评论

评论列表(0)

  1. 暂无评论