Skip to content

Commit 5d26536

Browse files
committed
修复 [区分问题文章]、[直达问题按钮] 功能在点击切换选项卡时不显示的问题; 优化 [区分问题文章] 标签样式
1 parent 33865ff commit 5d26536

1 file changed

Lines changed: 41 additions & 12 deletions

File tree

Zhihu-Enhanced.user.js

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name 知乎增强
3-
// @version 1.6.1
3+
// @version 1.6.2
44
// @author X.I.U
55
// @description 移除登录弹窗、默认收起回答、一键收起回答、收起当前回答/评论(点击两侧空白处)、快捷回到顶部(右键两侧空白处)、屏蔽用户 (发布的内容)、屏蔽关键词(标题/评论)、屏蔽盐选内容、展开问题描述、置顶显示时间、显示问题时间、区分问题文章、直达问题按钮、默认高清原图、默认站外直链
66
// @match *://www.zhihu.com/*
@@ -853,19 +853,22 @@ function blockYanXuan() {
853853
function addTypeTips() {
854854
if (!menu_value('menu_typeTips')) return
855855

856+
// 添加标签样式
857+
document.lastChild.appendChild(document.createElement('style')).textContent = `small.zhihu_e_tips {font-weight: bold;font-size: 13px;padding: 0 4px;border-radius: 2px;display: inline-block;height: 20px;}`;
858+
856859
// 一开始加载的信息流
857860
if (location.pathname === '/search') {
858-
setTimeout(function(){document.querySelectorAll('h2.ContentItem-title a').forEach(function(item){addTypeTips_(item);})}, 2000);
859-
} else {
860-
document.querySelectorAll('h2.ContentItem-title a').forEach(function(item){addTypeTips_(item);})
861+
addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_toQuestion)');
862+
} else {
863+
document.querySelectorAll('h2.ContentItem-title a:not(.zhihu_e_toQuestion)').forEach(function(item){addTypeTips_(item);})
861864
}
862865

863866
// 后续加载的信息流
864867
const observer = new MutationObserver(mutationsList => {
865868
for (const mutation of mutationsList) {
866869
for (const target of mutation.addedNodes) {
867870
if (target.nodeType != 1) return
868-
addTypeTips_(target.querySelector('h2.ContentItem-title a'));
871+
addTypeTips_(target.querySelector('h2.ContentItem-title a:not(.zhihu_e_toQuestion)'));
869872
}
870873
}
871874
});
@@ -874,10 +877,13 @@ function addTypeTips() {
874877
window.addEventListener('locationchange', function(){
875878
if (location.pathname.indexOf('/people/') > -1) {
876879
if (location.pathname.split('/').length === 3) {
880+
addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_toQuestion)');
877881
observer.observe(document, { childList: true, subtree: true });
878882
} else {
879883
observer.disconnect();
880884
}
885+
} else {
886+
addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_toQuestion)');
881887
}
882888
})
883889

@@ -890,19 +896,28 @@ function addTypeTips() {
890896
patt_question_answer = /answer\/\d+/,
891897
patt_video = /\/zvideo\//;
892898
if (patt_zhuanlan.test(titleA.href)) { // 如果是文章
893-
titleA.innerHTML = `<small class="zhihu_e_tips" style="color: #ffffff;font-weight: normal;font-size: 12px;padding: 0 3px;border-radius: 2px;background-color: #2196F3;display: inline-block;height: 18px;">文章</small> ` + titleA.innerHTML
899+
titleA.innerHTML = `<small class="zhihu_e_tips" style="color: #2196F3;background-color: #2196F333;">文章</small> ` + titleA.innerHTML
894900
} else if (patt_question.test(titleA.href)) { // 如果是问题
895901
if (!titleA.dataset.tooltip) { // 排除用户名后面的蓝标、黄标等链接
896902
if (patt_question_answer.test(titleA.href)) { // 如果是指向回答的问题(而非指向纯问题的链接)
897-
titleA.innerHTML = `<small class="zhihu_e_tips" style="color: #ffffff;font-weight: normal;font-size: 12px;padding: 0 3px;border-radius: 2px;background-color: #f68b83;display: inline-block;height: 18px;">问题</small> ` + titleA.innerHTML
903+
titleA.innerHTML = `<small class="zhihu_e_tips" style="color: #f68b83;background-color: #f68b8333;">问题</small> ` + titleA.innerHTML
898904
} else {
899-
titleA.innerHTML = `<small class="zhihu_e_tips" style="color: #ffffff;font-weight: normal;font-size: 12px;padding: 0 3px;border-radius: 2px;background-color: #ff5a4e;display: inline-block;height: 18px;">问题</small> ` + titleA.innerHTML
905+
titleA.innerHTML = `<small class="zhihu_e_tips" style="color: #ff5a4e;background-color: #ff5a4e33;">问题</small> ` + titleA.innerHTML
900906
}
901907
}
902908
} else if (patt_video.test(titleA.href)) { // 如果是视频
903-
titleA.innerHTML = `<small class="zhihu_e_tips" style="color: #ffffff;font-weight: normal;font-size: 12px;padding: 0 3px;border-radius: 2px;background-color: #00BCD4;display: inline-block;height: 18px;">视频</small> ` + titleA.innerHTML
909+
titleA.innerHTML = `<small class="zhihu_e_tips" style="color: #00BCD4;background-color: #00BCD433;">视频</small> ` + titleA.innerHTML
904910
}
911+
}
905912

