Video source link

Redirects to the video source when clicking on the button

  1. // ==UserScript==
  2. // @name Video source link
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @license MIT
  6. // @description Redirects to the video source when clicking on the button
  7. // @author You
  8. // @match http*://*/*
  9. // @icon https://www.google.com/s2/favicons?domain=earth.google.com/web/
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (async function() {
  14. 'use strict';
  15.  
  16. function runWhenReady(readySelector, callback) {
  17. var numAttempts = 0;
  18. var tryNow = function() {
  19. var elem = document.querySelector(readySelector);
  20. if (elem) {
  21. callback(elem);
  22. } else {
  23. numAttempts++;
  24. if (numAttempts >= 15) {
  25. console.warn('Giving up after 15 attempts. Could not find: ' + readySelector);
  26. } else {
  27. setTimeout(tryNow, 250 * Math.pow(1.1, numAttempts));
  28. }
  29. }
  30. };
  31. tryNow();
  32. }
  33.  
  34. var getSrc = () => {
  35. let video = document.querySelector("video");
  36. let src = video.src ? video.src : video.childNodes[0]?.src
  37. console.log(src)
  38. if (src && document.location.href != src ) {
  39. return src
  40. }
  41. else {
  42. console.log("You need to click on the video so that the src loads")
  43. }
  44. return "NonValidLink"
  45. }
  46.  
  47. runWhenReady ('video', async function (ele) {
  48. let btn = document.createElement("button");
  49. let anchor = document.createElement("a");
  50. btn.innerHTML = "Redirect";
  51. //btn.onclick = redirectToSrc;
  52. btn.style.position = "fixed"
  53. btn.style.zIndex = 2147483
  54. btn.style.inset = "70px auto auto 0px"
  55. btn.style.setProperty("background-color", "cyan", "important")
  56. btn.style.border = "none"
  57. btn.style.setProperty("border", "2px solid black", "important")
  58. btn.style.borderRadius = "10px"
  59. btn.style.setProperty("color", "black", "important")
  60. btn.style.setProperty("width", "clamp(50px, 80px, 120px)", "important")
  61. btn.style.height = "30px"
  62. await new Promise((r) => setTimeout(r, 1500))
  63. let src = getSrc()
  64. anchor.href = src
  65. anchor.appendChild(btn)
  66. // Only show button if it points to a meaningful link
  67. if (src.startsWith("http")) {
  68. document.body.appendChild(anchor)
  69. }
  70. })
  71. })();