JAV本地视频匹配

匹配本地视频文件名并标记 JavBus 和 JavLibrary 网站上的电影。

// ==UserScript==
// @name         JAV本地视频匹配
// @version      1.5
// @author       WANG & Squirtle
// @description  匹配本地视频文件名并标记 JavBus 和 JavLibrary 网站上的电影。
// @license      MIT
// @icon         https://www.javbus.com/favicon.ico
// @include      /^https:\/\/(\w*\.)?JavBus(\d)*\.com.*$/
// @match        *://*.javbus.com/*
// @match        *://*.javlibrary.com/*
// @namespace https://greasyfork.org/users/439255
// ==/UserScript==

(function() {
    'use strict';

    let button = document.createElement('button');
    button.innerHTML = '仓';
    button.style.position = 'fixed';
    button.style.bottom = '1px';
    button.style.left = '1px';
    button.style.zIndex = '1000';

    button.addEventListener('click', function() {
        btnClick();
    });

    document.body.appendChild(button);

    async function btnClick() {
        const fileInput = document.createElement('input');
        fileInput.type = 'file';
        fileInput.accept = '.txt';
        fileInput.onchange = async () => {
            const file = fileInput.files[0];
            if (file) {
                // 清空 localStorage 中的 movieCodes
                localStorage.removeItem("movieCodes");
                console.log("旧电影代码已清除");

                const movieCodes = await readFile(file);
                localStorage.setItem("movieCodes", JSON.stringify(movieCodes));
                console.log("新电影代码设置成功");
                start();
            }
        };
        fileInput.click();
    }

    async function readFile(file) {
        return new Promise((resolve, reject) => {
            const reader = new FileReader();
            reader.onload = (event) => {
                const content = event.target.result;
                let movieCodes = [];
                if (file.name.endsWith('.txt')) {
                    movieCodes = content.split('\n')
                        .map(line => line.trim())
                        .filter(line => line)
                        .map(extractMovieCode)
                        .filter(code => code);
                }
                resolve(movieCodes);
            };
            reader.onerror = (error) => reject(error);
            reader.readAsText(file);
        });
    }

    function extractMovieCode(filename) {
        let code = filename.replace(/^hhd800\.com@/, '');

        // 优先使用连字符分割
        if (code.indexOf('-') !== -1) {
            code = code.split('-')[0]+'-'+code.split('-')[1];
            code = code.replace(/\..*$/i, ''); // 去除后缀
            code = code.replace(/-C$|-4K$/gi, '');//去除-C -4K
            
        } else {
          // 尝试匹配无连字符的情况,例如IPX123或IPZZ123
          let match = code.match(/([a-zA-Z]{3,4}\d{3})/i);
          if (match) {
            code = match[1].toUpperCase();
             // 将番号和数字拆分,插入连字符
            code = code.replace(/([a-zA-Z]+)(\d+)/, '$1-$2');
          }else {
             //如果没识别出来,为了防止误伤,此处直接返回空值
            return '';
          }
        }
        return code;
    }

    async function start() {
        const movieCodesStr = localStorage.getItem('movieCodes');
        if (!movieCodesStr) {
            return;
        }
        const movieCodes = JSON.parse(movieCodesStr);

        if (location.href.includes('javbus.com')) {
            $('#waterfall .movie-box').each((index, ele) => {
                const movieBox = $(ele);
                const url = movieBox.attr('href');
                const code = url.substring(url.lastIndexOf('/') + 1);
                if (movieCodes.includes(code)) {
                    movieBox.css('background-color', 'red');
                }
            });

            const url = location.href;
            const code = url.substring(url.lastIndexOf('/') + 1);
            if (movieCodes.includes(code)) {
                $('div.row.movie').css('background-color','gold');
            }
        } else if (location.href.includes('javlibrary.com')) {
             $('.video').each((index, ele) => {
                const video = $(ele);
                const code = video.find('.id').text();
                if (movieCodes.includes(code)) {
                    video.css('background-color', 'red');
                }
            });
        }
    }

    start();
})();