扇贝爬取
// ==UserScript==
// @name 扇贝爬取
// @namespace http://tampermonkey.net/
// @version 1.3
// @description try to take over the world!
// @author Checkmate
// @match *://web.shanbay.com/*
// @grant GM_notification
// @icon https://www.google.com/s2/favicons?domain=shanbay.com
// @grant unsafeWindow
// @grant GM_xmlhttpRequest
// ==/UserScript==
var $ = unsafeWindow;
var english;
var chinese;
var index;
var end;
var timer;
var timer1;
var timer2;
var interval = 0;
var op = 0;
var flag = 0;
var radio_flag = 0;
var flag2 = 0;
var radio_flag2 = 0;
var o =document.createElement("input");
o.type = "button";
o.value = "开始";
o.style.width = "80px";
o.style.height = "40px";
o.style.margin = "2.5px 0px 0px 2px";
document.getElementsByClassName("SubNav_itemsWrapper__1mM4u")[0].appendChild(o);
var oo =document.createElement("input");
oo.type = "button";
oo.value = "清空";
oo.style.width = "80px";
oo.style.height = "40px";
oo.style.margin = "2.5px 0px 0px 2px";
document.getElementsByClassName("SubNav_itemsWrapper__1mM4u")[0].appendChild(oo);
var radio =document.createElement("input");
radio.setAttribute("id","radio");
radio.type = "radio";
radio.style.margin = "15.5px 0px 0px 5px";
radio.addEventListener("click",check);
document.getElementsByClassName("SubNav_itemsWrapper__1mM4u")[0].appendChild(radio);
var o2 =document.createElement("div");
o2.innerHTML = "爬取释义"
o2.style.margin = "10.5px 0px 0px 3px";
document.getElementsByClassName("SubNav_itemsWrapper__1mM4u")[0].appendChild(o2);
var radio2 =document.createElement("input");
radio2.setAttribute("id","radio2");
radio2.type = "radio";
radio2.style.margin = "15.5px 0px 0px 5px";
radio2.addEventListener("click",check2);
document.getElementsByClassName("SubNav_itemsWrapper__1mM4u")[0].appendChild(radio2);
var o22 =document.createElement("div");
o22.innerHTML = "纵向爬取(仅单词书)"
o22.style.margin = "10.5px 0px 0px 3px";
document.getElementsByClassName("SubNav_itemsWrapper__1mM4u")[0].appendChild(o22);
var o3 =document.createElement("div");
o3.innerHTML = "爬取间隔(毫秒)"
o3.style.margin = "10.5px 0px 0px 12px";
document.getElementsByClassName("SubNav_itemsWrapper__1mM4u")[0].appendChild(o3);
var o4 =document.createElement("input");
o4.style.margin = "10.5px 0px 0px 3px";
o4.style.width = "80px";
o4.style.height = "25px";
o4.value = 500;
document.getElementsByClassName("SubNav_itemsWrapper__1mM4u")[0].appendChild(o4);
var content =document.createElement("textarea");
content.setAttribute("id","content");
content.setAttribute("disabled","true");
content.style.width = "1320px";
document.getElementsByClassName("Layout_main__2_zw8")[0].appendChild(content);
var contentTemp =document.createElement("textarea");
contentTemp.setAttribute("id","contentTemp");
contentTemp.style.width = "1px";
contentTemp.style.height = "1px";
document.getElementsByClassName("Footer_footerWrap__L4iuD")[0].appendChild(contentTemp);
var leaf = 0;
var count = 0;
var countArea=document.createElement("div");
countArea.innerHTML = "当前爬取数量:" + count;
countArea.style.margin = "10.5px 0px 0px 15px";
document.getElementsByClassName("SubNav_itemsWrapper__1mM4u")[0].appendChild(countArea);
var o5 =document.createElement("input");
o5.type = "button";
o5.value = "复制";
o5.style.width = "80px";
o5.style.height = "40px";
o5.style.margin = "2.5px 0px 0px 120px";
document.getElementsByClassName("SubNav_itemsWrapper__1mM4u")[0].appendChild(o5);
function send_request(){
var nowVersion = "v1.3";
GM_xmlhttpRequest({
method: "get",
url: "https://scriptcat.org/script-show-page/203/version",
headers: {
"Content-Type": "charset=UTF-8"
},
onload: function(response){
var content = response.responseText;
var regularExpression = new RegExp("<div class=\"text-h5\".*?> (.*?) <div.*?>");
var onlineVersion = regularExpression.exec(content);
if(nowVersion != onlineVersion[1]){
alert("当前版本"+nowVersion+",最新版本"+onlineVersion[1]+",点击确定后将会跳转到更新页面。");
window.location = "https://scriptcat.org/script-show-page/203"
}
},
onerror: function(response){
alert("请求失败请检查网络!");
}
});
}
function check(){
if(radio_flag == 0){//选中
flag = 1;
radio_flag = 1;
}else if(radio_flag == 1){
flag = 0;
radio_flag = 0;
radio.checked = false;
}
}
function check2(){
if(radio_flag2 == 0){//选中
flag2 = 1;
radio_flag2 = 1;
}else if(radio_flag2 == 1){
flag2 = 0;
radio_flag2 = 0;
radio2.checked = false;
}
}
function initialize(){
send_request();//检查版本
oo.addEventListener("click",clear);
o5.addEventListener("click",copyText);
timer = setInterval(function(){
var url = window.location.href;
if( url == "https://web.shanbay.com/wordsweb/#/collection"){//生词本
o.removeEventListener("click",begin_wordText);
o.addEventListener("click",begin_newWordText);
}else if ( url == "https://web.shanbay.com/wordsweb/#/words-table"){
o.removeEventListener("click",begin_newWordText);
o.addEventListener("click",begin_wordText);
}
},300);
}
function copyText(){
if(content.innerHTML != ""){
contentTemp.innerHTML = content.innerHTML;
contentTemp.select();
document.execCommand("copy");
GM_notification({
title: "扇贝爬取",
text: "复制完成",
timeout: 1200,
});
}
}
function clear(){
content.innerHTML = "";
count = 0;
countArea.innerHTML = "当前爬取数量:" + count;
}
function copy_wordText(){
timer1 = setInterval(function(){
end = $.document.getElementsByTagName("li");
var last = parseInt(end[end.length - 2].innerHTML);
var res = end[end.length - 1];
var str="";
english = $.document.getElementsByClassName("index_wordName__1lkbV");
chinese = $.document.getElementsByClassName("index_bottom__XLoPQ");
if(flag == 0){
if(flag2 == 1){
for(var i=0;i<english.length;){
str += english[i].innerHTML + '\n';
count++;
i += 2;
}
for(i=1;i<english.length;){
str += english[i].innerHTML + '\n';
count++;
i += 2;
}
}else if(flag2 == 0){
for(i=0;i<english.length;i++){
str += english[i].innerHTML + '\n';
count++;
}
}
}else if(flag == 1){
if(flag2 == 1){
for(var j=0;j<english.length;){
var temp = chinese[j].innerHTML.replaceAll("<span>","");
temp = temp.replaceAll("<span class=\"index_pos__38JdC\">","");
temp = temp.replaceAll("</span>","");
str += english[j].innerHTML + " " + temp + '\n';
count++;
j += 2;
}
for(j=1;j<english.length;){
temp = chinese[j].innerHTML.replaceAll("<span>","");
temp = temp.replaceAll("<span class=\"index_pos__38JdC\">","");
temp = temp.replaceAll("</span>","");
str += english[j].innerHTML + " " + temp + '\n';
count++;
j += 2;
}
}else if(flag2 == 0){
for(j=0;j<english.length;j++){
temp = chinese[j].innerHTML.replaceAll("<span>","");
temp = temp.replaceAll("<span class=\"index_pos__38JdC\">","");
temp = temp.replaceAll("</span>","");
str += english[j].innerHTML + " " + temp + '\n';
count++;
}
}
}
content.innerHTML += str;
countArea.innerHTML = "当前爬取数量:" + count;
leaf++;
if(leaf < last){
res.click();
}else{
o.value = "开启";//close
clearInterval(timer1);
op = 0;
leaf = 0;
}
},interval);
}
function copy_newWordText(){
timer2 = setInterval(function(){
end = $.document.getElementsByTagName("li");
var last = parseInt(end[end.length - 2].innerHTML);
var res = end[end.length - 1];
var str="";
english = $.document.getElementsByClassName("CollectionList_word__7zQwd");
chinese = $.document.getElementsByClassName("CollectionList_definitionCn__3MoTq");
if(flag == 0){
for(var i=0;i<english.length;i++){
str += english[i].innerHTML + '\n';
count++;
}
}else if(flag == 1){
for(var j=0;j<english.length;j++){
var temp = chinese[j].innerHTML.replaceAll("<span>","");
temp = temp.replaceAll("</span>","");
str += english[j].innerHTML + " " + temp + '\n';
count++;
}
}
content.innerHTML += str;
countArea.innerHTML = "当前爬取数量:" + count;
leaf++;
if(leaf < last){
res.click();
}else{
o.value = "开启";//close
clearInterval(timer2);
op = 0;
leaf = 0;
}
},interval);
}
function begin_wordText(){
if(op == 0){
interval = o4.value;
o.value = "爬取中...";//open
copy_wordText();
op = 1;
}
}
function begin_newWordText(){
if(op == 0){
interval = o4.value;
o.value = "爬取中...";//open
copy_newWordText();
op = 1;
}
}
initialize();