// ==UserScript==
// @name 百度文库文字复制
// @namespace BlueFire
// @version 0.5
// @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
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
let timeoutId = -1;
let downloadBtn = '
';
let toastDiv = '
';
let textBlockDiv = '"';
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 ShowTextBlock(str){
$('#page-textblock-div').remove();
$('body').append(textBlockDiv);
$('#page-textblock').text(str);
$('#page-textblock-cancel-layer').click(function(){
$('#page-textblock-div').remove();
});
}
function PrependButtonTo(ele){
ele.prepend(downloadBtn);
ele.find('#reader-copy-text').click(function(){
let str = "";
let parent = $(this).parent().parent();
parent.find('.reader-word-layer').each(function(){
str += this.innerText.replace(/\u2002/g,' ');
});
str = str.replace(/。\s/g,'。\r\n');
let result= (str.length > 0 && Copy(str));
if(result){
ShowToast("复制成功");
ShowTextBlock(str);
}else{
ShowToast("复制失败,请等待网页加载");
}
});
}
$(document).ready(function(){
$('.mod.reader-page.complex').each(function(){
PrependButtonTo($(this));
});
});
})();