蜜柑计划(Mikan Project)复制全部磁链

复制某部番的某个字幕组的全部磁链

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         蜜柑计划(Mikan Project)复制全部磁链
// @namespace
// @version      0.2.0-b2
// @description  复制某部番的某个字幕组的全部磁链
// @author       cookedfish
// @match        http*://mikanime.tv/*
// @match        http*://mikanani.me/*
// @grant        GM_setClipboard
// @grant        GM_setValue
// @grant        GM_getValue
// @license      MIT
// @namespace
// @namespace 
// ==/UserScript==

///// SETTINGS /////

var SPREAD = true;
//自动展开开关 true/false

var SEARCH = true;
//搜索开关 true/false

var MODE = true;
//模式开关 true/false

var SAVE = true;
//保存设置开关 true/false

var LOAD = true;
//加载设置开关 true/false

var CLEAR = true;
//清空勾选框开关 true/false

var CATALOGUE = true;
//字幕组列表复制功能开关 true/false

/////SETTINGS END/////

//部分功能互斥。

if(SEARCH === false){
    MODE = false;
    SAVE = false;
    LOAD = false;
}

function saveSubgroupSettings(subgroupName, keyword, mode) {//保存设置
    if(!SAVE) return 'close';
    const settings = GM_getValue('mikan_subgroup_settings', {});
    settings[subgroupName] = {
        keyword: keyword,
        mode: mode,
        lastUsed: Date.now()
    };
    GM_setValue('mikan_subgroup_settings', settings);
}

function getSubgroupSettings(subgroupName) {//获取单字幕组设置
    const settings = GM_getValue('mikan_subgroup_settings', {});
    return settings[subgroupName] || null;
}

function getAllSubgroupSettings() {//获取全部设置
    return GM_getValue('mikan_subgroup_settings', {});
}

function print_messageBar(max){//消息框
    var messageBar = document.createElement('div');
    messageBar.textContent = max;
    messageBar.style.position = 'fixed';
    messageBar.style.bottom = '0';
    messageBar.style.left = '0';
    messageBar.style.width = '100%';
    messageBar.style.backgroundColor = 'green';
    messageBar.style.color = 'white';
    messageBar.style.textAlign = 'center';
    messageBar.style.padding = '10px 0';
    document.body.appendChild(messageBar);
    setTimeout(function() {
        messageBar.parentNode.removeChild(messageBar);
    }, 1000);
}

function search(text,Value){//单词模式
    var startIndex = 0;
    while (startIndex < text.length) {//匹配关键字词
        var index = text.indexOf(Value, startIndex);
        if (index !== -1) {
            console.log('%c' + 'true','color: green',text,Value);
            return true;
        } else {
            break;
        }
    }
    console.log('%c' + 'false', 'color: red', text, Value);
    return false;
}

function multiple_match(texts, Value) {//多词模式
    var searchTerms = Value.split(' ');
    for (var i = 0; i < searchTerms.length; i++) {
        console.log(searchTerms[i]);
        if (!search(texts, searchTerms[i])) {return false;}
    }
    return true;
}

function anti_election(texts, Value){//排除模式
    var searchTerms = Value.split(' ');
    for (var i = 0; i < searchTerms.length; i++) {
        console.log(searchTerms[i]);
        if (search(texts, searchTerms[i])) {return false;}
    }
    return true;
}

