代码优化

This commit is contained in:
xing 2020-02-21 09:57:52 +08:00
parent f404a8634c
commit e263943603

View File

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name tieba page // @name tieba page
// @namespace http://tampermonkey.net/ // @namespace http://tampermonkey.net/
// @version 0.53 // @version 0.60
// @author fthvgb1 // @author fthvgb1
// @match https://tieba.baidu.com/* // @match https://tieba.baidu.com/*
// @grant none // @grant none
@ -9,20 +9,18 @@
// ==/UserScript== // ==/UserScript==
(function () { (function () {
'use strict'; 'use strict';
function gif3(v) { function gif3(v) {
var imgs = v.querySelectorAll('img.BDE_Image'); let imgs = v.querySelectorAll('img.BDE_Image');
if (imgs.length > 0) { if (imgs.length > 0) {
imgs.forEach(img => { imgs.forEach(img => {
var src = img.src; let src = img.src;
var s = /&src=(.*)/.exec(src); let s = /&src=(.*)/.exec(src);
if (s != null) { if (s != null) {
var x = s.length > 0 ? s[1] : src; let x = s.length > 0 ? s[1] : src;
img.src = decodeURIComponent(x); img.src = decodeURIComponent(x);
} }
@ -31,16 +29,14 @@
} }
function gif(v) { function gif(v) {
var imgs = v.querySelectorAll('div[data-class="BDE_Image"]'); let imgs = v.querySelectorAll('div[data-class="BDE_Image"]');
if (imgs.length > 0) { if (imgs.length > 0) {
imgs.forEach(value => { imgs.forEach(value => {
var src = decodeURIComponent(value.getAttribute('data-url')); let src = decodeURIComponent(value.getAttribute('data-url'));
let s = /&src=(.*)/.exec(src);
//console.log(src);
var s = /&src=(.*)/.exec(src);
if (s != null) { if (s != null) {
var ss = s[1]; let ss = s[1];
var img = document.createElement('img'); let img = document.createElement('img');
img.src = (ss); img.src = (ss);
img.className = 'BDE_Image'; img.className = 'BDE_Image';
value.outerHTML = img.outerHTML; value.outerHTML = img.outerHTML;
@ -55,7 +51,7 @@
$("ul#pblist>li").forEach(function (e, iii) { $("ul#pblist>li").forEach(function (e, iii) {
f(e); f(e);
if (iii === 0) { if (iii === 0) {
var oo = e.querySelectorAll('.pb_img_item'); let oo = e.querySelectorAll('.pb_img_item');
if (oo.length > 0) { if (oo.length > 0) {
oo.forEach(value => { oo.forEach(value => {
if (value.getAttribute('data-url')) { if (value.getAttribute('data-url')) {
@ -65,7 +61,7 @@
}); });
//oo[0].parentElement.outerHTML=`<span class="wrap pbimgwapper">${oo[0].parentElement.innerHTML}</span>` //oo[0].parentElement.outerHTML=`<span class="wrap pbimgwapper">${oo[0].parentElement.innerHTML}</span>`
} }
var zz = e.querySelector('#diversBanner'); let zz = e.querySelector('#diversBanner');
if (zz) { if (zz) {
zz.parentNode.removeChild(zz); zz.parentNode.removeChild(zz);
} }
@ -73,46 +69,46 @@
} }
gif(e); gif(e);
var ee = $(e); let ee = $(e);
var tid = ee.attr("tid"); let tid = ee.attr("tid");
var content = ee.find(".list_item_top"); let content = ee.find(".list_item_top");
var x = ee.find('.list_item_top a.j_report_btn'); let x = ee.find('.list_item_top a.j_report_btn');
if (x && x.length > 0) { if (x && x.length > 0) {
var kz = x[0].href.match(/tid=(\d+)&/); let kz = x[0].href.match(/tid=(\d+)&/);
kz = kz[1]; kz = kz[1];
} }
var floor = e.getElementsByClassName('pb_floow_load'); let floor = e.getElementsByClassName('pb_floow_load');
if (floor.length > 0) { if (floor.length > 0) {
var text = floor[0].textContent; let text = floor[0].textContent;
var url = `https://tieba.baidu.com/t/p/${tid}`; let url = `https://tieba.baidu.com/t/p/${tid}`;
var num = parseInt(text.match(/\d+/)); let num = parseInt(text.match(/\d+/));
content.append(`<div style="text-align:center;background-color: #eee;margin: 8px 0 0 42px;"><a style="padding:12px;display:block;" href="javascript:void(0)" data-url="${url}" class="reply">查看剩余` + num + `条回复</a></div>`); content.append(`<div style="text-align:center;background-color: #eee;margin: 8px 0 0 42px;"><a style="padding:12px;display:block;" href="javascript:void(0)" data-url="${url}" class="reply">查看剩余` + num + `条回复</a></div>`);
var res = content.find('a.reply'); let res = content.find('a.reply');
var orgnum = num; let orgnum = num;
//console.log(content,res); //console.log(content,res);
if (res) { if (res) {
res.forEach(function (v, i) { res.forEach(function (v, i) {
var page = 2; let page = 2;
v.addEventListener('click', function () { v.addEventListener('click', function () {
var that = this; let that = this;
if (num === orgnum) { if (num === orgnum) {
var url = this.getAttribute('data-url'); let url = this.getAttribute('data-url');
$.get(url, function (rst) { $.get(url, function (rst) {
var dom = (new DOMParser()).parseFromString(rst, 'text/html'); let dom = (new DOMParser()).parseFromString(rst, 'text/html');
var r = dom.querySelector('.j_floor_panel'); let r = dom.querySelector('.j_floor_panel');
var lii = r.querySelectorAll('li'); let lii = r.querySelectorAll('li');
lii.forEach(function (li, index) { lii.forEach(function (li, index) {
if (index < 2) { if (index < 2) {
return; return;
} }
var username = li.querySelector('.left>div .user_name').outerHTML; let username = li.querySelector('.left>div .user_name').outerHTML;
username = username.replace('</a>', ':</a>'); username = username.replace('</a>', ':</a>');
var s = li.querySelector('.content span'); let s = li.querySelector('.content span');
s.className = 'floor_content'; s.className = 'floor_content';
var c = li.querySelector('.content').innerHTML; let c = li.querySelector('.content').innerHTML;
var div = ` let div = `
<div class="fmain j_floor_main"> <div class="fmain j_floor_main">
<div class="floor_footer_item"> <div class="floor_footer_item">
${username} ${username}
@ -120,7 +116,7 @@
</div> </div>
</div>`; </div>`;
li.innerHTML = div; li.innerHTML = div;
var ll = document.createElement('li'); let ll = document.createElement('li');
ll.classList.add('list_item_floor'); ll.classList.add('list_item_floor');
ll.classList.add('j_list_item_floor'); ll.classList.add('j_list_item_floor');
ll.innerHTML = div; ll.innerHTML = div;
@ -136,17 +132,17 @@
} }
}); });
} else { } else {
var url = `https://tieba.baidu.com/mo/q//flr?fpn=${page}&kz=${kz}&pid=${tid}&is_ajax=1&has_url_param=0&template=lzl`; let url = `https://tieba.baidu.com/mo/q//flr?fpn=${page}&kz=${kz}&pid=${tid}&is_ajax=1&has_url_param=0&template=lzl`;
$.get(url, function (res) { $.get(url, function (res) {
var ht = (new DOMParser()).parseFromString(res.data.floor_html, 'text/html'); let ht = (new DOMParser()).parseFromString(res.data.floor_html, 'text/html');
var lii = ht.querySelectorAll('li'); let lii = ht.querySelectorAll('li');
lii.forEach(function (li, index) { lii.forEach(function (li, index) {
var username = li.querySelector('.left>div .user_name').outerHTML; let username = li.querySelector('.left>div .user_name').outerHTML;
username = username.replace('</a>', ':</a>'); username = username.replace('</a>', ':</a>');
var s = li.querySelector('.content span'); let s = li.querySelector('.content span');
s.className = 'floor_content'; s.className = 'floor_content';
var c = li.querySelector('.content').innerHTML; let c = li.querySelector('.content').innerHTML;
var div = ` let div = `
<div class="fmain j_floor_main"> <div class="fmain j_floor_main">
<div class="floor_footer_item"> <div class="floor_footer_item">
${username} ${username}
@ -154,7 +150,7 @@
</div> </div>
</div>`; </div>`;
li.innerHTML = div; li.innerHTML = div;
var ll = document.createElement('li'); let ll = document.createElement('li');
ll.classList.add('list_item_floor'); ll.classList.add('list_item_floor');
ll.classList.add('j_list_item_floor'); ll.classList.add('j_list_item_floor');
ll.innerHTML = div; ll.innerHTML = div;
@ -182,11 +178,11 @@
} }
function lz() { function lz() {
var lz = document.querySelector('span.poster_only'); let lz = document.querySelector('span.poster_only');
if (lz) { if (lz) {
lz.onclick = null; lz.onclick = null;
var h = location.href; let h = location.href;
var ff = 0; let ff = 0;
if (h.indexOf('see_lz=1') > -1) { if (h.indexOf('see_lz=1') > -1) {
lz.textContent = '取消只看楼主'; lz.textContent = '取消只看楼主';
h = h.replace('see_lz=1', 'see_lz=0'); h = h.replace('see_lz=1', 'see_lz=0');
@ -204,10 +200,10 @@
} }
function f(value) { function f(value) {
var dt = JSON.parse(value.getAttribute('data-info')); let dt = JSON.parse(value.getAttribute('data-info'));
if (dt) { if (dt) {
var fl = dt.floor_num; let fl = dt.floor_num;
var l = document.createElement('span'); let l = document.createElement('span');
l.style.color = 'green'; l.style.color = 'green';
l.textContent = fl + '楼'; l.textContent = fl + '楼';
if (fl === 1) { if (fl === 1) {
@ -222,10 +218,10 @@
} }
function check() { function check() {
var userAgentInfo = navigator.userAgent; let userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"]; let Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
var flag = 0; let flag = 0;
for (var v = 0; v < Agents.length; v++) { for (let v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > -1) { if (userAgentInfo.indexOf(Agents[v]) > -1) {
flag = 1; flag = 1;
break; break;
@ -238,18 +234,18 @@
[ [
'.frs_daoliu_for_app', '.tl_shadow_for_app_modle', '.footer_logo', '.footer_link_highlight' '.frs_daoliu_for_app', '.tl_shadow_for_app_modle', '.footer_logo', '.footer_link_highlight'
].forEach(value => { ].forEach(value => {
var x = document.querySelector(value); let x = document.querySelector(value);
if (x) { if (x) {
x.parentNode.removeChild(x) x.parentNode.removeChild(x)
} }
}); });
var ads = document.querySelectorAll('li.tl_shadow_for_app'); let ads = document.querySelectorAll('li.tl_shadow_for_app');
if (ads.length > 0) { if (ads.length > 0) {
var url = document.querySelector('.tl_shadow_for_app').parentNode.querySelector('a.j_common').href; let url = document.querySelector('.tl_shadow_for_app').parentNode.querySelector('a.j_common').href;
ads.forEach(v => { ads.forEach(v => {
//v.classList.remove('tl_shadow_for_app'); //v.classList.remove('tl_shadow_for_app');
var a = v.querySelector('a.j_enter_for_app'); let a = v.querySelector('a.j_enter_for_app');
var tid = v.getAttribute('data-tid'); let tid = v.getAttribute('data-tid');
a.href = url.replace(/\/(\d+)\?/.exec(url)[1], tid); a.href = url.replace(/\/(\d+)\?/.exec(url)[1], tid);
a.classList.remove('tl_shadow_for_app'); a.classList.remove('tl_shadow_for_app');
}) })
@ -271,7 +267,7 @@
'.img_desc', '.father-cut-recommend-normal-box', '.father-cut-daoliu-normal-box', '.img_desc', '.father-cut-recommend-normal-box', '.father-cut-daoliu-normal-box',
'#diversBanner', '.footer_logo', '.j_footer_link', '.frs_daoliu_for_app' '#diversBanner', '.footer_logo', '.j_footer_link', '.frs_daoliu_for_app'
].forEach(value => { ].forEach(value => {
var x = document.querySelector(value); let x = document.querySelector(value);
if (x) { if (x) {
x.parentNode.removeChild(x) x.parentNode.removeChild(x)
} }
@ -289,8 +285,12 @@
}) })
} }
if (check()) { try {
var url = location.href;
if (!check()) {
return;
}
let url = location.href;
if (/\/p\/\d+/.test(url) || /\/mo\/(.*)\/m\?kz=\d+/.test(url)) { if (/\/p\/\d+/.test(url) || /\/mo\/(.*)\/m\?kz=\d+/.test(url)) {
detail(); detail();
@ -298,6 +298,8 @@
if (/f\?kw=.+/.test(url) || /mo\/q\/m\?word=.+/.test(url) || /\/mo\/(.*)\/m\?kw=/.test(url)) { if (/f\?kw=.+/.test(url) || /mo\/q\/m\?word=.+/.test(url) || /\/mo\/(.*)\/m\?kw=/.test(url)) {
list(); list();
} }
}
} catch (e) {
console.log(e)
}
})(); })();