我的插件
目前為
// ==UserScript==
// @name 图寻我的插件
// @namespace http://tampermonkey.net/
// @version 2.66
// @description 我的插件
// @author yukejun
// @match https://tuxun.fun/*
// @run-at document-end
// @grant none
// ==/UserScript==
// ==UserScript==
// @name 图寻我的插件
// @namespace http://tampermonkey.net/
// @version 2.65
// @description 我的插件
// @author yukejun
// @match https://tuxun.fun/*
// @run-at document-end
// @grant none
// ==/UserScript==
// 自执行函数,为了限制变量和函数的作用范围,防止全局污染
(function() {
'use strict'; // 使用严格模式
// 功能1:按空格键触发另一个选择器
document.addEventListener('keydown', function(e) {
// 输出到控制台,确保事件被触发
console.log('Key pressed:', e.keyCode);
if (e.keyCode === 32) {
const buttons = document.querySelectorAll('button');
buttons.forEach(function(button) {
if (button.textContent.includes('开始(经典5轮)') || button.textContent.includes('再来一局') || button.textContent.includes('保留')) {
button.click();
}
});
}
});
// 功能2:隐藏包含 "比赛已经开始或者这一轮游戏还未结束" 文本的提示
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === Node.ELEMENT_NODE && node.matches('.van-toast.van-toast--middle.van-toast--text')) {
const innerDiv = node.querySelector('.van-toast__text');
if (innerDiv && (innerDiv.textContent.trim() === "比赛已经开始或者这一轮游戏还未结束" || innerDiv.textContent.trim() === "你已经被淘汰")) {
node.style.display = 'none';
}
}
});
}
});
});
let currentRound = 1; // 当前轮次
// 根据原始URL获取修改后的URL
function getModifiedURL(originalURL) {
let match = originalURL.match(/(gameId|infinityId|challengeId|streakId)=([\w-]+)/); // 使用正则表达式匹配URL中的特定参数
if (match && match[2]) { // 如果匹配成功
return `https://tuxun.fun/replay_pano?gameId=${match[2]}&round=${currentRound}`; // 返回修改后的URL
}
return originalURL; // 否则返回原始URL
}
// 使用事件委托方法获取匹配的图片数量
function getMatchingImagesCount() {
return Array.from(document.querySelectorAll('img')) // 从文档中选择所有的img元素
.filter(img => /https:\/\/i\.chao-fan\.com\/biz\/\d+_[\w-]+_0\.png/.test(img.src)).length; // 过滤出匹配特定模式的img元素,并返回其数量
}
// 处理点击事件的函数
function handleClick(event) {
const imgElement = event.target.closest('img'); // 获取被点击的img元素
if (imgElement && /https:\/\/i\.chao-fan\.com\/biz\/\d+_[\w-]+_0\.png/.test(imgElement.src)) { // 如果img元素匹配特定模式
let modifiedURL = getModifiedURL(window.location.href); // 获取修改后的URL
window.open(modifiedURL, '_blank'); // 在新窗口中打开该URL
}
// 检查匹配的img元素的数量
if (getMatchingImagesCount() > 1) {
document.removeEventListener("click", handleClick); // 如果匹配的img元素数量大于1,移除点击事件监听器
}
}
document.addEventListener("click", handleClick); // 在文档上添加点击事件监听器
let roundDiv, roundObserver; // 用于监控轮次变化的变量
// 监控轮次变化的函数
function monitorRoundChange() {
// 尝试找到与文本内容匹配的 div 元素
let roundDivs = Array.from(document.querySelectorAll("div[data-v-26f5391c]"));
let targetDiv = roundDivs.find(div => /第 \d+ 轮/.test(div.textContent));
// 如果找到了目标元素
if (targetDiv) {
console.log('Target element found:', targetDiv); // 调试日志1
roundObserver = new MutationObserver(function(mutations) { // 创建一个新的MutationObserver实例来监控目标元素的变化
mutations.forEach(function(mutation) {
if(mutation.type === "childList") { // 如果观察到的变化是子元素列表的变化
let match = targetDiv.textContent.match(/第 (\d+) 轮/); // 使用正则表达式匹配轮次
if(match && match[1]) { // 如果匹配成功
currentRound = parseInt(match[1], 10); // 更新当前轮次
console.log('Updated currentRound:', currentRound); // 调试日志2
} else {
console.log('Pattern not matched in:', targetDiv.textContent); // 调试日志3
}
}
});
});
roundObserver.observe(targetDiv, { childList: true, subtree: true }); // 开始观察目标元素的子元素列表变化和子树变化
} else {
console.log('Target element not found'); // 调试日志4
}
}
setTimeout(monitorRoundChange, 2000); // 使用 setTimeout 延迟 3 秒后再执行 monitorRoundChange 函数
monitorRoundChange(); // 在适当的位置调用 monitorRoundChange 函数
// 拖拽功能的函数
function setInitialPositionFromStorage(element, selector) { // 从localStorage中获取元素的初始位置并设置
const storedPos = localStorage.getItem(selector);
if (storedPos) {
const { left, top } = JSON.parse(storedPos);
element.style.left = left;
element.style.top = top;
}
}
function makeDraggable(element, selector) { // 使元素可拖拽
let isDragging = false;
let startX, startY, initialX, initialY;
if (!element) return;
if (window.getComputedStyle(element).position === 'static') {
element.style.position = 'relative'; // 如果元素的位置是静态的,则设置为相对位置
}
setInitialPositionFromStorage(element, selector); // 设置元素的初始位置
element.addEventListener('mousedown', function(event) { // 添加鼠标按下事件监听器
isDragging = true;
startX = event.clientX;
startY = event.clientY;
initialX = parseInt(element.style.left || 0);
initialY = parseInt(element.style.top || 0);
const map = window.map || document.querySelector('#map').__gm;
if (map && map.setOptions) {
map.setOptions({draggable: false}); // 当元素被拖拽时,禁止地图拖拽
}
event.stopPropagation();
event.preventDefault();
});
document.addEventListener('mousemove', function(event) { // 添加鼠标移动事件监听器
if (!isDragging) return;
let dx = event.clientX - startX;
let dy = event.clientY - startY;
element.style.left = (initialX + dx) + 'px';
element.style.top = (initialY + dy) + 'px';
event.stopPropagation();
event.preventDefault();
});
document.addEventListener('mouseup', function(event) { // 添加鼠标松开事件监听器
if (isDragging) {
const map = window.map || document.querySelector('#map').__gm;
if (map && map.setOptions) {
map.setOptions({draggable: true}); // 当元素停止拖拽时,允许地图拖拽
}
localStorage.setItem(selector, JSON.stringify({
left: element.style.left,
top: element.style.top
})); // 将元素的位置存储到localStorage中
}
isDragging = false;
event.stopPropagation();
});
}
document.addEventListener('dblclick', function(event) { // 添加双击事件监听器
if (event.target.closest('#tuxun')) {
event.preventDefault();
event.stopPropagation();
}
}, true);
const selectors = [
'#viewer > div > div:nth-child(14) > div.gmnoprint.gm-bundled-control.gm-bundled-control-on-bottom > div'
];
const dragObserver = new MutationObserver(mutations => { // 创建一个新的MutationObserver实例来监控元素的变化
for (const mutation of mutations) {
if (mutation.addedNodes.length) {
selectors.forEach(selector => {
const element = document.querySelector(selector);
if (element) {
makeDraggable(element, selector); // 如果元素存在,则使其可拖拽
}
});
}
}
});
dragObserver.observe(document.body, {
childList: true,
subtree: true
}); // 开始观察文档体的子元素列表变化和子树变化
})();