// ==UserScript==
// @name Switch520下载优化
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Switch520点击下载按钮后,直接弹出下载地址,无需反复跳转、输入密码
// @author 一身惆怅
// @match http*://*.gamer520.com/*.html
// @icon https://img.piclabo.xyz/2023/10/25/d67adcffb89dd.jpg
// @run-at document-idle
// @grant GM_xmlhttpRequest
// @connect gamer520.com
// @connect xxxxx528.com
// @license MIT
// ==/UserScript==
(() => {
'use strict';
const swalOptions = {
warning: (message) => ({ title: message, type: "warning" }),
success: (title, htmlContent) => ({ title, type: "success", html: htmlContent })
};
const showMessage = (type, ...args) => Swal.fire(swalOptions[type](...args));
const request = async (url, method, headers = {}, data = null) => {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({ method, url, headers, data, onload: (response) => resolve(response.responseText), onerror: reject });
});
};
const disableButton = button => button.classList.add('disabled');
const enableButton = button => button.classList.remove('disabled');
const parseHTML = responseText => new DOMParser().parseFromString(responseText, "text/html");
const copyTextToClipboard = text => {
navigator.clipboard.writeText(text).then(() => {
}, err => {
const textarea = document.createElement('textarea');
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
});
};
document.addEventListener('click', event => {
if (event.target.matches('.copy-code-link')) {
copyTextToClipboard(event.target.dataset.code);
event.preventDefault();
}
});
const modifyContentElement = contentElement => {
contentElement.querySelectorAll('p').forEach(p => {
p.querySelectorAll('a').forEach(a => a.setAttribute('target', '_blank'));
const codeMatch = p.textContent.match(/提取码[::]\s*(\w+)/) || p.textContent.match(/解压密码[::]\s*(\w+)/);
if (codeMatch) {
const codeType = p.textContent.includes('提取码') ? '提取码' : '解压密码';
p.innerHTML = `点击复制${codeType}: ${codeMatch[1]}`;
}
if (p.textContent.includes('Onedrive盘下载')) {
p.innerHTML = `
`;
}
});
};
const getDownloadHTML = async (url) => {
try {
const responseText = await request(url, 'GET');
const doc = parseHTML(responseText);
const contentElement = doc.querySelector('div.entry-content.u-text-format.u-clearfix');
const downloadButton = doc.querySelector('a.go-down.btn.btn--secondary.btn--block');
if (contentElement) {
modifyContentElement(contentElement);
let messageHTML = contentElement.innerHTML;
if (downloadButton) {
const postId = downloadButton.dataset.id;
const redirectUrl = await getRedirectUrl(`https://xxxxx528.com/go/?post_id=${postId}`);
if (redirectUrl) {
messageHTML += `
Onedrive盘下载: 点击下载(推荐浏览器直接下载)
`; } } showMessage("success", "已获取下载内容", messageHTML); } else { showMessage("warning", "未找到下载内容"); } } catch (error) { showMessage("warning", "请求失败"); } }; const submitPassword = async (password, url) => { const passwordUrl = 'https://xxxxx528.com/wp-login.php?action=postpass'; const headers = { 'Content-Type': 'application/x-www-form-urlencoded' }; const data = `post_password=${password}&Submit=%E6%8F%90%E4%BA%A4`; try { await request(passwordUrl, 'POST', headers, data); await getDownloadHTML(url); } catch (error) { showMessage("warning", "提交密码失败"); } }; const readPassword = async (shareUrl) => { try { const responseText = await request(shareUrl, 'GET'); const doc = parseHTML(responseText); const passwordElement = doc.querySelector('h1.entry-title'); if (passwordElement) { const passwordMatch = passwordElement.textContent.match(/\d+/); if (passwordMatch) { await submitPassword(passwordMatch[0], shareUrl); } else { showMessage("warning", "未能成功读取分享页密码"); } } else { showMessage("warning", "未找到分享页密码"); } } catch (error) { showMessage("warning", "请求失败"); } }; const getRedirectUrl = async (url) => { try { const responseText = await request(url, 'GET'); const redirectRegex = /window.location\s*=\s*['"]([^'"]+)['"]/; const redirectMatch = responseText.match(redirectRegex); if (redirectMatch && redirectMatch[1]) { return redirectMatch[1]; } else { showMessage("warning", "未找到跳转网址"); return null; } } catch (error) { showMessage("warning", "请求失败,请查看控制台以获取更多信息"); console.error(error); return null; } }; const handleButtonClick = async (event) => { const button = event.target; disableButton(button); const postId = button.dataset.id; const targetUrl = await getRedirectUrl(`https://www.gamer520.com/go/?post_id=${postId}`); if (targetUrl) { await readPassword(targetUrl); } enableButton(button); }; const addSwalStyle = () => { const styleNode = document.createElement("style"); styleNode.textContent = '.swal2-content { text-align: left !important; line-height: unset !important; } .disabled { pointer-events: none; opacity: 0.7; }'; document.head.appendChild(styleNode); }; const init = () => { const payBoxElement = document.querySelector('.go-down'); if (payBoxElement) { addSwalStyle(); $(".go-down").off("click").on("click", handleButtonClick); } }; init(); })();