- // ==UserScript==
- // @name Greasy Fork镜像脚本页面适用于网址增强
- // @namespace https://gf.qytechs.cn/zh-CN/users/1169082-%E4%BA%BA%E6%B0%91%E7%9A%84%E5%8B%A4%E5%8A%A1%E5%91%98
- // @version 0.8
- // @description 脚本详情页适用于网址不默认跳转搜索 转为可点击的文本链接并弹出提示
- // @author 人民的勤务员 <millle@foxmail.com>
- // @match https://*.gf.qytechs.cn/zh-CN/scripts/*
- // @match https://*.sleazyfork.org/zh-CN/scripts/*
- // @icon https://www.google.com/s2/favicons?domain=https://gf.qytechs.cn
- // @license MIT
- // @grant GM_getValue
- // @grant GM_setValue
- // ==/UserScript==
-
- (function() {
- 'use strict';
- let useWindowOpen = GM_getValue('useWindowOpen', false); // 默认在当前页面跳转
- let linkBehavior = GM_getValue('linkBehavior', 0);
-
- function Toast(msg, duration) {
- duration = isNaN(duration) ? 3000 : duration;
- var m = document.createElement('div');
- m.innerHTML = msg;
- m.style.cssText = "max-width:60%;min-width: 150px;padding:0 14px;height: 40px;color: black;line-height: 40px;text-align: center;border-radius: 12px;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 2147483647;background: white;font-size: 16px;";
-
-
- document.body.appendChild(m);
- setTimeout(function () {
- var d = 0.5;
- m.style.transition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
- m.style.opacity = '0'; setTimeout(function () { document.body.removeChild(m) }, d * 1000);
- }, duration);
- }
-
- function navigateTo(url, useWindowOpen) {
- if (useWindowOpen) {
- window.open(url, '_blank');
- } else {
- window.location.href = url;
- }
- }
-
- const ddElements = document.querySelectorAll('dd.script-show-applies-to ul.block-list.expandable > li');
-
- ddElements.forEach(dd => {
- const link = dd.querySelector('a[title^="查看其他"]');
- const text = dd.textContent.trim();
- if (text.includes('*')) {
- return;
- }
- if (link) {
- link.addEventListener('click', function(event) {
- event.preventDefault();
- handleLinkClick(text);
- });
- } else {
- const newLink = document.createElement('a');
- newLink.textContent = text;
- newLink.href = '#';
- newLink.addEventListener('click', function(event) {
- event.preventDefault();
- handleLinkClick(text);
- });
- dd.textContent = '';
- dd.appendChild(newLink);
- }
- });
-
- function handleLinkClick(linkText) {
- if (linkBehavior === 0) {
- // 创建自定义对话框
- const bodyBackgroundColor = window.getComputedStyle(document.body).backgroundColor;
-
- const dialogBox = document.createElement('div');
- dialogBox.style.position = 'fixed';
- dialogBox.style.top = '50%';
- dialogBox.style.left = '50%';
- dialogBox.style.transform = 'translate(-50%, -50%)';
- dialogBox.style.background = bodyBackgroundColor;
- dialogBox.style.padding = '20px';
- dialogBox.style.boxShadow = '0 4px 6px rgba(0, 0, 0, 0.1)';
- dialogBox.style.borderRadius = '8px';
- dialogBox.style.zIndex = '9999';
- dialogBox.innerHTML = `
- <span id="closeBtn" style="position: absolute; top: 10px; right: 10px; color: red; cursor: pointer; font-size: 30px;">×</span>
- <p style="font-weight: bold; font-size: 20px;"> <span id="linkTextSpan" style="color: red;">${linkText}</span> 操作</p>
- <button id="forumSearchBtn">论坛搜索</button>
- <button id="openUrlBtn">打开网址</button>
- <button id="copyLinkBtn">复制链接</button>
- `;
- document.body.appendChild(dialogBox);
- // 记录对话框初始位置和鼠标初始位置
- // 记录对话框初始位置和鼠标/手指触摸初始位置
- let dialogInitialX, dialogInitialY, initialX, initialY;
-
- // 鼠标按下/触摸开始事件
- dialogBox.addEventListener('mousedown', onMouseDown);
- dialogBox.addEventListener('touchstart', onTouchStart);
-
- // 鼠标按下事件处理函数
- function onMouseDown(event) {
- dialogInitialX = dialogBox.offsetLeft;
- dialogInitialY = dialogBox.offsetTop;
- initialX = event.clientX;
- initialY = event.clientY;
-
- // 鼠标移动事件
- document.addEventListener('mousemove', onMouseMove);
-
- // 鼠标松开事件
- document.addEventListener('mouseup', onMouseUp);
- }
-
- // 触摸开始事件处理函数
- function onTouchStart(event) {
- dialogInitialX = dialogBox.offsetLeft;
- dialogInitialY = dialogBox.offsetTop;
- initialX = event.touches[0].clientX;
- initialY = event.touches[0].clientY;
-
- // 触摸移动事件
- document.addEventListener('touchmove', onTouchMove);
-
- // 触摸结束事件
- document.addEventListener('touchend', onTouchEnd);
- }
-
- // 鼠标移动事件处理函数
- function onMouseMove(event) {
- const deltaX = event.clientX - initialX;
- const deltaY = event.clientY - initialY;
-
- // 更新对话框位置
- dialogBox.style.left = dialogInitialX + deltaX + 'px';
- dialogBox.style.top = dialogInitialY + deltaY + 'px';
- }
-
- // 触摸移动事件处理函数
- function onTouchMove(event) {
- const deltaX = event.touches[0].clientX - initialX;
- const deltaY = event.touches[0].clientY - initialY;
-
- // 更新对话框位置
- dialogBox.style.left = dialogInitialX + deltaX + 'px';
- dialogBox.style.top = dialogInitialY + deltaY + 'px';
- }
-
- // 鼠标松开事件处理函数
- function onMouseUp() {
- // 移除鼠标移动事件监听器
- document.removeEventListener('mousemove', onMouseMove);
- document.removeEventListener('mouseup', onMouseUp);
- }
-
- // 触摸结束事件处理函数
- function onTouchEnd() {
- // 移除触摸移动事件监听器
- document.removeEventListener('touchmove', onTouchMove);
- document.removeEventListener('touchend', onTouchEnd);
- }
- // 设置链接文本不可点击
- document.getElementById('linkTextSpan').style.pointerEvents = 'none';
- // 绑定关闭按钮点击事件
- document.getElementById('closeBtn').addEventListener('click', function() {
- document.body.removeChild(dialogBox);
- });
-
- // 点击复制链接按钮复制文本到剪贴板
- document.getElementById('copyLinkBtn').addEventListener('click', function() {
- const linkText = document.getElementById('linkTextSpan').innerText;
-
- // 创建一个临时的textarea元素来容纳文本
- const tempTextarea = document.createElement('textarea');
- tempTextarea.value = linkText;
- document.body.appendChild(tempTextarea);
-
- // 选中文本并执行复制操作
- tempTextarea.select();
- document.execCommand('copy');
-
- // 移除临时textarea元素
- document.body.removeChild(tempTextarea);
- document.body.removeChild(dialogBox);
- // 提示用户文本已复制到剪贴板
- Toast('文本已复制到剪贴板!', 1000);
- });
-
- // 绑定论坛搜索按钮点击事件
- document.getElementById('forumSearchBtn').addEventListener('click', function() {
- const newUrl = `https://${location.host}/zh-CN/scripts/by-site/${linkText}`;
- navigateTo(newUrl, useWindowOpen);
- document.body.removeChild(dialogBox);
- });
-
- // 绑定打开网址按钮点击事件
- document.getElementById('openUrlBtn').addEventListener('click', function() {
- const originalUrl = `https://${linkText}`;
- navigateTo(originalUrl, useWindowOpen);
- document.body.removeChild(dialogBox);
- });
- } else if (linkBehavior === 1) {
- const originalUrl = `https://${linkText}`;
- navigateTo(originalUrl, useWindowOpen);
- } else if (linkBehavior === 2) {
- const newUrl = `https://${location.host}/zh-CN/scripts/by-site/${linkText}`;
- navigateTo(newUrl, useWindowOpen);
- }
- }
-
-
-
- const appliesToSection = document.querySelector('dt.script-show-applies-to');
- if (appliesToSection) {
- const changeConfigText = document.createElement('span');
- changeConfigText.textContent = '[适用于] ';
- changeConfigText.style.fontWeight= 'bold'; // Bold
-
- // 创建复选框
- const checkboxLabel = document.createElement('label');
- checkboxLabel.textContent = '新窗口打开';
- checkboxLabel.style.marginLeft = '10px';
-
- const checkboxInput = document.createElement('input');
- checkboxInput.type = 'checkbox';
- checkboxInput.checked = useWindowOpen;
- checkboxInput.style.marginRight = '5px';
-
- checkboxInput.addEventListener('change', function() {
- useWindowOpen = checkboxInput.checked;
- GM_setValue('useWindowOpen', useWindowOpen);
- });
-
- checkboxLabel.appendChild(checkboxInput);
-
- // 添加选择框和复选框
- const selectList = document.createElement('select');
- selectList.style.width = '7em'; // 设置宽度为四个中文字符的宽度
- const options = [
- { value: 0, text: '弹出提示' },
- { value: 1, text: '打开网址' },
- { value: 2, text: '论坛搜索' }
- ];
- options.forEach(option => {
- const optionElement = document.createElement('option');
- optionElement.textContent = option.text;
- optionElement.value = option.value;
- if (linkBehavior === option.value) {
- optionElement.selected = true;
- }
- selectList.appendChild(optionElement);
- });
- selectList.addEventListener('change', function() {
- linkBehavior = parseInt(selectList.value);
- GM_setValue('linkBehavior', linkBehavior);
- Toast(`点击"适用于"网址已设置为: ${options.find(option => option.value === linkBehavior).text}`, 1000);
- });
-
- appliesToSection.parentElement.appendChild(changeConfigText);
- appliesToSection.parentElement.appendChild(selectList);
- appliesToSection.parentElement.appendChild(checkboxLabel);
- }
- })();