Greasy Fork is available in English.
检测地址栏中的RJ号,并在指定的div元素中显示跳转Kikoeru按钮,同时根据资源存在情况改变按钮颜色和文本
当前为
// ==UserScript==
// @name ASMRONE跳转Kikoeru
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 检测地址栏中的RJ号,并在指定的div元素中显示跳转Kikoeru按钮,同时根据资源存在情况改变按钮颜色和文本
// @author 你的名字
// @match *://asmr-300.com/*
// @match *://asmr-200.com/*
// @match *://asmr-100.com/*
// @match *://asmr.one/*
// @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;
}
`;
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);
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.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();
}
}
};
// 检查资源是否存在
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);
};
// 监听所有链接的点击事件
document.addEventListener('click', (event) => {
const target = event.target.closest('a');
if (target) {
// 延迟一小段时间(例如100ms),等待URL更新
setTimeout(() => {
if (window.location.href !== lastUrl) {
lastUrl = window.location.href;
checkUrlAndCreateButton();
}
}, 100); // 延迟时间可以根据实际情况调整
}
});
})();