913+
function addSetInterval_(A) {
914+
let timer = setInterval(function(){
915+
let aTag = document.querySelectorAll(A);
916+
if (aTag.length > 0) {
917+
clearInterval(timer);
918+
aTag.forEach(function(item){addTypeTips_(item);})
919+
}
920+
});
906921
}
907922
}
908923

@@ -916,29 +931,43 @@ function addToQuestion() {
916931

917932
// 一开始加载的信息流
918933
if (location.pathname === '/search') {
919-
setTimeout(function(){document.querySelectorAll('h2.ContentItem-title a').forEach(function(item){addTypeTips_(item);})}, 2000);
934+
addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_tips)');
920935
} else {
921-
document.querySelectorAll('h2.ContentItem-title a').forEach(function(item){addTypeTips_(item);})
936+
document.querySelectorAll('h2.ContentItem-title a:not(.zhihu_e_tips)').forEach(function(item){addTypeTips_(item);})
922937
}
923938

924939
// 后续加载的信息流
925940
const observer = new MutationObserver(mutationsList => {
926941
for (const mutation of mutationsList) {
927942
for (const target of mutation.addedNodes) {
928943
if (target.nodeType != 1) return
929-
addTypeTips_(target.querySelector('h2.ContentItem-title a'));
944+
addTypeTips_(target.querySelector('h2.ContentItem-title a:not(.zhihu_e_tips)'));
930945
}
931946
}
932947
});
933948
observer.observe(document, { childList: true, subtree: true });
934949

950+
window.addEventListener('locationchange', function(){
951+
addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_tips)');
952+
})
953+
935954
function addTypeTips_(titleA) {
936955
if (!titleA) return // 判断是否为真
937956
if (titleA.parentElement.querySelector('a.zhihu_e_toQuestion')) return // 判断是否已添加
938957
if (/answer\/\d+/.test(titleA.href)) { // 如果是指向回答的问题(而非指向纯问题的链接)
939958
titleA.insertAdjacentHTML('afterend', `<a class="zhihu_e_toQuestion VoteButton" href="${titleA.parentElement.querySelector('meta[itemprop="url"]').content}" target="_blank"><span style="display: inline-flex; align-items: center;">​<svg class="Zi Zi--TriangleUp VoteButton-TriangleUp zhihu_e_toQuestion" fill="currentColor" viewBox="0 0 24 24" width="10" height="10"><path d="M2 18.242c0-.326.088-.532.237-.896l7.98-13.203C10.572 3.57 11.086 3 12 3c.915 0 1.429.571 1.784 1.143l7.98 13.203c.15.364.236.57.236.896 0 1.386-.875 1.9-1.955 1.9H3.955c-1.08 0-1.955-.517-1.955-1.9z" fill-rule="evenodd"></path></svg></span>直达问题</a>`);
940959
}
941960
}
961+
962+
function addSetInterval_(A) {
963+
let timer = setInterval(function(){
964+
let aTag = document.querySelectorAll(A);
965+
if (aTag.length > 0) {
966+
clearInterval(timer);
967+
aTag.forEach(function(item){addTypeTips_(item);})
968+
}
969+
});
970+
}
942971
}
943972

944973

0 commit comments

Comments
 (0)