Spotifyのアルバムリンクを貼ったらクリップボードにジャケットの画像をコピーするUserScript
ユーザーページ(

なら
yuta25に、
code:script.js
と書いて、以下を記述すると使えます
example.jsimport '/api/code/cd/Spotifyのアルバムリンクを貼ったらクリップボードにジャケットの画像をコピーするUserScript/script.js';
script.js(() => {
document.addEventListener("paste", async (e) => {
const text = e.clipboardData.getData("text");
const url = text;
if (
url.match(/^https:\/\/open\.spotify\.com\/(intl-ja\/)?(album|track)\/.+/) ||
url.match(/^https:\/\/open.spotify.com\/playlist\/.+/)
) {
const img = new Image;
const c = document.createElement("canvas");
const ctx = c.getContext("2d");
// refs.
// - https://stackoverflow.com/questions/42471755/convert-image-into-blob-using-javascript
// - https://stackoverflow.com/questions/33175909/copy-image-to-clipboard
img.onload = function() {
c.width = this.naturalWidth;
c.height = this.naturalHeight;
ctx.drawImage(this, 0, 0);
c.toBlob(async function(blob) {
const data = [new ClipboardItem({ [blob.type]: blob })];
await navigator.clipboard.write(data);
confirm('album jacket coppied!');
}, "image/png", 0.75);
};
img.crossOrigin = "anonymous";
img.src = `https://og-image-dlwr.vercel.app/image.jpg?url=${encodeURIComponent(url)}`;
}
});
})();