Created
August 31, 2022 23:11
-
-
Save magasine/80ae848bfd265435b44c00ae828e271e to your computer and use it in GitHub Desktop.
! Container Copy - Bookmarklet
This file contains 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
javascript: void (function () { | |
function a(a) { | |
if (navigator.clipboard && window.isSecureContext) | |
return navigator.clipboard.writeText(a); | |
else { | |
let b = document.createElement("textarea"); | |
return ( | |
(b.value = a), | |
(b.style.position = "fixed"), | |
(b.style.left = "-999999px"), | |
(b.style.top = "-999999px"), | |
document.body.appendChild(b), | |
b.focus(), | |
b.select(), | |
new Promise((a, c) => { | |
document.execCommand("copy") ? a() : c(), b.remove(); | |
}) | |
); | |
} | |
} | |
function b(a) { | |
c.style.pointerEvents = "none"; | |
var b = document.elementFromPoint(a.clientX, a.clientY); | |
return (c.style.pointerEvents = "auto"), b; | |
} | |
var c = document.createElement("div"); | |
Object.assign(c.style, { | |
position: "fixed", | |
top: 0, | |
left: 0, | |
width: "100vw", | |
height: "100vh", | |
zIndex: 99999999, | |
background: "transparent", | |
cursor: "crosshair", | |
}), | |
document.body.append(c); | |
document.addEventListener("mousemove", function (a) { | |
var d = b(a); | |
if (d) { | |
var e = d.getBoundingClientRect(); | |
Object.assign(c.style, { | |
background: "rgba(0, 128, 255, 0.25)", | |
outline: "1px solid rgba(0, 128, 255, 0.5)", | |
top: "" + e.top + "px", | |
left: "" + e.left + "px", | |
width: "" + e.width + "px", | |
height: "" + e.height + "px", | |
}); | |
} | |
}), | |
c.addEventListener("click", function (d) { | |
var e = b(d), | |
f = e.textContent || e.value; | |
(f = f | |
.replace(/\n[ \n]+\n/g, "\n") | |
.replace(/\n\n+/g, "\n\n") | |
.replace(/^\n+|\n+$/g, "")), | |
f.match("\n") || (f = f.replace(/^ +| +$/, "")), | |
a(f), | |
document.body.removeChild(c); | |
}); | |
})(); | |
// Container Copy | |
// The <div> tag defines a division or a section in an HTML document. The <div> tag is used as a container for HTML elements | |
// Usage: Fire up the bookmarklet and watch the mouse pointer change to a cross. Point the cross at the element you want to capture to the clipboard |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment