Skip to content

Commit bbf451f

Browse files
committed
Update X-Downloader.user.js
1 parent 1782dd9 commit bbf451f

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

X-Downloader/X-Downloader.user.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @name:zh-TW X-Downloader-Script
55
// @name:ja X-Downloader-Script
66
// @namespace hoothin
7-
// @version 2025-08-17
7+
// @version 2025-08-18
88
// @license MIT
99
// @description Enhances your Twitter (X) experience by adding a convenient download button to images and videos (GIFs), enabling easy, one-click saving of media.
1010
// @description:zh-CN 优化你的推特 (X) 浏览体验,直接在图片和视频(GIF)上添加一个便捷的下载按钮,一键轻松保存喜欢的媒体内容。
@@ -21,7 +21,7 @@
2121

2222
(function() {
2323
'use strict';
24-
let downloadBtn = document.createElement("a");
24+
let downloadBtn = document.createElement("a"), touch = false;
2525
downloadBtn.target = "_blank";
2626
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;";
2727
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>`;
@@ -59,7 +59,7 @@
5959
imgname = `${user.innerText} ${time.innerText.replace(/(.*) · (.*)/, "$2 $1")}.${ext}`;
6060
}
6161
downloadBtn.href = newsrc;
62-
if (e.altKey) {
62+
if (e.altKey || touch) {
6363
downloadByFetch(newsrc, imgname);
6464
}
6565
} else {
@@ -114,7 +114,7 @@
114114
const show = (ele) => {
115115
ele.appendChild(downloadBtn);
116116
setTimeout(() => {
117-
downloadBtn.style.opacity = 0.6;
117+
downloadBtn.style.opacity = touch ? 0.8 : 0.6;
118118
}, 0);
119119
};
120120
const addBtn = e => {
@@ -128,6 +128,34 @@
128128
show(e.target.parentNode.parentNode);
129129
}
130130
};
131+
function findFirstVisibleElement(selector) {
132+
const elements = document.querySelectorAll(selector);
133+
const firstVisibleElement = Array.from(elements).find(el => {
134+
const rect = el.getBoundingClientRect();
135+
return rect.top < window.innerHeight && rect.top > 0 && rect.bottom >= 0;
136+
});
137+
return firstVisibleElement;
138+
}
139+
let checkTimer;
140+
const touchCheck = e => {
141+
clearTimeout(checkTimer);
142+
if (e.target == downloadBtn) return;
143+
checkTimer = setTimeout(() => {
144+
let target = findFirstVisibleElement("[data-testid='card.layoutLarge.media']");
145+
if (target) {
146+
return show(target.parentNode);
147+
}
148+
target = findFirstVisibleElement("[data-testid='tweetPhoto']");
149+
if (target) {
150+
return show(target.parentNode);
151+
}
152+
target = findFirstVisibleElement("[data-testid^='video-player']");
153+
if (target) {
154+
return show(target.parentNode);
155+
}
156+
}, 100);
157+
};
131158
document.addEventListener("mouseenter", addBtn, true);
132-
document.addEventListener("touchstart", addBtn, true);
159+
document.addEventListener("touchstart", e => {touch = true; addBtn(e);}, true);
160+
document.addEventListener("touchend", touchCheck, true);
133161
})();

0 commit comments

Comments
 (0)