Skip to content

Commit

Permalink
twitter.downloadOriginalImage.user.js
Browse files Browse the repository at this point in the history
  • Loading branch information
eai04191 committed Jul 20, 2019
1 parent d031d0f commit 75a6e2b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -258,6 +258,16 @@ Tampermonkeyはわからん(ないっぽい)(つまり使えないとい

[Creative MarketのFree Goods Of The Week](https://creativemarket.com/free-goods)を開いたときに未購入の商品があれば自動で購入する。

### [twitter.downloadOriginalImage.user.js](/userscript/twitter.downloadOriginalImage.user.js)

<img src="https://i.imgur.com/9Nv5Smh.gif" align="right" width=50%>

新しいTwitterのUI用に書いた原寸画像ダウンローダー

画像の詳細かツイートの詳細を開いた状態でDキーを押すと、その時表示している画像の原寸画像を全てダウンロードする。

![](https://placehold.jp/ffffff/ffffff/1000x1.png)

### [](/userscript/)

## 🔖 bookmarklet
Expand Down
97 changes: 97 additions & 0 deletions userscript/twitter.downloadOriginalImage.user.js
@@ -0,0 +1,97 @@
// ==UserScript==
// @name Twitter オリジナル画像ダウンロード
// @author Eai <eai@mizle.net>
// @license MIT
// @version 1.0.0
// @match https://twitter.com/*
// @require https://cdn.jsdelivr.net/npm/@violentmonkey/shortcut@1
// @grant GM_download
// ==/UserScript==

/*global VM*/
/*global GM_download*/

(function() {
"use strict";

VM.registerShortcut("d", () => {
console.log("You have pressed D");
downloadImages();
});

function downloadImages() {
const state = getState();
if (state === "TIMELINE") {
console.log("Now is in Timeline. Do Nothing.");
return false;
}

const images = getImageList(state);

let urls = [];
images.forEach(img => {
urls.push(getOriginalURL(img.src));
});
console.log(urls);

urls.forEach(url => {
const o = new URL(url);

const ext = o.searchParams.get("format");
const dlObj = {
url: url,
name: o.pathname.slice(1).replace("/", "-") + "." + ext,
onload: null,
};
GM_download(dlObj);
});
}

function getOriginalURL(url) {
if (!/pbs\.twimg\.com\/media\/.+format=.../.test(url)) {
return null;
}
const o = new URL(url);

const originalUrl =
o.origin +
o.pathname +
"?format=" +
o.searchParams.get("format") +
"&name=orig";

return originalUrl;
}

function getImageList(state) {
if (state === "IMAGE_MODAL") {
return [
document.querySelector(
"img[src^='https://pbs.twimg.com/media']"
),
];
}

if (state === "TWEET_DETAILED") {
return document.querySelectorAll(
"article img[src^='https://pbs.twimg.com/media']"
);
}
}

// DOMの今の状態を返す
// "TIMELINE", "IMAGE_MODAL", "TWEET_DETAILED"
function getState() {
const l = document.location;
const imageModalRegEx = /twitter\.com\/.+\/status\/\d+\/photo\/\d/;
const tweetDetailedRegEx = /twitter\.com\/.+\/status\/\d+$/;

const state = imageModalRegEx.test(l)
? "IMAGE_MODAL"
: tweetDetailedRegEx.test(l)
? "TWEET_DETAILED"
: "TIMELINE";

return state;
}
})();

0 comments on commit 75a6e2b

Please sign in to comment.