Instantly share code, notes, and snippets.
Last active
October 14, 2024 04:06
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save arm64x/462bb47fca9f81560acd600dbdf0821a to your computer and use it in GitHub Desktop.
Fshare Select All and Copy Links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Fshare Select All and Copy Links | |
// @namespace http://tampermonkey.net/ | |
// @version 1.4 | |
// @description Thêm nút chọn tất cả và copy toàn bộ các URL của file trên Fshare.vn vào clipboard | |
// @author Lê Tí | |
// @match https://www.fshare.vn/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Tạo nút "Select All & Copy Links" | |
const selectAllCopyBtn = document.createElement('button'); | |
selectAllCopyBtn.textContent = 'Copy All Links'; | |
selectAllCopyBtn.style.position = 'fixed'; | |
selectAllCopyBtn.style.padding = '10px'; | |
selectAllCopyBtn.style.backgroundColor = '#28a745'; | |
selectAllCopyBtn.style.color = '#fff'; | |
selectAllCopyBtn.style.border = 'none'; | |
selectAllCopyBtn.style.cursor = 'pointer'; | |
selectAllCopyBtn.style.zIndex = '10000'; | |
// Hàm cập nhật vị trí của nút | |
function updateButtonPosition() { | |
if (window.innerWidth >= 1024) { | |
selectAllCopyBtn.style.top = '130px'; | |
selectAllCopyBtn.style.right = '10px'; | |
} else { | |
selectAllCopyBtn.style.top = '110px'; | |
selectAllCopyBtn.style.right = '10px'; | |
} | |
} | |
// Gọi hàm cập nhật vị trí khi khởi tạo trang | |
updateButtonPosition(); | |
// Thêm nút vào trang | |
document.body.appendChild(selectAllCopyBtn); | |
// Cập nhật vị trí nút khi kích thước màn hình thay đổi | |
window.addEventListener('resize', updateButtonPosition); | |
// Hàm chọn tất cả các checkbox | |
function selectAllCheckboxes() { | |
const checkboxes = document.querySelectorAll('.mdc-checkbox__native-control'); | |
checkboxes.forEach(checkbox => { | |
if (!checkbox.checked) { | |
checkbox.click(); // Giả lập click nếu chưa được chọn | |
} | |
}); | |
} | |
// Giả lập nhấp chuột phải để hiện context menu | |
function triggerContextMenuOnFirstFile() { | |
const firstFile = document.querySelector('.mdc-checkbox__native-control'); | |
if (firstFile) { | |
const event = new MouseEvent('contextmenu', { | |
bubbles: true, | |
cancelable: true, | |
view: window | |
}); | |
firstFile.dispatchEvent(event); // Giả lập chuột phải lên file đầu tiên | |
} | |
} | |
// Hàm lấy các liên kết của file từ menu context | |
function getFileLinksFromContextMenu() { | |
let links = ''; | |
const linkElements = document.querySelectorAll('[context-value="copy-link"]'); | |
linkElements.forEach(linkElement => { | |
const url = linkElement.getAttribute('data-clipboard-text'); | |
if (url) { | |
links += url + '\n'; // Thêm mỗi URL vào chuỗi | |
} | |
}); | |
return links.trim(); // Xóa khoảng trắng cuối cùng | |
} | |
// Sao chép văn bản vào clipboard | |
function copyToClipboard(text) { | |
const textarea = document.createElement('textarea'); | |
textarea.value = text; | |
document.body.appendChild(textarea); | |
textarea.select(); | |
document.execCommand('copy'); | |
document.body.removeChild(textarea); | |
} | |
// Hàm cập nhật CSS của menu tải xuống | |
function centerDownloadMenu() { | |
const downloadMenu = document.getElementById('download-menu'); | |
if (downloadMenu) { | |
downloadMenu.style.left = '50%'; | |
downloadMenu.style.transform = 'translateX(-50%)'; | |
downloadMenu.style.top = '50%'; // Đặt menu ở giữa theo chiều dọc | |
downloadMenu.style.position = 'fixed'; // Đảm bảo menu không cuộn theo trang | |
} | |
} | |
// Thêm quy tắc CSS ghi đè | |
function addCustomCSS() { | |
const style = document.createElement('style'); | |
style.innerHTML = ` | |
file-list .mdc-simple-menu, folder .mdc-simple-menu { | |
left: 50% !important; /* Ghi đè lên left: unset !important; */ | |
} | |
`; | |
document.head.appendChild(style); | |
} | |
// Thêm sự kiện click cho nút "Select All & Copy Links" | |
selectAllCopyBtn.addEventListener('click', function() { | |
selectAllCheckboxes(); // Chọn tất cả checkbox | |
setTimeout(() => { | |
triggerContextMenuOnFirstFile(); // Giả lập nhấp chuột phải để hiển thị menu | |
setTimeout(() => { | |
const links = getFileLinksFromContextMenu(); // Lấy tất cả các liên kết | |
if (links) { | |
copyToClipboard(links); // Sao chép liên kết vào clipboard | |
alert('Tất cả liên kết đã được sao chép vào clipboard!'); | |
centerDownloadMenu(); // Căn giữa menu tải xuống | |
addCustomCSS(); // Thêm quy tắc CSS để ghi đè | |
} else { | |
alert('Không tìm thấy liên kết nào.'); | |
} | |
}, 500); // Đợi để context menu xuất hiện và các liên kết được cập nhật | |
}, 1000); // Đợi 1 giây để đảm bảo các checkbox đã được chọn | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment