// ==UserScript==
// @name CopyExecl
// @name:zh-CN 网页表格复制器
// @namespace http://tampermonkey.net/
// @version 1.231214.4
// @description 网页表格复制脚本
// @author N-cat
// @match *://*/*
// @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAAELlJREFUeF7tnXnwXtMZxz9JEGInQoNqqH3GGFTQRJGxNZiaWlqprSq0lkpotWlR20xqSFU7UqWWoqIlf4haqpbotJaInVINIrYglhGaSELn4b78iN/7nnPvuefe99zvmcmMGc95znm+5/n8znvfe97z9EFNCkiBXhXoI22kgBToXQEBouyQAm0UECBKDykgQJQDUiCfAtpB8ummXg1RQIA0ZKEVZj4F6gxIX2CJ7J/N0/5bLR0FFgHvAwuBBcAHdQytjoAsB2wDbAesD6wBrAj0r6OAmlNuBQyKN4EXgaeAO4F7gHm5PZbQsU6A7ASMBb4GDMhitV1ELX0FbCexZrvKbcCZwD/qEHYdAFkPOB4YDfSrgyiaQ+UKGCiTgHHAc1XOpmpARgDnAZtUKYLGrq0C04EfZ7tKJZOsEpDDgLOBlSqJXIN2iwLPACcAk6uYcFWA7AdcBCxfRdAas+sUeAk4Brg29syrAOSrwFXA2rGD1XhdrYBBMhJ4IGYUsQFZB7gO2CxmkBorGQUey14BzI0VUWxALgMOihWcxklSgd8Cx8Z6sRgTkC2Ae/VVbpJJGzOo/wFbAv+OMWhMQG4F7GWgmhQoqsDFwJHZEZWivtr2jwXIEOBJYMlSo5HzpihgLxLtBfPMsgOOBcihgFGvJgVCKbBPjK99YwBix0cmAoeHUkZ+pABwOnBy2UrEAMQOHt4MDCs7GPlvlAKXAvbJpNQWA5AVgAcBew5RkwKhFLgR+H7ZzyExAFkZeBYwUIo0ezlk/2r5w5oigTWsr63fKsDSBeO+P3unZi8PS2sxABkE2IGz1m888gRjR1MOAGLMN8/81MddAQPEfvxmJyp2ce+2mOU7wI7AtAI+OnaNkXAhALHt9Osdo5FBNynwR+DAAhMWID3Euwn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HW5MQIYBdwD9yg5K/pNWYAGwFfBwjChjAmLxWDHOXWMEpjGSVeAq4EDA3o2V3mIDMhSYBHyp9Mg0QIoK2IvirYFnYwUXGxCLaxRwLjAwVpAaJwkF3gIOiF1bvgpAbLVGA+OBlZNYOgVRtgIGxxjATgBHbVUBYkF+GzgH+ELUiDVYtylgX+kenX00jz73KgGxYIcDlwFDokeuAbtBAXt3Zicw7qpqslUDYnHbad/js7ftq1YlhMatlQKvARcAZwNvVjmzOgDSin8J4ODsZaK9UBxQpTAaO7oC87LXAH8D7KvcSsFoRV8nQFpzWib7hmstwI6o2IO8nQjWC8boOVvqgO8Bc7Pf+NiRkTey/3631FE9ndcREM8QZC4FylNAgJSnrTwnoIAASWARFUJ5CgiQ8rSV5wQUECAJLKJCKE8BAVKetvKcgAICJIFFVAjlKSBAytNWnhNQ4P+0+QEyV+u1fAAAAABJRU5ErkJggg==
// @homepageURL https://greasyfork.org/zh-CN/scripts/481068-copyexecl
// @supportURL https://gitee.com/z2322739526/copyexecl
// ==/UserScript==
(function() {
'use strict';
// 全局变量默认值
let startX = 9999; // 按下坐标
let startY = 9999;
let endX = 9999; // 鼠标坐标
let endY = 9999;
var tds = ""; // 所有td,th标签
var tables = "";
// 样式添加
const style = `<style>
.get{
position:fixed;
right:80px;
bottom:150px;
background-color:#00a1d6;
color:white;
height:50px;
width:50px;
border-radius: 10px;
font-size:15px;
border:solid 3px #FA5A57;
cursor:pointer;
outline: none;
z-index: 9999;
}
.get:hover {
background-color:#33b4de;
}
.display{
position:fixed;
right:80px;
bottom:80px;
background-color:#00a1d6;
color:white;
height:50px;
width:50px;
border-radius: 10px;
font-size:15px;
border:solid 3px #FA5A57;
cursor:pointer;
outline: none;
z-index: 9999;
}
.display:hover {
background-color:#33b4de;
}
.CEinfo{
position:fixed;
right:80px;
top: unset !important; /* 取消设置 */
bottom:30px !important;
background-color:#00a1d6;
width: 100px;
color:white !important;
border-radius: 10px;
height: 30px;
border:solid 3px #FA5A57;
outline: none;
cursor:default;
display: inline-flex; /* 使其成为一个弹性盒子 */
justify-content: center; /* 垂直居中 */
align-items: center; /* 水平居中 */
text-decoration: none !important; /* 取消下滑线 */
cursor: pointer; /* 鼠标变为手势 */
font-size: 12px;
z-index: 9999;
}
.CEinfo:hover {
color:white; /* 悬浮时也不变色 */
background-color:#33b4de;
}
.alertMessage{
position: fixed;
top: 30px;
left: 40%;
right: 40%;
padding: 20px 30px;
background: rgba(0, 0, 0, 0.8);
color: #ffffff;
font-size: 20px;
text-align: center;
z-index: 9999;
display: none;
border-radius:5px;
}
</style>`;
let div = document.createElement("div");
div.innerHTML += style;
document.body.append(div);
// 提示框
var alertMessage = document.createElement("div");
alertMessage.classList.add('alertMessage');
document.body.append(alertMessage);
function alertmess(mess) {
alertMessage.innerHTML = mess; // 填入要显示的文字
alertMessage.style.display = "inline"; // 显示弹框
setTimeout(function () { // 倒计时
alertMessage.innerHTML = ''; // 清空文本
alertMessage.style.display = "none" // 隐藏弹框
}, 3000); // 3秒
}
// 鼠标悬浮在td上时提示
function tdmm(){
this.style.backgroundColor = 'black';
this.style.color = 'white';
}
function tdmo(){
this.style.removeProperty("background-color");
this.style.removeProperty("color");
}
// 框选变色,抬起复制
function wmd(e){
startX = e.clientX;
startY = e.clientY;
// console.log(e.target);
var wmdtd = e.target; // 是object对象而非HTMLTableCellElement
if (wmdtd instanceof HTMLTableCellElement) {
while (wmdtd && !wmdtd.matches("td, th")) { // css选择器
wmdtd = wmdtd.parentNode;
}
wmdtd.style.backgroundColor = 'black';
wmdtd.style.color = 'white';
wmdtd.style.borderColor = "white";
wmdtd.setAttribute("data-copycell", "copycell");
wmdtd.dataset.copycell = "copycell";
} else {
console.log("点击内容如下,非表格对象\n", wmdtd);
}
}
function wmm(e){
// 点击判断
if(startX !== 9999) {
endX = e.clientX;
endY = e.clientY;
for(let i of tds){
// 获取tr元素的左上角坐标和宽度、高度
var rect = i.getBoundingClientRect();
var tdleft = rect.left;
var tdtop = rect.top;
var tdwidth = rect.width;
var tdheight = rect.height;
// console.log("起止坐标", startX, endX, startY, endY);
// console.log("td坐标", tdleft, tdtop);
// 伪代码 if (X->((E<右 && 左<S) || (S<右 && 左<E)) && Y->((E<下 && 上<S) || (S<下 && 上<E))){}
if (((endX <= tdleft + tdwidth && tdleft <= startX) || (startX <= tdleft + tdwidth && tdleft <= endX)) && ((endY <= tdtop + tdheight && tdtop <= startY) || (startY <= tdtop + tdheight && tdtop <= endY))) {
//if (startX <= tdleft + tdwidth && startY <= tdtop + tdheight && endX >= tdleft && endY >= tdtop) { /////////目前只能从左上角框选到右下角,待更新
// 将tr元素的背景色设置为黑色(或其他你想要的颜色)
i.style.backgroundColor = 'black';
i.style.color = 'white';
i.style.borderColor = "white";
i.setAttribute("data-copycell", "copycell");
i.dataset.copycell = "copycell";
} else {
i.style.removeProperty("background-color");
i.style.removeProperty("color");
i.style.removeProperty("border-color");
i.dataset.copycell = "";
}
}
}
}
function wmu(e){
startX = 9999; // 按下坐标
startY = 9999;
endX = 9999; // 鼠标坐标
endY = 9999;
var copyexecl = "";
var oldtr = "";
for(let i of tds){
// 输出选中范围内信息
if(i.dataset.copycell === "copycell"){
// console.log(i.innerText);
var itr = i;
while (itr && !itr.matches("tr")) { // css选择器
itr = itr.parentNode;
}
if(itr === oldtr || oldtr === ""){
copyexecl += '\"\'' + i.innerText + '\"\t';
} else {
copyexecl = copyexecl.slice(0,-1) + '\n\"\'' + i.innerText + '\"\t';
}
oldtr = itr;
}
// 重置
i.style.removeProperty("background-color");
i.style.removeProperty("color");
i.style.removeProperty("border-color");
i.dataset.copycell = "";
}
// 当前框仍然为鼠标悬浮变色
if (e.target instanceof HTMLTableCellElement){
e.target.style.backgroundColor = 'black';
e.target.style.color = 'white';
}
console.log(copyexecl);
// 内容复制到剪切板
const textarea = document.createElement('textarea');
document.body.appendChild(textarea);
textarea.innerHTML = copyexecl;
textarea.select(); // 选取文本域的内容
if (document.execCommand('copy') && copyexecl !== "") {
document.execCommand('copy');
alertmess("框选区域已复制到剪切板");
}
document.body.removeChild(textarea);
}
/*
// 点击复制全部execl(基础适配)
function tabmd(){
var execl = []
var trs = this.getElementsByTagName("tr");
for(let j of trs){
var row = []
var tds = j.getElementsByTagName("td");
for(let k of tds){
if(k.style.display !== "none"){
row.push(k.innerText);
}
}
execl.push(row);
}
console.log(execl);
var csv = ""
for(let i of execl){
for(let j of i){
csv += '\"\'' + j + '\"\t';
}
csv = csv.slice(0,-1) + "\n";
}
console.log(csv);
// 复制视频名称到剪切板
const textarea = document.createElement('textarea');
document.body.appendChild(textarea);
textarea.innerHTML = csv;
textarea.select(); // 选取文本域的内容
if (document.execCommand('copy')) {
document.execCommand('copy');
alertmess("网页表格已复制到剪切板");
}
document.body.removeChild(textarea);
}
*/
// 获取包括iframe内的td和th
function getAllTags(node, tags){
var Tags = [];
var taglist = tags.replace(/\s*/g, "").split(',');
taglist.forEach(item => {
item = item.toLowerCase(); // 将每个元素转换为小写字母
});
for (let i of node.childNodes) {
// 如果子节点是td标签,则将其添加到结果数组中
if (i.tagName && taglist.includes(i.tagName.toLowerCase())) {
Tags.push(i);
}
// 如果子节点是iframe或者为Node.ELEMENT_NODE,则递归遍历其子节点
if (i.tagName && i.tagName.toLowerCase() === "iframe") {
Tags = Tags.concat(getAllTags(i.contentWindow.document, tags));
} else if (i.nodeType === Node.ELEMENT_NODE) {
console.log(i.nodeType);
Tags = Tags.concat(getAllTags(i, tags));
}
}
return Tags;
}
// 获取按钮 //////////main////////////
var getnum = 0 // 0:未(取消)触发 1:已触发
var get = document.createElement("input");
get.setAttribute("type", "button");
get.setAttribute("value", "开启");
get.classList.add('get');
document.body.append(get);
get.onclick = function(){
// tds = getAllTags(document, 'td, th'); // 获取包括iframe内的td和th
tds = document.querySelectorAll('td, th'); // css选择器
console.log(tds);
if(getnum === 0){ // 开启
getnum = 1;
get.setAttribute("value", "关闭");
for(let i of tds){
// 文本设置无法选中
i.style.userSelect = "none";
// 鼠标悬浮在td上时提示
i.addEventListener("mousemove", tdmm);
i.addEventListener("mouseout", tdmo);
}
// 框选变色(还未添加复制功能)
window.addEventListener("mousedown", wmd);
window.addEventListener("mousemove", wmm);
window.addEventListener("mouseup", wmu);
/*
// 点击复制全部execl(基础适配)
tables = document.getElementsByTagName("table");
for(let i of tables){
i.addEventListener("mousedown", tabmd);
}
*/
alertmess("网页表格复制器-已开启");
} else {
getnum = 0;
get.setAttribute("value", "开启");
for(let i of tds){
i.style.removeProperty("user-select");
i.removeEventListener('mousemove', tdmm);
i.removeEventListener('mouseout', tdmo);
}
window.removeEventListener('mousedown', wmd);
window.removeEventListener('mousemove', wmm);
window.removeEventListener("mouseup", wmu);
tables = document.getElementsByTagName("table");
/*
for(let i of tables){
i.removeEventListener("mousedown", tabmd);
}
*/
alertmess("网页表格复制器-已关闭");
}
}
// 程序CEinfo
var CEinfo = document.createElement("a");
CEinfo.classList.add('CEinfo');
CEinfo.innerHTML = "网页表格复制器";
CEinfo.setAttribute("href", "https://greasyfork.org/zh-CN/scripts/481068-copyexecl");
CEinfo.setAttribute("target", "_blank");
document.body.append(CEinfo);
// 单次隐藏按钮
var display = document.createElement("input");
display.setAttribute("type", "button");
display.setAttribute("value", "隐藏");
display.classList.add('display');
document.body.append(display);
display.onclick = function(){
get.style.display = "none";
display.style.display = "none";
CEinfo.style.display = "none";
// 关闭
for(let i of tds){
i.style.removeProperty("user-select");
i.removeEventListener('mousemove', tdmm);
i.removeEventListener('mouseout', tdmo);
}
window.removeEventListener('mousedown', wmd);
window.removeEventListener('mousemove', wmm);
window.removeEventListener("mouseup", wmu);
tables = document.getElementsByTagName("table");
/*
for(let i of tables){
i.removeEventListener("mousedown", tabmd);
}
*/
alertmess("网页表格复制器-已隐藏");
}
})();