Greasy Fork is available in English.

百度文库文字复制

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

Från och med 2018-06-27. Se den senaste versionen.

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

(function() {
    'use strict';
    let timeoutId = -1;
    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>';
    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: 44%;"><span id="page-toast-span" style="color:white;font-size:15px;"></span></div>';
    function Copy(str){
        let result = false;
        let save = function(e){
            e.clipboardData.setData('text/plain', str);
            e.preventDefault();
        }
        document.addEventListener('copy', save);
        result = document.execCommand('copy');
        document.removeEventListener('copy',save);
        return result;
    }

    function ShowToast(str){
        if(timeoutId >= 0){
            clearTimeout(timeoutId);
        }
        $('#page-toast-div').remove();
        $('body').append(toastDiv);
        $('#page-toast-span').text(str);
        timeoutId=setTimeout("$('#page-toast-div').remove();",1500);
    }

    function PrependButtonTo(ele){
        ele.prepend(downloadBtn);
        ele.find('#reader-copy-text').click(function(){
            let str = "";
            ele.find('.reader-word-layer').each(function(){
                str += this.innerText.replace(/\u2002/g,' ');
            });
            let result= (str.length > 0 && Copy(str));
            if(result){
                ShowToast("复制成功");
            }else{
                ShowToast("复制失败,请等待网页加载");
            }
        });
    }

    $(document).ready(function(){
        $('.mod.reader-page.complex').each(function(){
            PrependButtonTo($(this));
        });
    });
})();