扇贝爬取
// ==UserScript==
// @name 扇贝爬取
// @namespace http://tampermonkey.net/
// @version 1.4
// @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;
var oo;
var radio;
var o2;
var radio2;
var o22;
var o3;
var o4;
var content;
var contentTemp;
var leaf;
var count;
var countArea;
var o5;
var div;
initialize();
function initialize(){
//检查版本
send_request();
//添加元素
append_child();
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 send_request(){
var nowVersion = "v1.4";
GM_xmlhttpRequest({
method: "get",
url: "https://scriptcat.org/zh-CN/script-show-page/203/code",
headers: {
"Content-Type": "charset=UTF-8"
},
onload: function(response){
var content = response.responseText;
var regularExpression = new RegExp("var.*?nowVersion.*?"(.*?)"");
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 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__3VqeJ");
chinese = $.document.getElementsByClassName("index_bottom__367D_");
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__gtJW-\">","");
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__gtJW-\">","");
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__gtJW-\">","");
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__2jcPr");
chinese = $.document.getElementsByClassName("CollectionList_definitionCn__1IOu9");
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;
}
}
function append_child(){
div=document.createElement("div");
//div.style.border = "solid 1px #000000";
div.style.margin = "5px auto 5px auto";
div.style.padding = "5px 2px 5px 2px";
div.style.width = "50%";
div.className = "div";
document.getElementsByClassName("SubNav_subnav__3HXgY")[0].after(div);
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";
o.className = "o";
document.getElementsByClassName("div")[0].append(o);
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";
oo.className = "oo";
document.getElementsByClassName("o")[0].after(oo);
radio =document.createElement("input");
radio.setAttribute("id","radio");
radio.type = "radio";
radio.style.margin = "15.5px 0px 0px 5px";
radio.addEventListener("click",check);
radio.className = "radio";
document.getElementsByClassName("oo")[0].after(radio);
o2 =document.createElement("span");
o2.innerHTML = "爬取释义"
o2.style.margin = "10.5px 0px 0px 3px";
o2.className = "o2";
document.getElementsByClassName("radio")[0].after(o2);
radio2 =document.createElement("input");
radio2.setAttribute("id","radio2");
radio2.type = "radio";
radio2.style.margin = "15.5px 0px 0px 5px";
radio2.addEventListener("click",check2);
radio2.className = "radio2"
document.getElementsByClassName("o2")[0].after(radio2);
o22 =document.createElement("span");
o22.innerHTML = "纵向爬取(仅单词书)"
o22.style.margin = "10.5px 0px 0px 3px";
o22.className = "o22";
document.getElementsByClassName("radio2")[0].after(o22);
o3 =document.createElement("span");
o3.innerHTML = "爬取间隔(毫秒)"
o3.style.margin = "10.5px 0px 0px 12px";
o3.className = "o3";
document.getElementsByClassName("o22")[0].after(o3);
o4 =document.createElement("input");
o4.style.margin = "10.5px 0px 0px 3px";
o4.style.width = "80px";
o4.style.height = "25px";
o4.value = 500;
o4.className = "o4";
document.getElementsByClassName("o3")[0].after(o4);
content =document.createElement("textarea");
content.setAttribute("id","content");
content.setAttribute("disabled","true");
content.style.width = "100%";
content.className = "content";
document.getElementsByClassName("o4")[0].after(content);
contentTemp =document.createElement("textarea");
contentTemp.setAttribute("id","contentTemp");
contentTemp.style.width = "1px";
contentTemp.style.height = "1px";
contentTemp.className = "contentTemp";
document.getElementsByClassName("content")[0].after(contentTemp);
leaf = 0;
count = 0;
countArea=document.createElement("span");
countArea.innerHTML = "当前爬取数量:" + count;
countArea.style.margin = "10.5px 0px 0px 15px";
countArea.className = "countArea";
document.getElementsByClassName("contentTemp")[0].after(countArea);
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";
o5.className = "o5";
document.getElementsByClassName("countArea")[0].after(o5);
}