Twitter Media Downloader

Save Video/Photo by One-Click.

当前为 2021-03-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Twitter Media Downloader
  3. // @name:ja Twitter Media Downloader
  4. // @name:zh-cn Twitter 媒体下载
  5. // @name:zh-tw Twitter 媒體下載
  6. // @description Save Video/Photo by One-Click.
  7. // @description:ja ワンクリックで動画・画像を保存する。
  8. // @description:zh-cn 一键保存视频/图片
  9. // @description:zh-tw 一鍵保存視頻/圖片
  10. // @version 0.53
  11. // @author AMANE
  12. // @namespace none
  13. // @match https://twitter.com/*
  14. // @grant GM_setValue
  15. // @grant GM_getValue
  16. // @grant GM_download
  17. // ==/UserScript==
  18. /* jshint esversion: 8 */
  19.  
  20. (function () {
  21. 'use strict';
  22.  
  23. const preset_filename = 'twitter_{user-name}(@{user-id})_{date-time}_{status-id}_{file-type}';
  24.  
  25. const language = {
  26. en: {download: 'Download', completed: 'Download Completed', settings: 'Download Settings', save: 'Save', confirm: 'Confirm Save As Dialog', record: 'Remember Download History', clear: '(Clear)', clear_confirm: 'Clear download history?', pattern: 'File Name Pattern'},
  27. ja: {download: 'ダウンロード', completed: 'ダウンロード完了', settings: 'ダウンロード設定', save: '保存', confirm: '保存場所を確認する', record: 'ダウンロード履歴を保存する', clear: '(クリア)', clear_confirm: 'ダウンロード履歴を削除する?', pattern: 'ファイル名パターン'},
  28. zh: {download: '下载', completed: '下载完成', settings: '下载设置', save: '保存', confirm: '确认文件名和保存位置', record: '保存下载记录', clear: '(清除)', clear_confirm: '确认要清除下载记录?', pattern: '文件名格式'},
  29. 'zh-Hant': {download: '下載', completed: '下載完成', settings: '下載設置', save: '保存', confirm: '確認文件名和保存位置', record: '保存下載記錄', clear: '(清除)', clear_confirm: '確認要清除下載記錄?', pattern: '文件名規則'},
  30. };
  31. const str = language[document.querySelector('html').lang] || language.en;
  32.  
  33. const svg = `
  34. <g class="download"><path d="M3,14 v5 q0,2 2,2 h14 q2,0 2,-2 v-5 M7,10 l4,4 q1,1 2,0 l4,-4 M12,3 v11" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" /></g>
  35. <g class="completed"><path d="M3,14 v5 q0,2 2,2 h14 q2,0 2,-2 v-5 M7,10 l3,4 q1,1 2,0 l8,-11" fill="none" stroke="#1DA1F2" stroke-width="2" stroke-linecap="round" /></g>
  36. <g class="loading"><circle cx="12" cy="12" r="10" fill="none" stroke="#1DA1F2" stroke-width="4" opacity="0.4" /><path d="M12,2 a10,10 0 0 1 10,10" fill="none" stroke="#1DA1F2" stroke-width="4" stroke-linecap="round" /></g>
  37. <g class="failed"><circle cx="12" cy="12" r="11" fill="#f33" stroke="currentColor" stroke-width="2" opacity="0.8" /><path d="M14,5 a1,1 0 0 0 -4,0 l0.5,9.5 a1.5,1.5 0 0 0 3,0 z M12,17 a2,2 0 0 0 0,4 a2,2 0 0 0 0,-4" fill="#fff" stroke="none" /></g>
  38. `;
  39.  
  40. const css = `
  41. .tmd-down > div > div > div:nth-child(2) {display: none}
  42. .tmd-down:hover > div > div {color: rgba(29, 161, 242, 1.0);}
  43. .tmd-down:hover > div > div > div > div {background-color: rgba(29, 161, 242, 0.1);}
  44. .tmd-down:active > div > div > div > div {background-color: rgba(29, 161, 242, 0.2);}
  45. .tmd-down.loading svg {animation: spin 1s linear infinite;}
  46. .tmd-down g {display: none;}
  47. .tmd-down.download g.download, .tmd-down.completed g.completed, .tmd-down.loading g.loading,.tmd-down.failed g.failed {display: unset;}
  48. @keyframes spin {0% {transform: rotate(0deg);} 100% {transform: rotate(360deg);}}
  49. .tmd-btn {display: inline-block; background-color: #1DA1F2; color: #FFFFFF; padding: 0 20px; border-radius: 99px;}
  50. .tmd-tag {display: inline-block; background-color: #FFFFFF; color: #1DA1F2; padding: 0 10px; border-radius: 10px; border: 1px solid #1DA1F2; font-weight: bold; margin: 5px;}
  51. .tmd-btn:hover {background-color: rgba(29, 161, 242, 0.9);}
  52. .tmd-tag:hover {background-color: rgba(29, 161, 242, 0.1);}
  53. `;
  54.  
  55. let history = storage('history');
  56. document.head.insertAdjacentHTML('beforeend', '<style>' + css + '</style>');
  57. new MutationObserver(mutations => mutations.forEach(mutation => {
  58. mutation.addedNodes.forEach(node => {
  59. let article = node.tagName == 'DIV' && node.querySelector('article');
  60. btn_inject(article);
  61. });
  62. })).observe(document.body, {childList: true, subtree: true});
  63.  
  64. function btn_inject(article) {
  65. let media = article.querySelector('div[role="progressbar"], div[data-testid="playButton"], a[href*="/photo/1"], a[href="/settings/safety"]');
  66. if (!media || article.dataset.injected) return;
  67. article.dataset.injected = 'true';
  68. let status_id = article.querySelector('a[href*="/status/"]').href.split('/status/').pop().split('/').shift();
  69. let is_exist = history.indexOf(status_id) >= 0;
  70. let group = article.querySelector('div[role="group"]');
  71. let btn = group.querySelector(':scope>:first-child').cloneNode(true);
  72. btn.querySelector('svg').innerHTML = svg;
  73. group.appendChild(btn);
  74. btn_status(btn, is_exist ? 'completed' : 'download', is_exist ? str.completed : str.download);
  75. btn.onclick = () => btn_click(btn, status_id, is_exist);
  76. btn.oncontextmenu = e => {
  77. e.preventDefault();
  78. down_settings();
  79. };
  80. }
  81.  
  82. async function btn_click(btn, status_id, is_exist) {
  83. if (btn.classList.contains('loading')) return;
  84. btn_status(btn, 'loading');
  85. let filename = (await GM_getValue('filename', preset_filename)).split('\n').join('');
  86. let confirm = await GM_getValue('confirm', false);
  87. let record = await GM_getValue('record', true);
  88. let json = await fetch_json(status_id);
  89. let tweet = json.globalObjects.tweets[status_id];
  90. let user = json.globalObjects.users[tweet.user_id_str];
  91. let info = {
  92. 'status-id': status_id,
  93. 'user-name': user.name,
  94. 'user-id': user.screen_name,
  95. 'date-time': formatDate(tweet.created_at, 'YYYYMMDD-hhmmss')
  96. };
  97. let medias = tweet.extended_entities && tweet.extended_entities.media;
  98. if (medias) {
  99. let tasks = medias.length;
  100. medias.forEach((media, i) => {
  101. info.url = media.type == 'photo' ? media.media_url + ':orig' : media.video_info.variants.filter(n => n.content_type == 'video/mp4').sort((a, b) => b.bitrate - a.bitrate)[0].url;
  102. info.file = info.url.split('/').pop().split(/[:?]/).shift();
  103. info['file-name'] = info.file.split('.').shift();
  104. info['file-ext'] = info.file.split('.').pop();
  105. info['file-type'] = media.type.replace('animated_', '');
  106. info.out = (filename.replace(/\.?{file-ext}/, '') + (medias.length > 1 && !filename.match('{file-name}') ? '-' + i : '') + '.{file-ext}').replace(/{([^{}]+)}/g, (match, name) => info[name]);
  107. GM_download({
  108. url: info.url,
  109. name: info.out,
  110. // saveAs: confirm,
  111. onload: () => {
  112. tasks -= 1;
  113. if (tasks === 0) {
  114. btn_status(btn, 'completed', str.completed);
  115. if (record && !is_exist) {
  116. history.push(status_id);
  117. storage('history', status_id);
  118. }
  119. }
  120. },
  121. onerror: result => {
  122. tasks = - 1;
  123. btn_status(btn, 'failed', result.details.current);
  124. }
  125. });
  126. });
  127. } else {
  128. btn_status(btn, 'failed', 'MEDIA_NOT_FOUND');
  129. }
  130. }
  131.  
  132. function btn_status(btn, css, title) {
  133. btn.classList.remove('tmd-down', 'download', 'completed', 'loading', 'failed');
  134. btn.classList.add('tmd-down', css);
  135. if (title) btn.title = title;
  136. }
  137.  
  138. async function down_settings() {
  139. const $element = (parent, tag, style, content, css) => {
  140. let el = document.createElement(tag);
  141. if (style) el.style.cssText = style;
  142. if (typeof content !== 'undefined') {
  143. if (tag == 'input') {
  144. if (content == 'checkbox') el.type = content;
  145. else el.value = content;
  146. } else el.innerHTML = content;
  147. }
  148. if (css) css.split(' ').forEach(c => el.classList.add(c));
  149. parent.appendChild(el);
  150. return el;
  151. };
  152. let wapper = $element(document.body, 'div', 'position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; background-color: #0009; z-index: 10;');
  153. let wapper_close;
  154. wapper.onmousedown = e => {
  155. wapper_close = e.target == wapper;
  156. };
  157. wapper.onmouseup = e => {
  158. if (wapper_close && e.target == wapper) wapper.remove();
  159. };
  160. let dialog = $element(wapper, 'div', 'position: absolute; left: 50%; top: 50%; transform: translateX(-50%) translateY(-50%); width: fit-content; width: -moz-fit-content; background-color: #f3f3f3; border: 1px solid #ccc; border-radius: 10px;');
  161. let title = $element(dialog, 'h3', 'margin: 10px 20px;', str.settings);
  162. let options = $element(dialog, 'div', 'margin: 10px; border: 1px solid #ccc; border-radius: 5px;');
  163. // let confirm_input = $element($element(options, 'label', 'display: block; margin: 10px;', str.confirm), 'input', 'float: left;', 'checkbox');
  164. // confirm_input.checked = await GM_getValue('confirm', false);
  165. // confirm_input.onchange = () => GM_setValue('confirm', confirm_input.checked);
  166. let record_label = $element(options, 'label', 'display: block; margin: 10px;', str.record);
  167. let record_input = $element(record_label, 'input', 'float: left;', 'checkbox');
  168. record_input.checked = await GM_getValue('history', true);
  169. record_input.onchange = () => GM_setValue('history', record_input.checked);
  170. $element(record_label, 'label', 'margin: 10px; color: blue;', str.clear).onclick = () => {
  171. if (confirm(str.clear_confirm)) {
  172. history = [];
  173. localStorage.removeItem('history');
  174. }
  175. };
  176. let filename_div = $element(dialog, 'div', 'margin: 10px; border: 1px solid #ccc; border-radius: 5px;');
  177. let filename_label = $element(filename_div, 'label', 'display: block; margin: 10px 15px;', str.pattern);
  178. let filename_input = $element(filename_label, 'textarea', 'display: block; min-width: 500px; max-width: 500px; min-height: 100px; font-size: inherit;', await GM_getValue('filename', preset_filename));
  179. let filename_tags = $element(filename_div, 'label', 'display: table; margin: 10px;', `
  180. <span class="tmd-tag" title="user name">{user-name}</span>
  181. <span class="tmd-tag" title="The user name after @ sign.">{user-id}</span>
  182. <span class="tmd-tag" title="example: 1234567890987654321">{status-id}</span>
  183. <span class="tmd-tag" title="YYYYMMDD-hhmmss\nexample: 20201231-235959">{date-time}</span><br>
  184. <span class="tmd-tag" title="Type of &#34;video&#34; or &#34;photo&#34; or &#34;gif&#34;.">{file-type}</span>
  185. <span class="tmd-tag" title="Original filename from URL.">{file-name}</span>
  186. <span class="tmd-tag" title="Unnecessary. Will be added automatically.">{file-ext}</span>
  187. `);
  188. filename_input.selectionStart = filename_input.value.length;
  189. filename_tags.querySelectorAll('.tmd-tag').forEach(tag => {
  190. tag.onclick = () => {
  191. let ss = filename_input.selectionStart;
  192. let se = filename_input.selectionEnd;
  193. filename_input.value = filename_input.value.substring(0, ss) + tag.innerText + filename_input.value.substring(se);
  194. filename_input.selectionStart = ss + tag.innerText.length;
  195. filename_input.selectionEnd = ss + tag.innerText.length;
  196. filename_input.focus();
  197. };
  198. });
  199. let btn_save = $element(title, 'label', 'float: right;', str.save, 'tmd-btn');
  200. btn_save.onclick = async() => {
  201. await GM_setValue('filename', filename_input.value);
  202. wapper.remove();
  203. };
  204. }
  205.  
  206. function storage(name, value) {
  207. let data = JSON.parse(localStorage.getItem(name) || '[]');
  208. if (value) data.push(value);
  209. else return data;
  210. localStorage.setItem(name, JSON.stringify(data));
  211. }
  212.  
  213. async function fetch_json(status_id) {
  214. let url = 'https://twitter.com/i/api/2/timeline/conversation/' + status_id + '.json?tweet_mode=extended&include_entities=false&include_user_entities=false';
  215. let cookies = getCookie();
  216. let headers = {
  217. 'authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA',
  218. 'x-twitter-active-user': 'yes',
  219. 'x-twitter-client-language': cookies.lang,
  220. 'x-csrf-token': cookies.ct0
  221. };
  222. if (cookies.ct0.length == 32) headers['x-guest-token'] = cookies.gt;
  223. return await fetch(url, {headers: headers}).then(result => result.json());
  224. }
  225.  
  226. function getCookie(name) {
  227. let cookies = {};
  228. document.cookie.split(';').filter(n => n.indexOf('=') > 0).forEach(n => {
  229. n.replace(/^([^=]+)=(.+)$/, (match, name, value) => {
  230. cookies[name.trim()] = value.trim();
  231. });
  232. });
  233. return name ? cookies[name] : cookies;
  234. }
  235.  
  236. function formatDate(i, o) {
  237. let d = new Date(i);
  238. let v = {
  239. YYYY: d.getUTCFullYear().toString(),
  240. YY: d.getUTCFullYear().toString(),
  241. MM: '0' + (d.getUTCMonth() + 1),
  242. DD: '0' + d.getUTCDate(),
  243. hh: '0' + d.getUTCHours(),
  244. mm: '0' + d.getUTCMinutes(),
  245. ss: '0' + d.getUTCSeconds()
  246. };
  247. return o.replace(/(YY(YY)?|MM|DD|hh|mm|ss)/g, n => v[n].substr( - n.length));
  248. }
  249.  
  250. })();