Greasy Fork is available in English.
检测地址栏中的RJ号,并在指定的div元素中显示跳转按钮,同时根据资源存在情况改变按钮颜色和文本
当前为
// ==UserScript==
// @name ASMRONE&Kikoeru相互跳转
// @namespace http://tampermonkey.net/
// @version 0.2
// @description 检测地址栏中的RJ号,并在指定的div元素中显示跳转按钮,同时根据资源存在情况改变按钮颜色和文本
// @author 你的名字
// @match *://asmr-300.com/*
// @match *://asmr-200.com/*
// @match *://asmr-100.com/*
// @match *://asmr.one/*
// @match *://localhost:8889/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function () {
'use strict';
// 添加样式定义
const style = `
.rdl-button {
display: inline-block;
padding: 5px 10px;
text-decoration: none;
}
.rdl-button_green {
background-color: #67c23a;
color: white;
}
.rdl-button_red {
background-color: #f56c6c;
color: white;
}
.rdl-button_blue {
background-color: #409eff;
color: white;
}
`;
const styleSheet = document.createElement("style");
styleSheet.type = "text/css";
styleSheet.innerText = style;
document.head.appendChild(styleSheet);
// 检测地址栏中的RJ号并创建按钮
const checkUrlAndCreateButton = async () => {
console.log('checkUrlAndCreateButton called');
const url = window.location.href;
console.log('Current URL:', url);
// 判断当前页面是否是 localhost:8889
if (url.includes('localhost:8889')) {
createAsmrOneButton(url);
} else {
createKikoeruButton(url);
}
};
// 创建跳转到 Kikoeru 的按钮
const createKikoeruButton = async (url) => {
const rjRegex = /RJ(\d{6,8})/i;
const match = url.match(rjRegex);
if (match) {
let rjNumber = match[1];
if (rjNumber.length === 6) {
rjNumber = rjNumber; // 6位数直接去掉RJ
} else if (rjNumber.length === 8) {
rjNumber = rjNumber.slice(1); // 8位数保留后7位
}
const jumpUrl = `http://localhost:8889/work/${rjNumber}`;
const existingButton = document.getElementById('rj-jump-button');
if (existingButton) {
existingButton.remove();
}
const targetRow = document.querySelector('.row.items-center.q-gutter-xs');
if (targetRow) {
console.log('Target row found');
const button = document.createElement('a');
button.id = 'rj-jump-button';
button.textContent = '正在检查...';
button.style.marginLeft = '10px';
button.style.textDecoration = 'none';
button.style.cursor = 'pointer';
button.target = "_blank"; // 在新标签页中打开
// 设置默认样式为红色
button.className = 'rdl-button rdl-button_red';
// 添加到目标行的最后
targetRow.appendChild(button);
// 检查资源是否存在
await checkResource(rjNumber, button);
} else {
console.log('Target row not found');
}
} else {
console.log('No RJ number in URL');
const existingButton = document.getElementById('rj-jump-button');
if (existingButton) {
existingButton.remove();
}
}
};
// 创建跳转到 ASMRONE 的按钮
const createAsmrOneButton = (url) => {
const rjRegex = /work\/(\d{6,7})/i; // 匹配 6 位或 7 位的 RJ 号
const match = url.match(rjRegex);
if (match) {
let rjNumber = match[1];
if (rjNumber.length === 6) {
rjNumber = `RJ${rjNumber}`; // 6 位数直接使用
} else if (rjNumber.length === 7) {
rjNumber = `RJ0${rjNumber}`; // 7 位数补 0 为 8 位
}
const jumpUrl = `https://asmr-200.com/work/${rjNumber}`;
const existingButton = document.getElementById('asmr-one-jump-button');
if (existingButton) {
existingButton.remove();
}
const targetRow = document.querySelector('.row.items-center.q-gutter-xs');
if (targetRow) {
console.log('Target row found');
const button = document.createElement('a');
button.id = 'asmr-one-jump-button'; // 设置按钮的 ID,用于唯一标识该按钮
button.textContent = 'ASMRONE'; // 设置按钮上显示的文本内容
button.style.marginLeft = '10px'; // 设置按钮左边距为 10px,使其与左侧元素保持一定距离
button.style.textDecoration = 'none'; // 移除按钮文本的下划线(通常用于链接)
button.style.cursor = 'pointer'; // 设置鼠标悬停时的光标样式为指针(表示可点击)
button.style.backgroundColor = '#31ccec'; // 设置背景颜色为绿色
button.style.color = 'white'; // 设置文字颜色为白色
button.style.border = 'none'; // 移除边框
button.style.padding = '6px 12px'; // 设置内边距
button.style.borderRadius = '4px'; // 设置圆角
button.style.fontSize = '13px'; // 设置字体大小
button.style.width = 'auto'; // 宽度自适应内容
button.style.height = 'auto'; // 高度自适应内容
button.target = "_blank"; // 在新标签页中打开
button.href = jumpUrl; // 设置按钮点击后跳转的链接地址
// 设置样式为蓝色
button.className = 'rdl-button rdl-button_blue';
// 添加到目标行的最后
targetRow.appendChild(button);
} else {
console.log('Target row not found');
}
} else {
console.log('No RJ number in URL');
const existingButton = document.getElementById('asmr-one-jump-button');
if (existingButton) {
existingButton.remove();
}
}
};
// 检查资源是否存在
async function checkResource(rj, button) {
const url = `http://localhost:8889/api/search?keyword=${rj}`;
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: function (response) {
try {
const works = JSON.parse(response.responseText).works;
if (works.length > 0) {
button.textContent = '跳转kikoeru';
button.href = `http://localhost:8889/work/${rj}`;
button.className = "rdl-button rdl-button_green"; // 资源存在,变绿
} else {
button.textContent = '资源不存在';
button.href = '#'; // 禁用链接
button.className = "rdl-button rdl-button_red"; // 资源不存在,保持红
}
} catch (error) {
button.textContent = '请求失败';
button.href = '#'; // 禁用链接
button.className = "rdl-button rdl-button_red"; // 请求失败,保持红
}
},
onerror: function () {
button.textContent = '请求失败';
button.href = '#'; // 禁用链接
button.className = "rdl-button rdl-button_red"; // 请求失败,保持红
}
});
}
// 初始化时检测一次URL
checkUrlAndCreateButton();
let lastUrl = window.location.href;
// 监听 AJAX 请求完成事件,确保在内容加载完成后再执行按钮创建逻辑。
const originalOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function () {
this.addEventListener('load', () => {
if (this.responseURL.includes('/work/')) {
checkUrlAndCreateButton();
}
});
originalOpen.apply(this, arguments);
};
})();