Pixiv小工具

try to take over the world!

Stan na 04-07-2017. Zobacz najnowsza wersja.

// ==UserScript==
// @name         Pixiv小工具
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        *://www.pixiv.net/bookmark.php?*
// @match        *://www.pixiv.net/bookmark_detail.php?*
// @run-at document-end

// @grant none1
// @grant        unsafeWindow
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_listValues
// @grant        GM_info
// @grant        GM_addStyle
// @grant        GM_registerMenuCommand
// @grant       GM_deleteValue
// @grant       GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';
    var css = '';

    //增大管理收藏的复选框点击有效范围
    $('.image-item .input-container').click(function (e){
        // console.log('点击有效范围checked');
        // console.log(e.target);
        // console.log(this);
        if(e.target!=this){
            // console.log('true执行checked');
            return true;
        }
        // console.log('false执行checked');
        var _cb = $(this).find('input[type=checkbox]');
        _cb.prop('checked', !_cb.prop('checked'));
        // e.stopPropagation();
        // e.preventDefault();
    });

    //每行开头添加勾选本行
    var rowNum = 4;
    //alert($('._image-items').length);
    // var rowFirstHeight = [];
    // var _maxHeight = 0;
    // $('._image-items .image-item').each(function(i, ele){
    //     if(_maxHeight < $(this).height()){
    //         _maxHeight = $(this).height();
    //     }
    //     if((i + 1)%rowNum === 0){
    //         rowFirstHeight[rowFirstHeight.length] = _maxHeight;
    //         _maxHeight = 0;
    //     }
    // });
    //alert(rowFirstHeight);
    var rowCheckbox = $('<div>').css({
        height: '20px',
        width: '20px',
        position: 'absolute',
        left: '-20px',
        'background-color': 'rgba(0,0,0,0.1)'
    });
    css +=
        '.rowCheckbox {' +
        'height: 100%;' +
        'width: 100%;' +
        '}';
    var _checkbox = $('<input>')
    .prop('type', 'checkbox')
    .addClass('rowCheckbox')
    .appendTo(rowCheckbox);

    //$('.display_editable_works').prepend(rowCheckbox);
    // :contains(checkbox)
    $('._image-items .image-item .input-container').parent('.image-item').each(function(i, ele){
        // $('._image-items .image-item').each(function(i, ele){
        //console.log('index:' + i + '=' + i%rowNum);
        if(i%rowNum === 0){
            var rc = rowCheckbox.clone();
            $(this).prepend(rc);
            rc.click(function(e){
                // console.log(e.target==this);
                var _cb = $(this).find('input[type=checkbox]');
                // console.log(_cb.get(0));
                var _f = _cb.prop('checked');
                console.log(_f);
                if(e.target==this){
                    //不是直接点击复选框时需要手动改变
                    _f = !_f;
                    _cb.prop('checked', _f);
                }
                var _imgLi = $(this).parent('li.image-item');
                // console.log(_imgLi.get(0));
                // console.log(_imgLi.add(_imgLi.nextAll().slice(0, rowNum - 1)));
                // console.log(_imgLi.add(_imgLi.nextAll().slice(0, rowNum - 1)).find('.input-container input[type=checkbox]'));
                _imgLi.add(_imgLi.nextAll().slice(0, rowNum - 1))
                    .find('input[type=checkbox]')
                    .prop('checked', _f);
            });
        }
    });

    css += 
        '/* 额外添加样式 */' +

        '.fast_click-ul .image-item {' +
        'background-color: rgba(96, 188, 242, 0.22)' +
        '}' +

        '.fast_click-ul .fastClickDiv {' +
        'display: block;' +
        '}' +

        '.fastClickDiv {' +
        'display: none;' +
        'position: absolute;' +
        'height: 100%;' +
        'width: 100%;' +
        'left: 0px;' +
        'z-index: 99;' +
        // 'background-color: rgba(0, 0, 0, 0.1);' +
        '}';

    //console.log(unsafeWindow);
    GM_registerMenuCommand('快速点击',function(){
        //$('._image-items .image-item').toggleClass('fast_click');
        //console.log($('._image-items .image-item').length);
        //console.log($('._image-items .image-item:not(fast_click)').length);
        //console.log($('._image-items .image-item.fast_click').length);
        //$('._image-items .image-item:not(fast_click)').css( 'background-color', 'rgba(96, 188, 242, 0.22)');
        //$('._image-items .image-item.fast_click').css( 'background-color', '');

        toggle_fastClick();
    },'fast_click');
    $(document).keydown(function(e){
        // console.log(e.which);
        // console.log(e.altKey);
        // e.shiftKey、e.ctrlKey
        // alt + s 热键触发
        if(e.altKey && e.keyCode == 83){
            toggle_fastClick();
        }
    });
    function toggle_fastClick(){
        $('._image-items').toggleClass('fast_click-ul');
    }

    var fastClickDiv = $('<div>').addClass('fastClickDiv');

    //列表快速点击模式
    //$('#illust-recommend')
    $('._image-items').each(function(i, ele){
        console.log('_image-item:' + i);
        $(this).on('DOMNodeInserted', function(e) {
            //动态加载内容时触发
            // console.log('element now contains: ' + $(e.target).html());
            var ele;
            if($(e.target).hasClass('fastClickDiv')){
                // continue;
                return true;
            }else if($(e.target).hasClass('image-item')){
                ele = e.target;
            }else{
                ele = $(e.target).parent('.image-item').get(0);
            }
            fastClick_imageItemsBuild(ele);
        });
        $(this).find('.image-item').each(function(i, ele){
            // console.log('image-item:' + i);
            fastClick_imageItemsBuild(ele);
        });
    });
    function fastClick_imageItemsBuild(ele){
        // console.log(ele);
        if(!ele || $(ele).find('.fastClickDiv').length > 0){
            return false;
        }
        var _fcd = fastClickDiv.clone();
        $(ele).prepend(_fcd);
        _fcd.click(function(e){
            console.log('fastClickDiv>>>');
            // console.log($(this).parent('.image-item').find('_one-click-bookmark'));
            var _imgLi = $(this).parent('.image-item');
            var _cb = _imgLi.find('input[type=checkbox]');
            if(_cb.length > 0){
                _cb.prop('checked', !_cb.prop('checked'));
            }else{
                _imgLi.find('._one-click-bookmark:not(.on)').click();
            }
        });
    }

    GM_addStyle(css);
})();