Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
EmbeddedVideoCatcher - Adds a link to the video file. Supports Firefox-based browser up to version 36. Newer versions should use a UserAgent (UA) spoofer add-on. YouTube loads a lighter version of the YouTube page for older browsers. This script will require the GreaseMonkey add-on to be executed by the Firefox browser. If the video link is inac…
// ==UserScript==
// @name EmbeddedVideoCatcher
// @namespace com.vsubhash.js.embedded-video-catcher
// @description Adds a link to the video file. Supports Firefox-based browser up to version 36. Newer versions should use a UserAgent (UA) spoofer add-on. YouTube loads a lighter version of the YouTube page for older browsers. This script will require the GreaseMonkey add-on to be executed by the Firefox browser. If the video link is inaccessible by a transparent image or other HTML element, the URL can be obtained from the browser's web console output (Tools -> Web Developers -> Console). The script also automatically pauses the video several times.
// @include http://*
// @include https://*
// @exclude https://www.youtube.com/watch*
// @exclude https://www.dailymotion.com/video*
// @version 2019.06
// @grant none
// ==/UserScript==
var sAdStyle = " { visibility: none!important; display: none!important; }";
document.addEventListener("readystatechange", loadHandler, false);
try {
window.setTimeout(pauseVideos, 2*1000);
window.setTimeout(pauseVideos, 4*1000);
window.setTimeout(pauseVideos, 6*1000);
} catch (e) { console.error("EVC Error: " + e); }
function loadHandler() {
try {
addVideoList();
} catch (e) {
console.error("EVC Error: " + e);
}
}
function updateYouTubeVideoLink() {
console.log("EVC: Loaded video event for YouTube video");
if (document.getElementById("mvyJsList") &&
document.getElementById("player") &&
(document.getElementById("player").getElementsByTagName("video").length > 0)) {
document.getElementById("player").getElementsByTagName("video")[0].pause();
document.getElementById("mvyJsList").innerHTML = "<li style=\"margin-left: 0.2em; padding-left: 0.2em; \"><a id=\"VidLinkUrl\" title=\"" + document.title + "\" download=\"" + document.title.replace(/\s+/ig, "-").replace(/-{2,}/ig, "-") + ".mp4\" style=\"color: navy; font-weight: bold; \" target=\"_blank\" href=\"" + document.getElementById("player").getElementsByTagName("video")[0].src + "\">" + document.title + "</a></li>";
console.log("Video : " + document.title + "\n" + document.getElementById("player").getElementsByTagName("video")[0].src);
} else {
//console.log("EVC: Loaded video event for YouTube video noooh");
}
}
function updateNumberedVideoLink(aoEvent) {
console.log("EVC: Loaded video event for " + aoEvent.target.src);
var oLink = document.getElementById("VidLinkUrl" + aoEvent.target.getAttribute("mvy_id"));
if (oLink) {
oLink.setAttribute("href", aoEvent.target.getAttribute("src"));
oLink.innerHTML = aoEvent.target.getAttribute("src").substr(aoEvent.target.getAttribute("src").lastIndexOf("/")+1);
console.log("EVC: URL : " + document.title + "\n" + aoEvent.target.src);
console.log("EVC: File: " + aoEvent.target.getAttribute("src").substr(aoEvent.target.getAttribute("src").lastIndexOf("/")+1));
}
}
function addVideoList() {
if (document.readyState == "complete") {
console.log("EVC: Executing EmbeddedVideoCatcher");
var i, n, oDlButtonEl, oDlDiv, oVideosList;
if (location.href.indexOf("youtube.com/embed") > -1) {
// Embedded Youtube video page loads twice. On the second time, it will have a video element.
// However, the video element will not have a 'src' attribute. The attribute will be assigned
// a video URL after the play button is clicked. A handler for the "loadeddata" event is added
// obtain this video URL.
console.log("EVC: Found YouTube embedded video player");
if (document.getElementById("mvyJsDiv") == null) {
oDlDiv = document.createElement("div");
oDlDiv.setAttribute("id", "mvyJsDiv");
oDlDiv.setAttribute("style", "position: absolute; top: 0; display: block; background-color: orange!important; border: 2px dashed firebrick; font-size: 0.34cm!important; font-family: sans-serif!important; line-height: 0.4cm!important; margin: 10px 10px 10px 50%; padding: 1em; width: 40%; z-index: 203; ");
}
if (document.getElementById("mvyJsList") == null) {
oDlDiv.innerHTML = "Download video from:";
oDlList = document.createElement("ul");
oDlList.setAttribute("style", "display: block; list-style: disc outside none; margin-left: 0.2em; padding-left: 0.2em; ");
oDlList.setAttribute("id", "mvyJsList");
oDlDiv.appendChild(oDlList);
}
var oVideoDiv = document.getElementById("player");
if (oVideoDiv == null) {
console.log("EVC: Player DIV not found");
} else if (oVideoDiv.getElementsByTagName("video").length < 1) {
console.log("EVC: Video tag not found in player DIV");
} else {
oVideoDiv.appendChild(oDlDiv);
var oVideo = oVideoDiv.getElementsByTagName("video")[0];
oVideo.addEventListener("loadeddata", updateYouTubeVideoLink, false);
if (oVideo.src) {
updateYouTubeVideoLink();
} else {
document.querySelector("button.ytp-large-play-button").click();
window.setTimeout(updateYouTubeVideoLink, 3*1000);
document.getElementById("mvyJsList").innerHTML = "Video not loaded yet";
}
console.log("EVC: Added Youtube video to list");
}
} else {
console.log("EVC: Not youtube");
if (document.getElementsByTagName("video").length > 0) {
console.log("EVC: Number of videos = " + document.getElementsByTagName("video").length);
for (var i = 0; i < document.getElementsByTagName("video").length; i++) {
console.log("EVC: Parsing video " + i);
oDlDiv = document.createElement("div");
oDlDiv.setAttribute("id", ("mvyJsDiv" + i));
oDlDiv.setAttribute("style", "position: absolute; top: 0; display: block; background-color: orange!important; border: 2px dashed firebrick; font-size: 0.34cm!important; font-family: sans-serif!important; line-height: 0.4cm!important; margin: 10px 10px 10px 50%; padding: 0.5em; width: 40%; z-index: " + (203 + (i*10)) + "; ");
oDlDiv.style.top = (i*100) + "px";
oDlDiv.innerHTML = "Download video from:";
oDlList = document.createElement("ul");
oDlList.setAttribute("style", "display: block; list-style: disc outside none; margin-left: 0.2em!important; padding-left: 0.2em!important;");
oDlList.setAttribute("id", ("mvyJsList" + i));
oDlDiv.appendChild(oDlList);
var oVideo = document.getElementsByTagName("video")[i];
oVideo.pause();
oVideo.parentElement.setAttribute("style", "background-color: brown; overflow: scroll!important; ");
oVideo.parentElement.insertBefore(oDlDiv, oVideo);
oVideo.setAttribute("mvy_id", i);
oVideo.addEventListener("loadeddata", updateNumberedVideoLink, false);
//oVideo.onloadeddata = updateNumberedVideoLink;
if (oVideo.getElementsByTagName("source").length > 0) {
oDlList.innerHTML = "<li style=\"margin-left: 0.2em; padding-left: 0.2em; \"><a id=\"VidLinkUrl" + i + "\" title=\"" + document.title + "\" style=\"color: navy; font-weight: bold; \" target=\"_blank\" href=\"" + oVideo.src + "\">Not Available</a></li>";
console.log("EVC: Video " + (i) + " has source tag(s)");
for (var j = 0; j < oVideo.getElementsByTagName("source").length; j++) {
var oSource = oVideo.getElementsByTagName("source")[j];
oDlList.innerHTML = oDlList.innerHTML + "<li style=\"margin-left: 0.2em; padding-left: 0.2em; \"><a id=\"VidLinkUrl" + i + "-" + j + "\" title=\"" + (document.title?document.title:"") + " - Source " + (j+1) + " (" + oSource.type + ")\" download=\"" + document.title.replace(/\s+/ig, "-").replace(/-{2,}/ig, "-") + ".mp4\" style=\"color: navy; font-weight: bold; \" target=\"_blank\" href=\"" + oSource.src + "\">" + (document.title?document.title:"") + " - Source " + (j+1) + (oSource.getAttribute("res")?(" [" + oSource.getAttribute("res") + "]"):"") + (oSource.getAttribute("label")?(" [" + oSource.getAttribute("label") + "]"):"") + "</a></li>";
console.log("EVC: Found source " + (j+1) + " " + oSource.getAttribute("res") + " ~ " + oSource.src);
}
console.log("EVC: Added " + oVideo.getElementsByTagName("source").length + " videos to list");
} else {
console.log("EVC: Video " + (i+1) + " has no source tag");
if (oVideo.src) {
console.log("EVC: Video " + (i+1) + " is at ~ " + oVideo.src);
if (document.title) {
oDlList.innerHTML = "<li style=\"margin-left: 0.2em; padding-left: 0.2em; \"><a id=\"VidLinkUrl" + i + "\" title=\"" + document.title + "\" download=\"" + document.title.replace(/\s+/ig, "-").replace(/-{2,}/ig, "-") + ".mp4\" style=\"color: navy; font-weight: bold; \" target=\"_blank\" href=\"" + oVideo.src + "\">" + document.title + "</a></li>";
} else {
oDlList.innerHTML = "<li style=\"margin-left: 0.2em; padding-left: 0.2em; \"><a id=\"VidLinkUrl" + i + "\" title=\"Video\" style=\"color: navy; font-weight: bold; \" target=\"_blank\" href=\"" + oVideo.src + "\">Video Link</a></li>";
}
} else {
console.log("EVC: Video " + (i+1) + " has no URL");
oDlList.innerHTML = "<li style=\"margin-left: 0.2em; padding-left: 0.2em; \"><a id=\"VidLinkUrl" + i + "\" title=\"Video\" style=\"color: navy; font-weight: bold; \" href=\"javascript:return(false);\">Video Link Not Available</a></li>";
}
}
}
} else {
console.log("EVC: Found no videos");
}
}
} else {
// page not ready
}
}
function pauseVideos() {
console.log("EVC: Pausing videos");
try {
var oVideoEls = document.getElementsByTagName("video");
console.log("EVC: Number of videos " + oVideoEls.length);
for (var i = 0; i < oVideoEls.length; i++) {
oVideoEls[i].pause();
oVideoEls[i].muted = true;
if (oVideoEls[i].src.indexOf("pltype=adhost") > -1) {
console.log("EVC: Video ad found... closing tab");
oVideoEls[i].src = "";
}
console.log("EVC: Pausing video " + (i+1));
if (!oVideoEls[i].paused) {
oVideoEls[i].pause();
}
//oVideoEls[i].volume = 0.6; // custom controls do not update
oVideoEls[i].muted = true;
oVideoEls[i].removeAttribute("autoplay");
oVideoEls[i].removeAttribute("loop");
oVideoEls[i].removeAttribute("controls");
oVideoEls[i].setAttribute("preload", "none");
oVideoEls[i].pause();
}
var oButtons = document.getElementsByTagName("button");
for (var i = 0; i < oButtons.length; i++) {
if (oButtons[i].className) {
if (oButtons[i].className.indexOf("ytp-mute-button") > -1) {
console.log("EVC: Mute button " + oButtons[i].className);
oButtons[i].click();
oButtons[i].click();
}
}
}
} catch (e) {
console.error("EVC: Error - " + e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.