Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an Openload VTT subtitle link

I'd like to use Greasemonkey to add a subtitle download button for Openload VTT subtitles. However, I can't figure out how to access the <track> tag.

Take, for example, this French video clip with English subtitles. When I looked at the source code in Firefox, I found this:

<video id="olvideo" width="100%" height="100%" crossorigin="anonymous" controls>
    <track kind="captions" src="https://thumb.oloadcdn.net/subtitle/rjC09fkPLYs/vt8zTaIaVqQ.vtt" srclang="en" label="English" default />
</video>

Why doesn't my proof-of-concept Greasemonkey code work?

// ==UserScript==
// @name        Openload
// @include     *openload.co*
// @run-at      document-idle
// ==/UserScript==

var video = document.querySelector("video");

if (video) {
    var track = video.querySelector("track");
    if (track) {
        alert ("<track> FOUND.");
    } else {
        alert ("<track> NOT found!");
    }

} else { 
    alert ("<video> tag not found");
}

(When I ran the script I got the message "<track> NOT found!".)

like image 483
Nemo XXX Avatar asked Mar 06 '23 03:03

Nemo XXX


1 Answers

The link you gave doesn't ever have a <track> node, at least for me (un logged-in, and not the video's creator).

Nevertheless, this is may be a standard AJAX problem. That is, if the node is added via javascript (AJAX), the Tampermonkey script will have finished before the target node is loaded.

Use standard ajax-aware techniques for that. One way:

// ==UserScript==
// @name     Openload.co, Report on track nodes
// @match    *://openload.co/embed/*
// @match    *://interactive-examples.mdn.mozilla.net/pages/tabbed/track.html
// @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// @grant    GM.getValue
// ==/UserScript==
//- The @grant directives are needed to restore the proper sandbox.

waitForKeyElements ("track", reportTrackNode);

//-- For Mozilla page, which uses shadow DOM:
waitForKeyElements ("shadow-output", reportTrackNodeWithinShadowDOM);

function reportTrackNode (jNode) {
    console.log ("Found <track>:", jNode[0]);
}

function reportTrackNodeWithinShadowDOM (jNode) {
    var sr      = jNode[0].shadowRoot;
    var trck    = $(sr.childNodes).find ("track");
    if (trck.length === 0)  return true;  //  Keep waiting.

    console.log ("Found <track>:", trck[0]);
}

Note that the above code works in Tampermonkey, Violentmonkey, and earlier versions of Greasemonkey. It should work in Greasemonkey 4+, but that engine is very broken, so no guarantees.

You can see that the code does find the track when it exists (even in a shadow DOM) by installing the script and visiting this MDN video demo page.

like image 183
Brock Adams Avatar answered Mar 08 '23 22:03

Brock Adams