Netflix subtitle cleanup

Remove all "[inhales]", "[loud noise]" from the subtitles

  1. // ==UserScript==
  2. // @name Netflix subtitle cleanup
  3. // @namespace Violentmonkey Scripts
  4. // @match https://www.netflix.com/*
  5. // @grant none
  6. // @version 1.4
  7. // @author Einar Lielmanis, einars@spicausis.lv
  8. // @license MIT
  9. // @description Remove all "[inhales]", "[loud noise]" from the subtitles
  10. // ==/UserScript==
  11.  
  12. let observed_node = undefined
  13.  
  14. let kill_song_lyrics = false
  15.  
  16. const cleanup = (t) => {
  17. if (kill_song_lyrics && t.includes('♪')) {
  18. return '' // ignore song lyrics
  19. } else if (t.includes('[') && t.includes(']')) {
  20. return t.replace(/(- *)?\[[^\]]+\]/g, '') // (maybe "- ") "[" .. (not "]")+ .. "]"
  21. } else if (t.includes('(') && t.includes(')')) {
  22. return t.replace(/(- *)?\([^\)]+\)/g, '') // (maybe "- ") "(" .. (not ")")+ .. ")"
  23. }
  24.  
  25. return t
  26. }
  27.  
  28. const on_mutated = (changes) => {
  29. const ts = observed_node.querySelectorAll('.player-timedtext-text-container span')
  30. for (let i = 0; i < ts.length; i++) {
  31. const t = ts[i].innerHTML
  32. const nt = cleanup(t)
  33. if (nt !== t) {
  34. ts[i].innerHTML = nt
  35. // console.log({ original: t, filtered: nt })
  36. }
  37. }
  38. }
  39.  
  40. const observer = new MutationObserver(on_mutated)
  41.  
  42. const reobserve = () => {
  43. const elems = document.getElementsByClassName('player-timedtext')
  44. if (elems[0] !== undefined) {
  45. if (observed_node !== elems[0]) {
  46. observed_node = elems[0]
  47. console.log({ observed_node })
  48. observer.observe(observed_node, { childList: true, subtree: true})
  49. }
  50. }
  51. window.setTimeout(reobserve, 1000)
  52. }
  53.  
  54.  
  55. const run_tests = () => {
  56. // the tests are lightning fast, so just do run them quickly on every script startup
  57. const test_cleanup = (source, expected) => {
  58. console.assert(cleanup(source) === expected, { test_result: false, source, expected, actual: cleanup(source) })
  59. }
  60. test_cleanup('normal text', 'normal text')
  61. test_cleanup('[coughs]', '')
  62. test_cleanup('[coughs] yeah', ' yeah')
  63. test_cleanup('-[coughs]', '')
  64. test_cleanup('- [coughs]', '')
  65. test_cleanup('- (inhales)', '')
  66. test_cleanup('some ♪ singing', '')
  67. console.log('tests ok')
  68. }
  69.  
  70.  
  71. console.log('Netflix subtitle filter userscript starting up')
  72. run_tests()
  73. reobserve()