function get_xunlei(){//复制按钮
    var num = parseInt($(this).closest('div').attr('id'));//字幕组id
    console.log(num);
    var urls = [];
    var abc = undefined;
    var checkbox = [];
    var box = false;
    var mode = $(this).nextAll("a:first").next('a').text();//模式
    var keyword = $(this).next('input').val();//关键词
    var subgroupName = $(this).closest('div').children('a:first').text();//字幕组名
    if(!MODE) mode = '  模式切换:多词模式';
    console.log(mode,keyword,subgroupName);
    $('.table').find('input[aria-label="选择此行"]').each(function () {
        if($(this).attr('class') !== "js-episode-select") return true;
        if (parseInt($(this).closest('table').closest('div').prev('div').attr('id')) !== num && $(this).closest('table').closest('div').prev('div').attr('id') !== abc) return true;
        checkbox.push($(this).attr('effect'));
    });
    console.log(checkbox);
    for(var i = 0;i < checkbox.length;i++){
        if(checkbox[i] === 'checked'){//预检查勾选框是否有勾选
            box=true;
            break;
            console.log(i);
        }
        box=false;
    }
    console.log(box);
    if(!box){
        $('.table').find('a').each(function () {//按条件排除
            if($(this).attr('class') !== 'js-magnet magnet-link') return true;
            if (parseInt($(this).closest('table').closest('div').prev('div').attr('id')) !== num && $(this).closest('table').closest('div').prev('div').attr('id') !== abc) return true;
            if(keyword && mode === '  模式切换:单词模式'&&search($(this).prev('a').text(),keyword)===false) return true;
            if(keyword && mode === '  模式切换:排除模式'&&anti_election($(this).prev('a').text(),keyword)===false) return true;
            if(keyword && mode === '  模式切换:多词模式'&&multiple_match($(this).prev('a').text(),keyword)===false) return true;
            if(keyword && mode === '  模式切换:打开模式'&&multiple_match($(this).prev('a').text(),keyword)===false) return true;
            urls.push($(this).attr('data-clipboard-text'));
            $(this).closest('td').prev('td').children().first().click();
        });
    }
    else{
        $('.table').find('input[aria-label="选择此行"]').each(function () {
            if($(this).attr('class') !== "js-episode-select") return true;
            if (parseInt($(this).closest('table').closest('div').prev('div').attr('id')) !== num && $(this).closest('table').closest('div').prev('div').attr('id') !== abc) return true;
            if($(this).attr('effect') === 'uncheck') return true;
            urls.push($(this).attr('data-magnet'));
        });
    };

    if (subgroupName&&SAVE) {// 保存设置到本地存储
        var cleanMode = mode.replace('  模式切换:', '');
        saveSubgroupSettings(subgroupName, keyword, cleanMode);
        console.log('保存设置:', subgroupName, keyword, cleanMode);
    }

    if(mode === '  模式切换:打开模式'){//打开模式
        for(i = 0;i < urls.length;i++)
        {
            var pages = window.open(urls[i], '_blank');
            pages.close();
            if(i % 10 === 9) alert(i-8 + '-' + (i+1) + '条,共' + urls.length + '条');
        }
    }
    print_messageBar('复制了'+urls.length+'个链接');
    GM_setClipboard(urls.join('\n'));//更改剪贴板
};

function click(){//自动展开
    if(!SPREAD) return 'close';
    document.querySelectorAll('a.js-expand-episode').forEach(function(element) {
        element.click();
    });
}

function Mode(){//模式切换
    if(!MODE) return 'close';
    var currentMode = $(this).text().trim();
    switch (currentMode) {
        case '模式切换:多词模式':
            $(this).text('  模式切换:排除模式');
            break;
        case '模式切换:排除模式':
            $(this).text('  模式切换:打开模式');
            break;
        case '模式切换:单词模式':
            $(this).text('  模式切换:多词模式');
            break;
        case '模式切换:打开模式':
            $(this).text('  模式切换:单词模式');
            break;
        default:
            break;
    };
}

function del(){//清空勾选框
    if(!CLEAR) return 'close';
    var num = parseInt($(this).closest('div').attr('id'));
    $('.table').find('input').each(function () {
        if($(this).attr('aria-label') !== '选择此行') return true;
        if (parseInt($(this).closest('table').closest('div').prev('div').attr('id')) !== num && $(this).closest('table').closest('div').prev('div').attr('id') !== 'undefined') return true;
        if($(this).attr('effect') === 'checked') $(this).click();
        console.log($(this).attr('effect'));
    });
}

function change(){//改变勾选框
    if($(this).attr('effect') === 'uncheck') $(this).attr('effect', 'checked');
    else $(this).attr('effect', 'uncheck');
}

function CtrlA(){//全选勾选框
    var num = parseInt($(this).closest('div').closest('div').prev('div').attr('id'));
    console.log(num);
    var abc = undefined;
    var checkbox = [];
    var checkedbox=0;
    var box = false;
    $('.table').find('input[aria-label="选择此行"]').each(function () {
        if($(this).attr('class') !== 'js-episode-select') return true;
        if (parseInt($(this).closest('table').closest('div').prev('div').attr('id')) !== num && $(this).closest('table').closest('div').prev('div').attr('id') !== abc) return true;
        checkbox.push($(this).attr('effect'));
    });
    console.log(checkbox);
    for(var i = 0;i < checkbox.length;i++){
        if(checkbox[i] === 'checked'){//预检查勾选框是否有勾选
            checkedbox++;
        }
        if(checkedbox <= checkbox.length) box=false;
        if(checkedbox === checkbox.length) box=true;
    }
    console.log(box);
    if(box){
        $('.table').find('input').each(function () {
            if($(this).attr('class') !== 'js-episode-select') return true;
            if (parseInt($(this).closest('table').closest('div').prev('div').attr('id')) !== num && $(this).closest('table').closest('div').prev('div').attr('id') !== abc) return true;
            $(this).attr('effect','uncheck');
        })
    }
    else{
        $('.table').find('input').each(function () {
            if($(this).attr('class') !== 'js-episode-select') return true;
            if (parseInt($(this).closest('table').closest('div').prev('div').attr('id')) !== num && $(this).closest('table').closest('div').prev('div').attr('id') !== abc) return true;
            $(this).attr('effect','checked');
        })
    }
}

