Files
jav-play/entrypoints/content/index.ts

115 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 从两个组件中导入方法
import { addOrUpdatePlayerButtons, hidePlayerButtons } from '../../components/PlayerButtons';
import { addOrUpdateNavigationButtons, hideNavigationButtons } from '../../components/NavigationButtons';
const STORAGE_KEY = 'feature_enabled';
export default defineContentScript({
matches: ['*://*.javdb.com/v/*'],
async main() {
const isEnabled = await storage.getItem(`sync:${STORAGE_KEY}`) ?? true;
if (!isEnabled) {
console.log('❌ [JavDB Helper] 功能已禁用。');
hidePlayerButtons();
hideNavigationButtons(); // 同时隐藏导航按钮
return;
}
console.log('🚀 [JavDB Helper] 功能已启用,正在运行脚本...');
const processPage = async () => {
if (window.location.pathname.startsWith('/v/')) {
const videoNumber = getVideoNumber();
if (videoNumber) {
// 只要有番号,就显示导航按钮
addOrUpdateNavigationButtons(videoNumber);
// 异步获取 UUID 来显示播放器按钮
const missavUUID = await getMissavUUID(videoNumber);
if (missavUUID) {
addOrUpdatePlayerButtons(missavUUID);
} else {
// 如果没有 UUID则只隐藏播放器按钮
hidePlayerButtons();
}
}
} else {
// 如果不在视频详情页,隐藏所有按钮
hidePlayerButtons();
hideNavigationButtons();
}
};
// 监听 URL 路径变化
let lastPathname = window.location.pathname;
new MutationObserver(() => {
const currentPathname = window.location.pathname;
if (currentPathname !== lastPathname) {
lastPathname = currentPathname;
processPage();
}
}).observe(document.body, { childList: true, subtree: true });
// 页面加载时执行一次
processPage();
}
});
// 获取目标视频番号
function getVideoNumber(): string {
const targetElement = document.querySelector('a.button.is-white.copy-to-clipboard');
if (!targetElement) {
console.log('未找到目标元素');
return '';
}
const targetNumber = targetElement.getAttribute('data-clipboard-text');
if (!targetNumber) {
console.log('无目标番号');
return '';
}
console.log('目标番号', targetNumber);
return targetNumber;
}
// 获取 missav UUID
async function getMissavUUID(videoNumber: string): Promise<string> {
const lowerTargetNumber = videoNumber.toLowerCase();
const targetUrl = `https://missav.ws/dm1/en/${lowerTargetNumber}`;
try {
const response = await chrome.runtime.sendMessage({
type: 'fetchMissav',
url: targetUrl
});
if (!response.success) {
throw new Error(response.error);
}
const parser = new DOMParser();
const doc = parser.parseFromString(response.html, 'text/html');
const scripts = doc.getElementsByTagName('script');
for (const script of scripts) {
const content = script.textContent || '';
if (content.includes('thumbnail')) {
const urlsMatch = content.match(/urls:\s*\[(.*?)\]/s);
if (urlsMatch) {
const firstUrl = urlsMatch[1].split(',')[0].trim().replace(/"/g, '').replace(/\\/g, '');
const uuidMatch = firstUrl.match(/\/([0-9a-f-]+)\/seek\//i);
if (uuidMatch) {
console.log('uuidMatch', uuidMatch[1]);
return uuidMatch[1];
}
}
}
}
console.warn('未找到 uuid');
return '';
} catch (error) {
console.error('获取或解析文档时出错:', error);
return '';
}
}