jkluio668
2/13/2018 - 2:51 AM

spider_zhihu_to md

var url = window.location.href
var title = document.title
//
declaration1()
str1 += '# ' + title  + '\t' + url + '\n'
//
var answers = document.querySelectorAll('div.List-item')
for (var i = 0; i < answers.length; i++) {
    var temp = answers[i].getElementsByClassName('Voters')[0]
    var n_up = temp ? temp.innerText : '0'
    n_up = parseInt(n_up.split(' ')[0].replace(',', ''))
    var author1 = answers[i].querySelector('div.AuthorInfo-head').innerText.replace('\n', '')
    var date1 = answers[i].querySelector('div.ContentItem-time a span').innerText
    date1 = /\d{4}-\d{2}-\d{2}/.exec(date1)[0]
    var detail_1 = answers[i].querySelector('div.RichContent div.RichContent-inner').innerText
    //
    str1 += '## [' + n_up + ']\t' + author1 + '\t[' + date1 + ']\n' + detail_1 + '\n\n'
}
function again1() {
    var body = document.getElementsByTagName('body')[0];
    var textarea1 = document.getElementById(textarea_id)
    var button1 = document.getElementById(input_id)
    if (textarea1) {
        body.removeChild(textarea1)
    }
    if (button1) {
        body.removeChild(button1)
    }
    var textarea1 = document.createElement("textarea");
    // textarea1.innerHTML = text; 
    textarea1.setAttribute('id', textarea_id);
    textarea1.setAttribute('cols', '100');
    textarea1.setAttribute('rows', '10');
    //
    var button1 = document.createElement("input");
    button1.setAttribute('id', input_id);
    button1.setAttribute('type', 'button');
    button1.setAttribute('onclick', 'selText()');
    button1.setAttribute('value', 'Copy');
    button1.style.cssText = '    -webkit-appearance: button;    box-sizing: border-box;    height: 180px;    position: absolute;'
    //
    body.insertBefore(button1, body.childNodes[0]);
    body.insertBefore(textarea1, body.childNodes[0]);
}
again1()
selText = function() {
    //   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 declaration1() {
    str1 = ''
    textarea_id = 'copy3'
    input_id = 'copy4'
}
var textarea1 = document.getElementById(textarea_id)
textarea1.innerHTML = str1
//此处不能用innerText,否则\n会被转换为<br>
document.querySelector('input#copy4').addEventListener('click', function() {selText()})

scrollTo(0, 0)