获取 Openload VTT 字幕 link

Get an Openload VTT subtitle link

我想使用 Greasemonkey 为 Openload VTT 字幕添加字幕下载按钮。但是,我不知道如何访问 标签。

例如,这个 French video clip 有英文字幕。 当我在 Firefox 中查看源代码时,我发现:

<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>

为什么我的概念验证 Greasemonkey 代码不起作用?

// ==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");
}

(当我 运行 脚本时,我收到消息“ NOT found!”。)

link you gave从来没有<track>节点,至少对我来说是这样(未登录,也不是视频的创建者) .

不过,这可能是一个标准的 AJAX 问题。也就是说,如果节点是通过 javascript (AJAX) 添加的,Tampermonkey 脚本将在加载目标节点之前完成。

为此使用标准的 ajax 感知技术。一种方式:

// ==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]);
}

请注意,以上代码适用于 Tampermonkey、Violentmonkey 和 Greasemonkey 的早期版本。它应该 在 Greasemonkey 4+ 中工作,但是那个引擎很坏,所以不能保证。

通过安装脚本并访问 this MDN video demo page.

,您可以看到代码确实找到了轨道(即使在阴影中 DOM)

这是一个 simple/basic 脚本。 我不知道您使用的是哪个版本的 GM。这是为GM4写的 如果您使用的是 GM 3,则更改:

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

它会在新选项卡中打开字幕,以便您保存。 您可以在嵌入和普通文件页面上 运行 它。 例如
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
})();