Disable coursera video play when clicking transscript

Avoid paused coursera video being played when clicking transscript.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

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