Search code examples
javascriptgreasemonkeytampermonkeyvtt

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!".)


Solution

  • Here is a simple/basic script. I didn't know which version of GM you are using. This is written for GM4 If you are using GM 3, then change:

    GM.xmlHttpRequest -> GM_xmlhttpRequest
    GM.openInTab -> GM_openInTab
    

    It opens the subtitles in a new tab so you can save it. You can run it on both embed and normal file pages. e.g.
    https://openload.co/embed/rjC09fkPLYs
    https://openload.co/f/rjC09fkPLYs

    // ==UserScript==
    // @name          Openload Subtitle Download
    // @namespace     erosman
    // @description   Openload Subtitle Download
    // @include       https://openload.co/f/*
    // @include       https://openload.co/embed/*
    // @grant         GM.xmlHttpRequest
    // @grant         GM_xmlhttpRequest
    // @grant         GM.openInTab
    // @grant         GM_openInTab
    // @author        erosman
    // @version       1.0
    // ==/UserScript==
    
    /* --------- Note ---------
      This script download Openload Subtitles.
      It runs on both embed and normal file pages.
      --------- History ---------
    
    
      1.0   Initial release
    
    */
    
    (() => { // anonymous function wrapper, for error checking & limiting scope, async FF52+
    'use strict';
    
    if (frameElement || !location || !document.body) { return; } // end execution if in a frame/object/embedding points
    
    
    // --- get the document
    GM.xmlHttpRequest({
      method: 'GET',
      url: location.href,
      onload: result => processResult(result.responseText),
      onerror: error => console.log(error)
    });
    
    function processResult(str) {
    
      // convert to DOM
      const doc = new DOMParser().parseFromString(str, 'text/html');
    
      // get tracks with source, convert to array for forEach, 
      // open each subtitle (if there are more than one) in a new tab
      // you can save it from there
      [...doc.querySelectorAll('track[src]')].forEach(item => GM.openInTab(item.src));
    }
    
    // end of anonymous function
    })();