百度文库文字下载

添加一个按钮,复制百度文库中的文字

Tính đến 27-06-2018. Xem phiên bản mới nhất.

// ==UserScript==
// @name         百度文库文字下载
// @namespace    BlueFire
// @version      0.2
// @description  添加一个按钮,复制百度文库中的文字
// @author       BlueFire
// @match        *://wenku.baidu.com/view/*
// @require      http://cdn.bootcss.com/jquery/1.8.3/jquery.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    let toastDiv = '<div id="page-toast-div" style="margin: 0px auto;background: black;opacity: 0.8;padding: 15px 30px;position: fixed;z-index: 1000;display: block;top: 85%;left: 48%;"><span id="page-toast-span" style="color:white;"></span></div>';

    function Copy(str){
        var save = function(e){
            e.clipboardData.setData('text/plain', str);
            e.preventDefault();
        }
        document.addEventListener('copy', save);
        document.execCommand('copy');
        document.removeEventListener('copy',save);
    }

    function ShowToast(str){
        $('#page-toast-div').remove();
        $('body').append(toastDiv);
        $('#page-toast-span').text(str);
        var t=setTimeout("$('#page-toast-div').remove();",1500);
    }

    $(document).ready(function(){

        let downloadBtn = '<div style="float:left;padding:10px 20px;background:green;z-index:999;position:relative;top:60px;left:0px;"><a id="reader-copy-text" href="###" style="color:white;font-size:15px;"><b class="ui-btn-btc">复制此页</b></a></div>';
        $('.mod.reader-page.complex').each(function(){
            $(this).prepend(downloadBtn);
            let parent = $(this);
            $(this).find('#reader-copy-text').click(function(){
                let str = "";
                parent.find('.reader-word-layer').each(function(){
                    str += this.innerText.replace(/\u2002/g,' ');
                });
                if(str.length > 0){
                    Copy(str);
                    ShowToast("复制成功");
                }else{
                    ShowToast("复制失败,请等待网页加载");
                }
            });
        });
    });
})();