Created
April 25, 2025 09:16
-
-
Save FreePhoenix888/c620c7a1e57ef6db1949588ef1c5d1a0 to your computer and use it in GitHub Desktop.
Download Media Under Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Download Media Under Cursor | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Downloads image or video under your mouse cursor when you press 'D' | |
// @author You | |
// @match *://*/* | |
// @grant none | |
// ==/UserScript== | |
(function () { | |
'use strict'; | |
const MediaDownloader = { | |
playBeep(frequency = 800, duration = 100) { | |
const ctx = new (window.AudioContext || window.webkitAudioContext)(); | |
const oscillator = ctx.createOscillator(); | |
const gain = ctx.createGain(); | |
oscillator.connect(gain); | |
gain.connect(ctx.destination); | |
oscillator.type = "sine"; | |
oscillator.frequency.value = frequency; | |
oscillator.start(); | |
gain.gain.setValueAtTime(0.1, ctx.currentTime); | |
oscillator.stop(ctx.currentTime + duration / 1000); | |
}, | |
downloadMediaUnderCursor(event) { | |
const element = document.elementFromPoint(event.clientX, event.clientY); | |
let mediaSrc; | |
let isVideo = false; | |
if (element instanceof HTMLImageElement) { | |
mediaSrc = element.src; | |
} else if (element instanceof HTMLVideoElement) { | |
const source = element.querySelector("source"); | |
mediaSrc = source?.src || element.src; | |
isVideo = true; | |
} | |
if (!mediaSrc) { | |
console.warn("No media found under cursor to download."); | |
return; | |
} | |
const filename = new URL(mediaSrc).pathname.split('/').pop().split('?')[0] || (isVideo ? "video.mp4" : "image.png"); | |
this.playBeep(); | |
fetch(mediaSrc) | |
.then(response => response.blob()) | |
.then(blob => { | |
const url = URL.createObjectURL(blob); | |
const anchor = document.createElement("a"); | |
anchor.href = url; | |
anchor.download = filename; | |
document.body.appendChild(anchor); | |
anchor.click(); | |
URL.revokeObjectURL(url); | |
document.body.removeChild(anchor); | |
this.playBeep(); | |
}) | |
.catch(error => { | |
this.playBeep(500); | |
console.error("Error downloading media:", error); | |
if (error.message.includes("Failed to fetch")) { | |
console.log("Failed to fetch. Ensure it's not a CORS issue. Use a browser extension if needed."); | |
} | |
}); | |
} | |
}; | |
document.addEventListener("keydown", (e) => { | |
if (e.key.toLowerCase() === "d") { | |
// Use the last known mouse position | |
if (window._lastMouseEvent) { | |
MediaDownloader.downloadMediaUnderCursor.call(MediaDownloader, window._lastMouseEvent); | |
} | |
} | |
}); | |
document.addEventListener("mousemove", (e) => { | |
window._lastMouseEvent = e; | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment