Skip to content

Instantly share code, notes, and snippets.

@Kazuki-tam
Last active December 2, 2019 04:55
Show Gist options
  • Save Kazuki-tam/3436ba9023c31915209abe1974d1f582 to your computer and use it in GitHub Desktop.
Save Kazuki-tam/3436ba9023c31915209abe1974d1f582 to your computer and use it in GitHub Desktop.
Bookmarklet Script for Download Image
/*
任意の画像ダウンロード
*/
function download() {
// 画像要素取得
const imgLinks = document.querySelectorAll(
".swiper-container .swiper-slide img"
);
// スライダー数取得
const imgCount = imgLinks.length;
// ダイアログ表示
const result = window.prompt(
"何番目のスライダー画像を開きますか?\nこのページでは" +
imgCount +
"番目まで選択できます。",
1
);
// 自然数変換
const imgNumber = Number(result) - 1;
// 入力確認
if (result == null) {
window.alert("キャンセルされました。");
} else {
if (imgNumber <= imgCount) {
// 画像URL取得
const url = imgLinks[imgNumber].src;
// 画像名取得
const fileName = "download-file" + (imgNumber + 1);
const link = document.createElement("a");
// ダウンロード用リンク設定
link.href = url;
link.download = fileName;
// aタグ作成
document.body.appendChild(link);
// リンククリック
link.click();
// aタグ削除
document.body.removeChild(link);
} else {
window.alert("正しい画像番号を入力してください!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment