Skip to content

Commit b5c091f

Browse files
committed
新增 [直达问题按钮] 功能
1 parent 94137d5 commit b5c091f

1 file changed

Lines changed: 64 additions & 27 deletions

File tree

Zhihu-Enhanced.user.js

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// ==UserScript==
22
// @name 知乎增强
3-
// @version 1.5.9
3+
// @version 1.6.0
44
// @author X.I.U
5-
// @description 移除登录弹窗、默认收起回答、一键收起回答、收起当前回答/评论(点击两侧空白处)、快捷回到顶部(右键两侧空白处)、屏蔽用户 (发布的内容)、屏蔽关键词(标题/评论)、屏蔽盐选内容、展开问题描述、置顶显示时间、显示问题时间、区分问题文章、默认高清原图、默认站外直链
5+
// @description 移除登录弹窗、默认收起回答、一键收起回答、收起当前回答/评论(点击两侧空白处)、快捷回到顶部(右键两侧空白处)、屏蔽用户 (发布的内容)、屏蔽关键词(标题/评论)、屏蔽盐选内容、展开问题描述、置顶显示时间、显示问题时间、区分问题文章、直达问题按钮、默认高清原图、默认站外直链
66
// @match *://www.zhihu.com/*
77
// @match *://zhuanlan.zhihu.com/*
88
// @icon https://static.zhihu.com/heifetz/favicon.ico
@@ -36,6 +36,7 @@ var menu_ALL = [
3636
['menu_publishTop', '置顶显示时间', '置顶显示时间', true],
3737
['menu_allTime', '完整显示时间', '完整显示时间', true],
3838
['menu_typeTips', '区分问题文章', '区分问题文章', true],
39+
['menu_toQuestion', '直达问题按钮', '直达问题按钮', true],
3940
['menu_directLink', '默认站外直链', '默认站外直链', true]
4041
], menu_ID = [];
4142
for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
@@ -851,20 +852,57 @@ function blockYanXuan() {
851852
// 区分问题文章
852853
function addTypeTips() {
853854
if (!menu_value('menu_typeTips')) return
854-
let patt_zhuanlan = /zhuanlan.zhihu.com/,
855-
patt_question = /question\/\d+/,
856-
patt_question_answer = /answer\/\d+/,
857-
patt_video = /\/zvideo\//,
858-
patt_tip = /zhihu_e_tips/;
859855

860-
const typeTips = (mutationsList, observer) => {
856+
// 一开始加载的信息流
857+
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+
}
862+
863+
// 后续加载的信息流
864+
const observer = new MutationObserver(mutationsList => {
861865
for (const mutation of mutationsList) {
862866
for (const target of mutation.addedNodes) {
863867
if (target.nodeType != 1) return
864868
addTypeTips_(target.querySelector('h2.ContentItem-title a'));
865869
}
866870
}
867-
};
871+
});
872+
observer.observe(document, { childList: true, subtree: true });
873+
874+
function addTypeTips_(titleA) {
875+
if (!titleA) return // 判断是否为真
876+
if (titleA.querySelector('small.zhihu_e_tips')) return // 判断是否已添加
877+
878+
let patt_zhuanlan = /zhuanlan.zhihu.com/,
879+
patt_question = /question\/\d+/,
880+
patt_question_answer = /answer\/\d+/,
881+
patt_video = /\/zvideo\//;
882+
if (patt_zhuanlan.test(titleA.href)) { // 如果是文章
883+
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
884+
} else if (patt_question.test(titleA.href)) { // 如果是问题
885+
if (!titleA.dataset.tooltip) { // 排除用户名后面的蓝标、黄标等链接
886+
if (patt_question_answer.test(titleA.href)) { // 如果是指向回答的问题(而非指向纯问题的链接)
887+
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
888+
} else {
889+
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
890+
}
891+
}
892+
} else if (patt_video.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: #00BCD4;display: inline-block;height: 18px;">视频</small> ` + titleA.innerHTML
894+
}
895+
896+
}
897+
}
898+
899+
900+
// 直达问题按钮
901+
function addToQuestion() {
902+
if (!menu_value('menu_toQuestion')) return
903+
904+
// 添加按钮样式
905+
document.lastChild.appendChild(document.createElement('style')).textContent = `svg.zhihu_e_toQuestion {-webkit-transform: rotate(270deg);transform: rotate(270deg);} a.zhihu_e_toQuestion {font-size: 14px;font-weight: normal;padding: 3px 5px;margin-left: 3px;border-radius: 3px;}`;
868906

869907
// 一开始加载的信息流
870908
if (location.pathname === '/search') {
@@ -874,26 +912,22 @@ function addTypeTips() {
874912
}
875913

876914
// 后续加载的信息流
877-
const observer = new MutationObserver(typeTips);
915+
const observer = new MutationObserver(mutationsList => {
916+
for (const mutation of mutationsList) {
917+
for (const target of mutation.addedNodes) {
918+
if (target.nodeType != 1) return
919+
addTypeTips_(target.querySelector('h2.ContentItem-title a'));
920+
}
921+
}
922+
});
878923
observer.observe(document, { childList: true, subtree: true });
879924

880925
function addTypeTips_(titleA) {
881-
if (!titleA) return
882-
if (!patt_tip.test(titleA.innerHTML)) {
883-
if (patt_zhuanlan.test(titleA.href)) { // 如果是文章
884-
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
885-
} else if (patt_question.test(titleA.href)) { // 如果是问题
886-
if (!titleA.dataset.tooltip) { // 排除用户名后面的蓝标、黄标等链接
887-
if (patt_question_answer.test(titleA.href)) { // 如果是指向回答的问题(而非指向纯问题的链接)
888-
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
889-
} else {
890-
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
891-
}
892-
}
893-
} else if (patt_video.test(titleA.href)) { // 如果是视频
894-
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
895-
}
896-
}
926+
if (!titleA) return // 判断是否为真
927+
if (titleA.parentElement.querySelector('a.zhihu_e_toQuestion')) return // 判断是否已添加
928+
if (/answer\/\d+/.test(titleA.href)) { // 如果是指向回答的问题(而非指向纯问题的链接)
929+
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>`);
930+
}
897931
}
898932
}
899933

@@ -1274,6 +1308,7 @@ function questionInvitation(){
12741308
collapsedNowAnswer('.Search-container'); // 收起当前回答 + 快捷返回顶部
12751309
setInterval(topTime_search, 300); // 置顶显示时间
12761310
addTypeTips(); // 区分问题文章
1311+
addToQuestion(); // 直达问题按钮
12771312
blockUsers('search'); // 屏蔽指定用户
12781313
blockKeywords('search'); // 屏蔽指定关键词
12791314
} else if (location.pathname.indexOf('/topic/') > -1) { // 话题页 //
@@ -1282,6 +1317,7 @@ function questionInvitation(){
12821317
collapsedNowAnswer('.ContentLayout'); // 收起当前回答 + 快捷返回顶部
12831318
setInterval(topTime_people, 300); // 置顶显示时间
12841319
addTypeTips(); // 区分问题文章
1320+
addToQuestion(); // 直达问题按钮
12851321
blockUsers('topic'); // 屏蔽指定用户
12861322
blockKeywords('topic'); // 屏蔽指定关键词
12871323
}
@@ -1295,7 +1331,7 @@ function questionInvitation(){
12951331
setInterval(topTime_zhuanlan, 300); // 置顶显示时间
12961332
blockUsers(); // 屏蔽指定用户
12971333
} else if (location.pathname.indexOf('/people/') > -1 || location.href.indexOf('org') > -1) { // 用户主页 //
1298-
if (location.pathname.split('/').length === 3) addTypeTips(); // 区分问题文章
1334+
if (location.pathname.split('/').length === 3) addTypeTips();addToQuestion(); // 区分问题文章、直达问题按钮
12991335
collapsedNowAnswer('main div'); // 收起当前回答 + 快捷返回顶部
13001336
collapsedNowAnswer('.Profile-main'); // 收起当前回答 + 快捷返回顶部
13011337
setInterval(topTime_people, 300); // 置顶显示时间
@@ -1306,6 +1342,7 @@ function questionInvitation(){
13061342
collapsedNowAnswer('.Topstory-container'); // 收起当前回答 + 快捷返回顶部
13071343
setInterval(topTime_index, 300); // 置顶显示时间
13081344
addTypeTips(); // 区分问题文章
1345+
addToQuestion(); // 直达问题按钮
13091346
blockUsers('index'); // 屏蔽指定用户
13101347
blockKeywords('index'); // 屏蔽指定关键词
13111348
}

0 commit comments

Comments
 (0)