forked from hoothin/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathX-Downloader.user.js
More file actions
165 lines (164 loc) · 7.82 KB
/
Copy pathX-Downloader.user.js
File metadata and controls
165 lines (164 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// ==UserScript==
// @name X-Downloader-Script
// @name:zh-CN X-Downloader-Script
// @name:zh-TW X-Downloader-Script
// @name:ja X-Downloader-Script
// @namespace hoothin
// @version 2025-08-18
// @license MIT
// @description Enhances your Twitter (X) experience by adding a convenient download button to images and videos (GIFs), enabling easy, one-click saving of media.
// @description:zh-CN 优化你的推特 (X) 浏览体验,直接在图片和视频(GIF)上添加一个便捷的下载按钮,一键轻松保存喜欢的媒体内容。
// @description:zh-TW 優化您的 Twitter (X) 瀏覽體驗,直接在圖片及影片(GIF)上新增一個便捷的下載按鈕,一鍵輕鬆儲存喜愛的媒體內容。
// @description:ja Twitter (X) の画像や動画(GIF)に便利なダウンロードボタンを追加し、ワンクリックでお気に入りのメディアを簡単に保存できるようにします。
// @author hoothin
// @match https://x.com/*
// @match https://twitter.com/*
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// @downloadURL https://update.greasyfork.org/scripts/545186/X-Downloader.user.js
// @updateURL https://update.greasyfork.org/scripts/545186/X-Downloader.meta.js
// ==/UserScript==
(function() {
'use strict';
let downloadBtn = document.createElement("a"), touch = false;
downloadBtn.target = "_blank";
downloadBtn.style.cssText = "background: #000000aa; border-radius: 50%; transition: opacity ease 0.3s; position: absolute; top: 0; right: 0px; cursor: pointer; opacity: 0; padding: 5px;";
downloadBtn.innerHTML = `<svg width="25" height="25" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M3 17a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zm3.293-7.707a1 1 0 011.414 0L9 10.586V3a1 1 0 112 0v7.586l1.293-1.293a1 1 0 111.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>`;
downloadBtn.addEventListener("mousedown", e => {
let parent = downloadBtn.parentNode;
if (!parent) return;
let img = parent.querySelector('[data-testid="tweetPhoto"]>img,[data-testid="card.layoutLarge.media"] img');
if (img) {
let newsrc = img.src.replace("_normal.",".").replace("_200x200.",".").replace("_mini.",".").replace("_bigger.",".").replace(/_x\d+\./,"."), imgname;
if (/\.svg$/.test(newsrc)) return;
if (newsrc == img.src) {
newsrc=newsrc.replace(/\?format=/i, ".").replace(/\&name=/i, ":").replace(/\.(?=[^\.\/]*$)/, "?format=").replace( /(:large|:medium|:small|:orig|:thumb|:[\dx]+)/i, "");
if (newsrc != img.src) {
newsrc = newsrc + "&name=orig";
}
}
while(parent) {
if (parent.nodeName == "ARTICLE" && parent.dataset && parent.dataset.testid == "tweet") {
break;
}
parent = parent.parentNode;
}
if (parent) {
const time = parent.querySelector('time[datetime]');
const user = parent.querySelector('[role="link"]>div>div>span>span');
let formatMatch = img.src.match(/format=(\w+)/), ext = "jpg";
if (formatMatch) {
ext = formatMatch[1];
} else {
formatMatch = newsrc.match(/\.(\w+)/);
if (formatMatch) {
ext = formatMatch[1];
}
}
imgname = `${user.innerText} ${time.innerText.replace(/(.*) · (.*)/, "$2 $1")}.${ext}`;
}
downloadBtn.href = newsrc;
if (e.altKey || touch) {
downloadByFetch(newsrc, imgname);
}
} else {
while(parent) {
if (parent.nodeName == "ARTICLE" && parent.dataset && parent.dataset.testid == "tweet") {
break;
}
parent = parent.parentNode;
}
if (parent) {
downloadBtn.removeAttribute('download');
let link = parent.querySelector('a[role="link"][aria-label][href^="/"]');
downloadBtn.href = `https://twitter.hoothin.com/?url=${encodeURIComponent(link ? link.href : document.location.href)}`;
if (e.altKey || touch) {
window.open(downloadBtn.href, "_blank");
}
}
}
});
downloadBtn.addEventListener("click", e => {
if (e.altKey || touch) {
e.preventDefault();
e.stopPropagation();
}
});
downloadBtn.addEventListener("mouseenter", () => {
downloadBtn.style.opacity = 1;
});
downloadBtn.addEventListener("mouseleave", () => {
setTimeout(() => {
downloadBtn.style.opacity = 0.1;
}, 100);
});
async function downloadByFetch(imageUrl, filename) {
try {
const response = await fetch(imageUrl);
if (!response.ok) throw new Error('CORS request failed');
const blob = await response.blob();
const blobUrl = URL.createObjectURL(blob);
const tempLink = document.createElement('a');
tempLink.href = blobUrl;
tempLink.setAttribute('download', filename);
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
URL.revokeObjectURL(blobUrl);
} catch (error) {
console.error('error:', error);
window.open(imageUrl, '_blank');
}
}
const show = (ele) => {
ele.appendChild(downloadBtn);
setTimeout(() => {
downloadBtn.style.opacity = touch ? 0.8 : 0.6;
}, 0);
};
const addBtn = e => {
if (e.target.dataset && e.target.dataset.testid == "card.layoutLarge.media") {
show(e.target.parentNode);
} else if (e.target.dataset && e.target.dataset.testid == "tweetPhoto") {
show(e.target.parentNode);
} else if (e.target.dataset && /^video\-player/.test(e.target.dataset.testid)) {
show(e.target.parentNode);
} else if (e.target.parentNode && e.target.parentNode.dataset && e.target.parentNode.dataset.testid == "tweetPhoto") {
show(e.target.parentNode.parentNode);
}
};
function isElementVisible(el) {
const rect = el.getBoundingClientRect();
return rect.top < window.innerHeight && rect.top > 0 && rect.bottom >= 0;
}
function findFirstVisibleElement(selector) {
const elements = document.querySelectorAll(selector);
const firstVisibleElement = Array.from(elements).find(el => {
return isElementVisible(el);
});
return firstVisibleElement;
}
let checkTimer;
const touchCheck = e => {
clearTimeout(checkTimer);
if (e.target == downloadBtn) return;
checkTimer = setTimeout(() => {
if (isElementVisible(downloadBtn)) return;
let target = findFirstVisibleElement("[data-testid='card.layoutLarge.media']");
if (target) {
return show(target.parentNode);
}
target = findFirstVisibleElement("[data-testid='tweetPhoto']");
if (target) {
return show(target.parentNode);
}
target = findFirstVisibleElement("[data-testid^='video-player']");
if (target) {
return show(target.parentNode);
}
}, 100);
};
document.addEventListener("mouseenter", addBtn, true);
document.addEventListener("touchstart", e => {touch = true; addBtn(e);}, true);
document.addEventListener("touchend", touchCheck, true);
})();