jkluio668
3/3/2019 - 7:42 AM

spider-jd-book

// ==UserScript==
// @name         0jd-book-_content-1.0.2-181110
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  try to take over the world!
// @author       You
// @match        https://item.jd.com/*
// @match        item.jd.com/*
// @grant        none
// ==/UserScript==

(function() {
    setTimeout(do1, 3 * 1000);
    // do1()
    function do1() {
        var cont1 = document.querySelector('div.item.first a').innerText
        if(cont1='图书'){
            main1()
        }
    }
//----------------
    function main1(){
        var url1 = window.location.href
        var title1 = document.querySelector('div.m-item-inner div div div.sku-name').innerText
        var s_tmp = document.querySelectorAll('div.detail-content-item div div.book-detail-item')
        var cont_1='' , rcm_1='' ,cont_2='', author_1='' , con_1='' , perface_1=''
        for (var i = 0; i < s_tmp.length; i++) {
            var str_tmp1 = s_tmp[i].getAttribute('text')
            try{
                var str_tmp2 = str_strip(s_tmp[i].querySelector('div.item-mc div.book-detail-content').innerText)
            } catch(err){
                continue
            }
            if (str_tmp1=='目录'){
                // 此处需要用getAttribute,不能用['text'],否则会得到文字
                cont_1 = str_tmp2
                //             break
            } else if(str_tmp1=='编辑推荐'){
                rcm_1 = str_tmp2
            } else if(str_tmp1=='内容简介'){
                cont_2 = str_tmp2
            } else if(str_tmp1=='作者简介'){
                author_1 = str_tmp2
            } else if(str_tmp1=='精彩书评'){
                con_1 = str_tmp2
            } else if(str_tmp1=='前言/序言'){
                perface_1 = str_tmp2
            }
        }
        // tampermonkey中,无法使用'text=目录'这样的中文的选择器
        // var detail_1 = document.querySelector('div[text=\'目录\'] div.item-mc div.book-detail-content').innerText

        //--------------------------
        var l1 = [cont_1,cont_2,rcm_1,con_1,author_1,perface_1]
        var l2 = ['目录','内容','编辑推荐','评论','作者','序言']
        for (var i = l1.length-1; i >= 0; i--){
            again1(i+1,l1[i],l2[i])
        }
//     again1(textarea_id,input_id,cont_1)
//     again1('copy21','copy22',rcm_1)
//     again1('copy31','copy32',cont_2)
//     again1('copy41','copy42',author_1)
//     again1('copy51','copy52',perface_1)
//     again1('copy61','copy62',con_1)

        scrollTo(0, 0)

    //--------------------------
    function str_strip(str1){
        str1 = str1.replace(/(\s)*\n/g,'\n')
        var frame_1 = str1.split('\n')
        str1 = [url1,title1 ].join('\t') +'\n'
        for (var i = 0; i < frame_1.length-1; i++) {
            str1 += '\t\t'+frame_1[i]
            if (i != frame_1.length-2){
                str1+= '\n'
            }
        }
        return str1
    }
    function selText(textarea_id) {
        //   console.clear();
//         textarea_id = 'copy3';
        document.getElementById(textarea_id).select();
        try {
            if (document.execCommand('copy', false, null)) {
                //     if(document.execCommand('copy', false, null)){
                console.log('success')
                //success info
            } else {
                console.log('fail')
                //fail info
            }
        } catch (err) {
            console.log(err)
            //fail info
        }
    }
//     function again1(textarea_id,input_id,input_id2,span1_id,str1,str2) {
    function again1(i,str1,str2) {
        var span_id='cp'+(i)+'1'
        var input_id2='cp'+(i)+'2'
        var textarea_id='cp'+(i)+'3'
        var input_id='cp'+(i)+'4'
        //
        var body = document.getElementsByTagName('body')[0];
        var span1 = document.getElementById(span_id)
        var textarea1, btn_1, btn_2,div1
        //
        if (span1){
            body.removeChild(span1)
        }
        //
        textarea1 = document.createElement("textarea");
        // textarea1.innerHTML = text;
        textarea1.setAttribute('id', textarea_id);
        textarea1.setAttribute('cols', '80');
        textarea1.setAttribute('rows', '10');
        textarea1.style.cssText ='font-size:15px;'
        //
        btn_1 = document.createElement("input");
        btn_1.setAttribute('id', input_id);
        btn_1.setAttribute('type', 'button');
        btn_1.setAttribute('onclick', 'selText()');
        btn_1.setAttribute('value', 'cp');
        btn_1.style.cssText = '    -webkit-appearance: button;    box-sizing: border-box;  width:28px;  height: 120px;    position: absolute;'
        //
        btn_2 = document.createElement("input");
        btn_2.setAttribute('id', input_id2);
        btn_2.setAttribute('value', str2);
        btn_2.setAttribute('readonly', 'true');
        btn_2.setAttribute('size', '10');
        //
        div1 = document.createElement("div");
        //
        span1 = document.createElement("span");
        span1.setAttribute('id', span_id);
//         if
        span1.style.cssText = 'display:inline-block;margin:0px 16px 0px 12px';//btn_1的width是28px
        // span1.style.margin='0px 12px 0px 2px'
        //
		div1.insertBefore(btn_2,div1.childNodes[0])
        var l_tmp = [btn_1,textarea1,div1];
        for (i = 0;i < l_tmp.length;i++){
            span1.insertBefore(l_tmp[i],span1.childNodes[0]);
        }
        body.insertBefore(span1, body.childNodes[0]);
//         body.insertBefore(btn_1, body.childNodes[0]);
//         body.insertBefore(textarea1, body.childNodes[0]);
//         body.insertBefore(btn_2, body.childNodes[0]);
        //
//         var textarea1 = document.getElementById(textarea_id)
        textarea1.innerHTML = str1
        //此处不能用innerText,否则\n会被转换为<br>
//         document.querySelector('input#'+input_id).addEventListener('click', function() {selText()})
        btn_1.addEventListener('click', function() {selText(textarea_id)})
    }
}

})();