Skip to content

Commit 200e25f

Browse files
committed
优化 代码
1 parent 67a6efb commit 200e25f

1 file changed

Lines changed: 58 additions & 139 deletions

File tree

Zhihu-Enhanced.user.js

Lines changed: 58 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name 知乎增强
3-
// @version 1.8.8
3+
// @version 1.8.9
44
// @author X.I.U
55
// @description 移除登录弹窗、屏蔽首页视频、默认收起回答、快捷收起当前回答/评论(左键两侧空白处)、快捷回到顶部(右键两侧空白处)、屏蔽用户 (发布的内容)、屏蔽关键词(标题/评论)、移除高亮链接、屏蔽盐选内容、净化标题消息、展开问题描述、置顶显示时间、完整问题时间、区分问题文章、直达问题按钮、默认高清原图、默认站外直链
66
// @match *://www.zhihu.com/*
@@ -384,6 +384,7 @@ function isElementInViewport_(el) {
384384
function customBlockUsers() {
385385
let nowBlockUsers = '';
386386
menu_value('menu_customBlockUsers').forEach(function(item){nowBlockUsers += '|' + item})
387+
//console.log(nowBlockUsers.replace('|',''))
387388
let newBlockUsers = prompt('编辑 [自定义屏蔽用户]\n(不同用户名之间使用 "|" 分隔,例如:用户A|用户B|用户C )', nowBlockUsers.replace('|',''));
388389
if (newBlockUsers === '') {
389390
GM_setValue('menu_customBlockUsers', []);
@@ -1218,17 +1219,17 @@ function addLocationchange() {
12181219
}
12191220

12201221

1221-
// [完整显示时间 + 置顶显示时间] 功能修改自:https://greasyfork.org/scripts/402808(从 JQuery 改为原生 JavaScript,且优化了代码
1222+
// [完整显示时间 + 置顶显示时间] 功能修改自:https://greasyfork.org/scripts/402808(从 JQuery 改为原生 JavaScript,且精简、优化了代码
12221223
// 完整显示时间 + 置顶显示时间 - 首页
12231224
function topTime_index() {
12241225
let topTime = document.querySelectorAll('.TopstoryItem');if (!topTime) return
12251226
topTime.forEach(function(_this) {
1226-
let ContentItemTime = _this.querySelector('.ContentItem-time');if (!ContentItemTime) return
1227-
if (!(ContentItemTime.classList.contains('full')) && ContentItemTime.querySelector('span') && ContentItemTime.querySelector('span').innerText != null) {
1227+
let t = _this.querySelector('.ContentItem-time');if (!t) return
1228+
if (!(t.classList.contains('full')) && t.querySelector('span') && t.querySelector('span').innerText != null) {
12281229
// 完整显示时间
1229-
topTime_allTime(ContentItemTime)
1230+
topTime_allTime(t)
12301231
// 发布时间置顶
1231-
topTime_publishTop(ContentItemTime, _this, 'ContentItem-meta')
1232+
topTime_publishTop(t, _this, 'ContentItem-meta')
12321233
}
12331234
});
12341235
}
@@ -1238,24 +1239,19 @@ function topTime_index() {
12381239
function topTime_question() {
12391240
let topTime = document.querySelectorAll('.ContentItem.AnswerItem');if (!topTime) return
12401241
topTime.forEach(function(_this) {
1241-
let ContentItemTime = _this.querySelector('.ContentItem-time');if (!ContentItemTime) return
1242-
if (!(ContentItemTime.classList.contains('full')) && ContentItemTime.querySelector('span') && ContentItemTime.querySelector('span').innerText != null) {
1242+
let t = _this.querySelector('.ContentItem-time');if (!t) return
1243+
if (!(t.classList.contains('full')) && t.querySelector('span') && t.querySelector('span').innerText != null) {
12431244
// 完整显示时间
1244-
topTime_allTime(ContentItemTime)
1245+
topTime_allTime(t)
12451246
// 发布时间置顶
1246-
topTime_publishTop(ContentItemTime, _this, 'ContentItem-meta')
1247+
topTime_publishTop(t, _this, 'ContentItem-meta')
12471248
}
12481249

12491250
});
12501251

12511252
// 问题创建时间
1252-
if (!(document.querySelector('.QuestionPage .QuestionHeader-side p')) && location.href.indexOf("log") == -1) { // 没有执行过 且 非问题日志页
1253-
let createtime = document.querySelector('.QuestionPage>[itemprop~=dateCreated]').getAttribute('content');
1254-
let modifiedtime = document.querySelector('.QuestionPage>[itemprop~=dateModified]').getAttribute('content');
1255-
createtime = getUTC8(new Date(createtime));
1256-
modifiedtime = getUTC8(new Date(modifiedtime));
1257-
// 添加到问题页右上角
1258-
document.querySelector('.QuestionPage .QuestionHeader-side').insertAdjacentHTML('beforeEnd', '<div style=\"color:#8590a6; margin-top:15px\"><p>创建时间:&nbsp;&nbsp;' + createtime + '</p><p>最后编辑:&nbsp;&nbsp;' + modifiedtime + '</p></div>');
1253+
if (!(document.querySelector('.QuestionPage .QuestionHeader-side .QuestionTime-xiu')) && location.href.indexOf('/log') == -1) {
1254+
document.querySelector('.QuestionPage .QuestionHeader-side').insertAdjacentHTML('beforeEnd', '<div class="QuestionTime-xiu" style="color:#9098ac; margin-top:10px"><p>创建时间:' + getUTC8(new Date(document.querySelector('.QuestionPage > meta[itemprop=dateCreated]').content)) + '</p><p>最后编辑:' + getUTC8(new Date(document.querySelector('.QuestionPage > meta[itemprop=dateModified]').content)) + '</p></div>');
12591255
}
12601256
}
12611257

@@ -1264,12 +1260,12 @@ function topTime_question() {
12641260
function topTime_search() {
12651261
let topTime = document.querySelectorAll('.ContentItem.AnswerItem, .ContentItem.ArticleItem');if (!topTime) return
12661262
topTime.forEach(function(_this) {
1267-
let ContentItemTime = _this.querySelector('.ContentItem-time');if (!ContentItemTime) return
1268-
if (!(ContentItemTime.classList.contains('full')) && ContentItemTime.querySelector('span') && ContentItemTime.querySelector('span').innerText != null) {
1263+
let t = _this.querySelector('.ContentItem-time');if (!t) return
1264+
if (!(t.classList.contains('full')) && t.querySelector('span') && t.querySelector('span').innerText != null) {
12691265
// 完整显示时间
1270-
topTime_allTime(ContentItemTime)
1266+
topTime_allTime(t)
12711267
// 发布时间置顶
1272-
topTime_publishTop(ContentItemTime, _this, 'SearchItem-meta')
1268+
topTime_publishTop(t, _this, 'SearchItem-meta')
12731269
}
12741270

12751271
});
@@ -1280,12 +1276,12 @@ function topTime_search() {
12801276
function topTime_people() {
12811277
let topTime = document.querySelectorAll('.ContentItem.AnswerItem, .ContentItem.ArticleItem');if (!topTime) return
12821278
topTime.forEach(function(_this) {
1283-
let ContentItemTime = _this.querySelector('.ContentItem-time');if (!ContentItemTime) return
1284-
if (!(ContentItemTime.classList.contains('full')) && ContentItemTime.querySelector('span') && ContentItemTime.querySelector('span').innerText != null) {
1279+
let t = _this.querySelector('.ContentItem-time');if (!t) return
1280+
if (!(t.classList.contains('full')) && t.querySelector('span') && t.querySelector('span').innerText != null) {
12851281
// 完整显示时间
1286-
topTime_allTime(ContentItemTime)
1282+
topTime_allTime(t)
12871283
// 发布时间置顶
1288-
topTime_publishTop(ContentItemTime, _this, 'ContentItem-meta')
1284+
topTime_publishTop(t, _this, 'ContentItem-meta')
12891285
}
12901286

12911287
});
@@ -1294,22 +1290,22 @@ function topTime_people() {
12941290

12951291
// 完整显示时间 + 置顶显示时间 - 专栏/文章
12961292
function topTime_zhuanlan() {
1297-
let ContentItemTime = document.querySelector('.ContentItem-time');if (!ContentItemTime) return
1293+
let t = document.querySelector('.ContentItem-time');if (!t) return
12981294
// 完整显示时间
12991295
if (menu_value('menu_allTime')) {
1300-
if (ContentItemTime.innerText.indexOf('编辑于') > -1 && !(ContentItemTime.classList.contains('doneeeeee'))) {
1301-
let bianjiyu = ContentItemTime.innerText;
1302-
ContentItemTime.click();
1303-
ContentItemTime.innerText = (ContentItemTime.innerText + "," + bianjiyu)
1304-
ContentItemTime.classList.add("doneeeeee");
1296+
if (t.innerText.indexOf('编辑于') > -1 && !(t.classList.contains('xiu-time'))) {
1297+
let bianjiyu = t.innerText;
1298+
t.click();
1299+
t.innerText = (t.innerText + "," + bianjiyu)
1300+
t.classList.add('xiu-time');
13051301
}
13061302
}
13071303

13081304
//发布时间置顶
13091305
if (menu_value('menu_publishTop') && !(document.querySelector('.Post-Header > .ContentItem-time')) && !(document.querySelector('.ContentItem-meta > .ContentItem-time'))) {
1310-
ContentItemTime.style.cssText = 'padding:0px 0px 0px 0px; margin-top: 14px'
1311-
let temp_time = ContentItemTime.cloneNode(true);
1312-
// ContentItemTime.style.display = 'none';
1306+
t.style.cssText = 'padding:0px 0px 0px 0px; margin-top: 14px'
1307+
let temp_time = t.cloneNode(true);
1308+
// t.style.display = 'none';
13131309
if (location.href.indexOf('/column/') > -1){
13141310
document.querySelector('.ContentItem-meta').insertAdjacentElement('beforeEnd', temp_time);
13151311
} else {
@@ -1320,137 +1316,60 @@ function topTime_zhuanlan() {
13201316

13211317

13221318
// 完整显示时间
1323-
function topTime_allTime(ContentItemTime) {
1319+
function topTime_allTime(t) {
13241320
if (!menu_value('menu_allTime')) return
1325-
if (ContentItemTime.innerText.indexOf("发布于") == -1 && ContentItemTime.innerText.indexOf("编辑于") > -1) { //只有 "编辑于" 时增加具体发布时间 data-tooltip
1326-
let data_tooltip = ContentItemTime.querySelector('span').getAttribute('data-tooltip');
1327-
let oldtext = ContentItemTime.querySelector('span').innerText;
1328-
ContentItemTime.querySelector('span').innerText = data_tooltip + "," + oldtext;
1329-
ContentItemTime.classList.add('full');
1330-
} else if (ContentItemTime.innerText.indexOf("发布于") > -1 && ContentItemTime.innerText.indexOf("编辑于") == -1) { //只有 "发布于" 时替换为具体发布时间 data-tooltip
1331-
let data_tooltip = ContentItemTime.querySelector('span').getAttribute('data-tooltip');
1332-
ContentItemTime.querySelector('span').innerText = data_tooltip;
1333-
ContentItemTime.classList.add('full');
1321+
if (t.textContent.indexOf('发布于') > -1 && t.textContent.indexOf('编辑于') == -1) {
1322+
t.querySelector('span').textContent = (t.querySelector('span').dataset.tooltip);
1323+
t.classList.add('full');
1324+
} else if (t.textContent.indexOf('发布于') == -1 && t.textContent.indexOf('编辑于') > -1) {
1325+
t.querySelector('span').textContent = (t.querySelector('span').dataset.tooltip) + ' ,' + (t.querySelector('span').textContent);
1326+
t.classList.add('full');
13341327
}
13351328
}
13361329

13371330

13381331
// 发布时间置顶
1339-
function topTime_publishTop(ContentItemTime, _this, class_) {
1332+
function topTime_publishTop(t, _this, _class) {
13401333
if (!menu_value('menu_publishTop')) return
1341-
if (!ContentItemTime.parentNode.classList.contains(class_)) {
1342-
let temp_time = ContentItemTime.cloneNode(true);
1343-
//_this.querySelector('.RichContent .ContentItem-time').style.display = 'none';
1344-
_this.querySelector('.' + class_).insertAdjacentElement('beforeEnd', temp_time);
1334+
if (!t.parentNode.classList.contains(_class)) {
1335+
let temp_time = t.cloneNode(true);
1336+
_this.querySelector('.' + _class).insertAdjacentElement('beforeEnd', temp_time);
13451337
}
13461338
}
13471339

13481340

1349-
// UTC 标准时转 UTC+8 北京时间,来自:https://greasyfork.org/zh-CN/scripts/402808
1350-
function getUTC8(datetime) {
1351-
let month = (datetime.getMonth() + 1) < 10 ? "0" + (datetime.getMonth() + 1) : (datetime.getMonth() + 1);
1352-
let date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
1353-
let hours = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
1354-
let minutes = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
1355-
let seconds = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
1356-
return (datetime.getFullYear() + "-" + month + "-" + date + "\xa0\xa0" + hours + ":" + minutes + ":" + seconds);
1341+
// UTC 标准时转 UTC+8 北京时间,修改自:https://greasyfork.org/zh-CN/scripts/402808(精简)
1342+
function getUTC8(t) {
1343+
return (t.getFullYear() + '-' + (((t.getMonth() + 1) < 10) ? ('0' + (t.getMonth() + 1)) : (t.getMonth() + 1)) + '-' + ((t.getDate() < 10) ? ('0' + t.getDate()) : t.getDate()) + '\xa0\xa0' + ((t.getHours() < 10) ? ('0' + t.getHours()) : t.getHours()) + ':' + ((t.getMinutes() < 10) ? ('0' + t.getMinutes()) : t.getMinutes()) + ':' + ((t.getSeconds() < 10) ? ('0' + t.getSeconds()) : t.getSeconds()));
13571344
}
13581345

13591346

1360-
// 默认站外直链,修改自:https://greasyfork.org/scripts/402808(从 JQuery 改为原生 JavaScript)
1347+
// 默认站外直链,修改自:https://greasyfork.org/scripts/402808(从 JQuery 改为原生 JavaScript,且精简、优化了代码
13611348
function directLink () {
1362-
let link, equal, colon, externalHref, protocol, path, newHref;
1363-
// 文字链接
1364-
link = document.querySelectorAll('a[class*="external"]')
1365-
if (link) {
1366-
link.forEach(function (_this) {
1367-
if (_this.getElementsByTagName('span').length > 0) {
1368-
newHref = _this.innerText;
1369-
_this.setAttribute('href', newHref);
1370-
} else if (_this.href.indexOf("link.zhihu.com/?target=") > -1) {
1371-
externalHref = _this.href;
1372-
newHref = externalHref.substring(externalHref = _this.href.indexOf('link.zhihu.com/?target=') + 'link.zhihu.com/?target='.length);
1373-
_this.setAttribute('href', decodeURIComponent(newHref));
1374-
} else {
1375-
externalHref = _this.href;
1376-
if (externalHref.lastIndexOf("https%3A")) {
1377-
newHref = _this.href.substring(_this.href.lastIndexOf("https%3A"));
1378-
} else if (externalHref.lastIndexOf("http%3A%2F%2F")) {
1379-
newHref = _this.href.substring(_this.href.lastIndexOf("http%3A"));
1380-
}
1381-
_this.setAttribute('href', decodeURIComponent(newHref));
1382-
}
1383-
});
1384-
}
1385-
1386-
// 卡片链接
1387-
link = document.querySelectorAll('a[class*="LinkCard"]:not([class*="MCNLinkCard"]):not([class*="ZVideoLinkCard"])')
1388-
if (link) {
1389-
link.forEach(function (_this) {
1390-
if (_this.getElementsByTagName('LinkCard-title').length > 0 && _this.getElementsByTagName('LinkCard-title')[0].indexOf("http") > -1) {
1391-
newHref = _this.getElementsByTagName('LinkCard-title').innerText;
1392-
_this.setAttribute('href', newHref);
1393-
} else if (_this.href.indexOf("link.zhihu.com/?target=") > -1) {
1394-
externalHref = _this.href;
1395-
newHref = externalHref.substring(externalHref = _this.href.indexOf("link.zhihu.com/?target=") + "link.zhihu.com/?target=".length);
1396-
_this.setAttribute('href', decodeURIComponent(newHref));
1397-
} else {
1398-
externalHref = _this.href;
1399-
if (externalHref.lastIndexOf("https%3A")) {
1400-
newHref = _this.href.substring(_this.href.lastIndexOf("https%3A"));
1401-
} else if (externalHref.lastIndexOf("http%3A%2F%2F")) {
1402-
newHref = _this.href.substring(_this.href.lastIndexOf("http%3A"));
1403-
}
1404-
_this.setAttribute('href', decodeURIComponent(newHref));
1405-
}
1406-
});
1407-
}
1408-
1409-
// 旧版视频卡片链接
1410-
link = document.querySelectorAll('a.VideoCard-link')
1411-
if (link) {
1412-
link.forEach(function (_this) {
1413-
if (_this.href.indexOf('link.zhihu.com/?target=') > -1) {
1414-
externalHref = _this.href;
1415-
equal = externalHref.lastIndexOf('http');
1416-
colon = externalHref.lastIndexOf('%3A');
1417-
protocol = externalHref.substring(equal, colon);
1418-
path = externalHref.substring(colon + 5, externalHref.length);
1419-
newHref = protocol + '://' + path;
1420-
_this.setAttribute('href', decodeURIComponent(newHref));
1421-
}
1422-
});
1423-
}
1349+
document.querySelectorAll('a.external[href*="link.zhihu.com/?target="], a.LinkCard[href*="link.zhihu.com/?target="]:not(.MCNLinkCard):not(.ZVideoLinkCard):not(.ADLinkCardContainer)').forEach(function (_this) {_this.href = decodeURIComponent(_this.href.substring(_this.href.indexOf('link.zhihu.com/?target=') + 23));});
14241350
}
14251351

14261352

1427-
// 默认高清原图,修改自:https://greasyfork.org/scripts/402808(从 JQuery 改为原生 JavaScript)
1353+
// 默认高清原图,修改自:https://greasyfork.org/scripts/402808(从 JQuery 改为原生 JavaScript,且精简、优化了代码
14281354
function originalPic(){
1429-
let pic = document.getElementsByTagName('img');if (!pic) return
1430-
Array.from(pic).forEach(function(pic1){
1431-
if (pic1.getAttribute('data-original') != undefined && pic1.className != 'comment_sticker') {
1432-
if (pic1.getAttribute('src') != pic1.getAttribute('data-original')) {
1433-
pic1.setAttribute('src', pic1.getAttribute('data-original'))
1434-
}
1435-
}
1436-
});
1355+
document.querySelectorAll('img').forEach(function(one){if (one.dataset.original != undefined && one.src != one.dataset.original && one.className != 'comment_sticker') {one.src = one.dataset.original}});
14371356
}
14381357

14391358

1440-
// 默认折叠邀请,修改自:https://greasyfork.org/scripts/402808(从 JQuery 改为原生 JavaScript)
1359+
// 默认折叠邀请,修改自:https://greasyfork.org/scripts/402808(从 JQuery 改为原生 JavaScript,且精简、优化了代码
14411360
function questionInvitation(){
1442-
let timer = setInterval(function(){
1443-
let QuestionInvitation = document.querySelector('.QuestionInvitation-content');if (!QuestionInvitation) return
1444-
clearInterval(timer);
1445-
QuestionInvitation.style.display = 'none';
1446-
document.querySelector('.QuestionInvitation-title').innerHTML = document.querySelector('.QuestionInvitation-title').innerText + '<span style="color: #8590a6;font-size: 14px;"> 展开/折叠</span>'
1361+
let time = setInterval(function(){
1362+
let q = document.querySelector('.QuestionInvitation-content'); if (!q) return
1363+
clearInterval(time);
1364+
q.style.display = 'none';
1365+
document.querySelector('.QuestionInvitation-title').innerHTML = document.querySelector('.QuestionInvitation-title').innerText + '<span style="cursor: pointer; font-size: 14px; color: #919aae;"> 展开/折叠</span>'
14471366
// 点击事件(展开/折叠)
14481367
document.querySelector('.Topbar').onclick = function(){
1449-
let QuestionInvitation = document.querySelector('.QuestionInvitation-content')
1450-
if (QuestionInvitation.style.display == 'none') {
1451-
QuestionInvitation.style.display = ''
1368+
let q = document.querySelector('.QuestionInvitation-content')
1369+
if (q.style.display == 'none') {
1370+
q.style.display = ''
14521371
} else {
1453-
QuestionInvitation.style.display = 'none'
1372+
q.style.display = 'none'
14541373
}
14551374
}
14561375
});

0 commit comments

Comments
 (0)