Improve and upgrade the script below with more advanced and capable logic which aims to make the script able to detect media more effectively 
!function(){"use strict";let e=new Map,t=["video/mp4","video/webm","video/ogg","application/x-mpegURL"],r=[function t(){document.querySelectorAll("video").forEach(t=>{let r=t.src||t.querySelector("source")&&t.querySelector("source").src;r&&!e.has(r)&&(e.set(r,{}),o(r))})},function t(){document.querySelectorAll('meta[property="og:video"]').forEach(t=>{let r=t.content;r&&!e.has(r)&&(e.set(r,{}),o(r))})},function r(){new MutationObserver(()=>{performance.getEntriesByType("resource").forEach(r=>{t.includes(r.initiatorType)&&!e.has(r.name)&&(e.set(r.name,{}),o(r.name))})}).observe(document.body,{childList:!0,subtree:!0})},function t(){document.querySelectorAll("script").forEach(t=>{let r=t.textContent;if(r){let n=r.match(/https?://[^\s]+?.(mp4|webm|ogg)/g);n&&n.forEach(t=>{e.has(t)||(e.set(t,{}),o(t))})}})},function t(){document.querySelectorAll('[style*="background"]').forEach(t=>{let r=t.getAttribute("style").match(/url(["']?(https?://[^\s]+?.(mp4|webm|ogg))["']?)/);if(r){let n=r[1];e.has(n)||(e.set(n,{}),o(n))}})},function t(){document.querySelectorAll("[src], [data-src], [data-href]").forEach(t=>{let r=t.getAttribute("src")||t.getAttribute("data-src")||t.getAttribute("data-href");r&&(r.endsWith(".mp4")||r.endsWith(".webm")||r.endsWith(".ogg"))&&!e.has(r)&&(e.set(r,{}),o(r))})},function t(){document.querySelectorAll("[data-video-url]").forEach(t=>{let r=t.getAttribute("data-video-url");r&&!e.has(r)&&(e.set(r,{}),o(r))})},function t(){document.querySelectorAll("video").forEach(t=>{t.currentSrc&&!e.has(t.currentSrc)&&(e.set(t.currentSrc,{}),o(t.currentSrc))})},function t(){document.querySelectorAll('script[type="application/ld+json"]').forEach(t=>{let r=JSON.parse(t.textContent);r&&r.contentUrl&&!e.has(r.contentUrl)&&(e.set(r.contentUrl,{}),o(r.contentUrl))})},function e(){},function r(){let n=XMLHttpRequest.prototype.open,i=XMLHttpRequest.prototype.send;XMLHttpRequest.prototype.open=function(e,t){this._url=t,n.apply(this,arguments)},XMLHttpRequest.prototype.send=function(){this.addEventListener("load",function(){if(t.some(e=>this.getResponseHeader("Content-Type")?.includes(e))){let r={};this.getAllResponseHeaders().trim().split(/[\r\n]+/).forEach(e=>{let t=e.split(": "),o=t.shift(),n=t.join(": ");r[o]=n}),e.has(this._url)||(e.set(this._url,r),o(this._url,r))}}),i.apply(this,arguments)}}];function o(e,t={}){let r=document.createElement("button");r.innerText="Download Video",r.style.position="fixed",r.style.top="10px",r.style.right="10px",r.style.zIndex=1e4,r.style.backgroundColor="#4CAF50",r.style.color="white",r.style.border="none",r.style.padding="10px",r.style.cursor="pointer",r.addEventListener("click",()=>{n(e,t,r)}),document.body.appendChild(r)}async function n(e,t,r){r.innerText="Starting download...";try{let o=await fetch(e,{headers:new Headers(t)});if(!o.ok)throw Error(HTTP error! Status: ${o.status});let n=o.headers.get("Content-Length"),i=n?parseInt(n,10):null,l=0,s=o.body.getReader(),c=await new Response(new ReadableStream({start(e){!function t(){s.read().then(({done:o,value:n})=>{if(o){e.close(),r.innerText="Download complete";return}if(l+=n.byteLength,i){let s=(l/i*100).toFixed(2);r.innerText=Downloading... ${s}%}else r.innerText=Downloading... ${l} bytes;e.enqueue(n),t()}).catch(e=>{r.innerText=Error: ${e.message},console.error("Stream reading error:",e)})}()}})).blob(),a=URL.createObjectURL(c),d=document.createElement("a");d.href=a,d.download="video.mp4",document.body.appendChild(d),d.click(),document.body.removeChild(d),URL.revokeObjectURL(a)}catch(u){r.innerText=Error: ${u.message},console.error("Download error:",u)}}function i(){r.forEach(e=>e())}new MutationObserver(()=>{i()}).observe(document.body,{childList:!0,subtree:!0}),i()}();