function title(){//字幕组名
    if(!CATALOGUE) return 'close';
    var title=$(this).attr('data-anchor');
    title = title.replace(/#/g, '');
    console.log(title);
    var urls = [];
    var abc = undefined;
    $('.table').find('a').each(function () {//按条件排除
        if($(this).attr('class') !== "js-magnet magnet-link") return true;
        if ($(this).closest('table').closest('div').prev('div').attr('id') !== title && $(this).closest('table').closest('div').prev('div').attr('id') !== abc) return true;
        urls.push($(this).attr('data-clipboard-text'));
    });
    print_messageBar('复制了'+urls.length+'个链接');
    GM_setClipboard(urls.join('\n'));//更改剪贴板
}

function autoFillSubgroupSettings() {// 自动填充字幕组设置
    if(!LOAD) return 'close';
    $('.subgroup-text i').closest('a').prev('a').each(function() {
        var subgroupName = $(this).text();
        var settings = getSubgroupSettings(subgroupName);
        if (settings) {
            console.log('找到字幕组设置:', subgroupName, settings);
            // 找到对应的输入框和模式按钮
            var $container = $(this).closest('div');
            var $input = $container.find('input[type="text"]');
            var $modeButton = $container.find('a[ref="mode"]');
            if ($input.length && $modeButton.length) {
                if(SEARCH)$input.val(settings.keyword);
                if(MODE)$modeButton.text('  模式切换:' + settings.mode);
                console.log('自动填充:', subgroupName, settings.keyword, settings.mode);
            }
        }
    });
}

(function (){
    const currentUrl = window.location.href.toLowerCase();
    const currentPath = window.location.pathname.toLowerCase();
    const pattern = /\/home\/bangumi\/\d+/;// 匹配 /home/bangumi/ 后跟数字的路径(不区分大小写)
    if (pattern.test(currentPath)) {// 提取数字部分
        const match = currentPath.match(/\/home\/bangumi\/(\d+)/);
        const number = match ? match[1] : null;
        console.log('匹配到路径,数字ID:', number);
        runScript();
    }
    function runScript() {// 脚本主逻辑
        console.log('在目标页面运行脚本');
        $(document).on('click', 'a[ref="thunder"]', get_xunlei);//绑定事件
        if(MODE) $(document).on('click', 'a[ref="mode"]', Mode);
        if(CATALOGUE) $(document).on('click', 'a[data-anchor^="#"]',title);
        if(CLEAR) $(document).on('click', 'a[ref="del"]', del);
        $(document).on('click', 'input[aria-label="选择此行"]', change);
        if(SPREAD) $(document).on('click', 'input[aria-label="选择全部"]', CtrlA);
        $('.subgroup-text i').closest('a').each(function() {
            if(MODE) var $button = $('<a class="js-magnet magnet-link" ref="mode" style="background-color:white" >  模式切换:多词模式</a>');
            if(CLEAR) var $del = $('<a class="js-magnet magnet-link" ref="del" >  [清空勾选框]</a>');
            if(SEARCH) var $input = $('<input type="text" id="magnet-input" placeholder="输入关键字词(区分大小写)">');
            var $thunder_magnet = $('<a class="js-magnet magnet-link" ref="thunder" style="background-color:white" >  [复制/打开]  </a>');
            $(this).after($thunder_magnet, $input ,$del, $button);//添加按钮
        });
        if(LOAD) setTimeout(autoFillSubgroupSettings, 500);
        $('input.js-episode-select').each(function() {//先执行一遍
            $(this).attr('effect', 'uncheck');
        });
        const origOpen = XMLHttpRequest.prototype.open;
        XMLHttpRequest.prototype.open = function() {
            this.addEventListener('loadend', function() {
                $('input.js-episode-select').each(function() {
                    $(this).attr('effect', 'uncheck');
                });
            });
            origOpen.apply(this, arguments);
        };
        if(CATALOGUE) $('div.header').text('字幕组列表(点击复制)');
        if(SPREAD) click();
    }
})();