Disable coursera video play when clicking transscript

Avoid paused coursera video being played when clicking transscript.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Disable coursera video play when clicking transscript
// @description Avoid paused coursera video being played when clicking transscript.
// @version 20220506
// @match       https://www.coursera.org/learn/*
// @grant       none
// @author      -
// @run-at document-idle
// @supportURL https://github.com/whtsky/userscripts/issues
// @namespace https://greasyfork.org/users/164794
// ==/UserScript==

const noop = () => {}

const preventVideoPlay = () => {
  const video = document.querySelector('video')
  const oldVideoPlay = video.play
  video.play = noop
  video.oldPlay = oldVideoPlay
}

const resumeVideoPlay = () => {
  const video = document.querySelector('video')
  video.play = video.oldPlay ?? video.play
}

const check = (changes, observer) => {
  const transscriptDiv = document.querySelector('.rc-TranscriptHighlighter')
  if (transscriptDiv) {
    transscriptDiv.addEventListener('mouseenter', preventVideoPlay)
    transscriptDiv.addEventListener('mouseleave', resumeVideoPlay)
  }
}

new MutationObserver(check).observe(document, { childList: true, subtree: true })