Netflix subtitle cleanup

Remove all "[inhales]", "[loud noise]" sounds and music lyrics from the subtitles

当前为 2021-11-14 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Netflix subtitle cleanup
// @namespace   Violentmonkey Scripts
// @match       https://www.netflix.com/*
// @grant       none
// @version     1.1
// @author      Einar Lielmanis, [email protected]
// @license     MIT
// @description Remove all "[inhales]", "[loud noise]" sounds and music lyrics from the subtitles
// ==/UserScript==

let observed_node = undefined

const on_mutated = (changes) => {
  const ts = observed_node.querySelectorAll('.player-timedtext-text-container span')
  for (let i = 0; i < ts.length; i++) {
    
    const t = ts[i].innerHTML
    let nt = t
    
    if (t.includes('♪')) {
      nt = ""; // ignore song lyrics
    } else if (t.includes('[') && t.includes(']')) {
      nt = t.replace(/\[[^\]]+\]/g, '') // "[" .. (not "]")+ .. "]"
    }
    
    if (nt !== t) {
      ts[i].innerHTML = nt
      // console.log({ original: t, filtered: nt })
    }
  }
}

const observer = new MutationObserver(on_mutated)

const reobserve = () => {
  
  const elems = document.getElementsByClassName('player-timedtext')
  if (elems[0] !== undefined) {
    if (observed_node !== elems[0]) {
      observed_node = elems[0]
      console.log({ observed_node })
      observer.observe(observed_node, { childList: true, subtree: true})
    }
  }
  window.setTimeout(reobserve, 1000)
}


console.log('Netflix subtitle filter userscript starting up')
reobserve()