Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TwoSquirrels/e59797217afe457315e5334b22bf57bc to your computer and use it in GitHub Desktop.
Save TwoSquirrels/e59797217afe457315e5334b22bf57bc to your computer and use it in GitHub Desktop.
画像保存ブックマークレット

画像保存ブックマークレット

This bookmarklet downloads up to four images from a Twitter page, and names them based on the username, tweet ID, and image number. It only works on Twitter pages and will throw an error if run on a different website.

Bookmarklet

javascript:(async()=>{if(location.hostname.match(/\.?twitter\.com$/)){let[,t,,e]=location.pathname.split("/");return[...document.getElementsByTagName("img")].filter(t=>t.src.startsWith("https://pbs.twimg.com/media/")).slice(0,4).map((a,c)=>({url:a.src.replace(/&name=.+/,""),name:`Title-twitter-${t}-${e}-${c+1}.jpg`}))}throw Error("This page is not supported.")})().then(t=>t.forEach(async({url:t,name:e})=>{let a=document.createElement("a");a.href=URL.createObjectURL(await (await fetch(t,{mode:"cors"})).blob()),a.download=e,a.click()})).catch(t=>alert(t))

Features

  • Twitter (filename: Title-twitter-{username}-{tweet_id}-{photo_index}.jpg)

Original

(async () => {
  if (location.hostname.match(/\.?twitter\.com$/)) {
    // Twitter
    const [, username, , tweetId] = location.pathname.split("/");
    return [...document.getElementsByTagName("img")]
      .filter((img) => img.src.startsWith("https://pbs.twimg.com/media/"))
      .slice(0, 4)
      .map((img, i) => ({
        url: img.src.replace(/&name=.+/, ""),
        name: `Title-twitter-${username}-${tweetId}-${i + 1}.jpg`,
      }));
  }
  throw new Error("This page is not supported.");
})()
  .then((images) => images.forEach(async ({ url, name }) => {
    const a = document.createElement("a");
    a.href = URL.createObjectURL(await (await fetch(url, { mode: "cors" })).blob());
    a.download = name;
    a.click();
  }))
  .catch((error) => alert(error));
@TwoSquirrels
Copy link
Author

Pixiv に対応させようとしたら、どうやら Pixiv は画像の表示の仕組みが複雑っぽくて、うまくダウンロードできなかった。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment