mirror of
https://github.com/XIU2/UserScript.git
synced 2026-09-07 11:27:41 +00:00
新增 [直达问题按钮] 功能
This commit is contained in:
+69
-32
@@ -1,8 +1,8 @@
|
||||
// ==UserScript==
|
||||
// @name 知乎增强
|
||||
// @version 1.5.9
|
||||
// @version 1.6.0
|
||||
// @author X.I.U
|
||||
// @description 移除登录弹窗、默认收起回答、一键收起回答、收起当前回答/评论(点击两侧空白处)、快捷回到顶部(右键两侧空白处)、屏蔽用户 (发布的内容)、屏蔽关键词(标题/评论)、屏蔽盐选内容、展开问题描述、置顶显示时间、显示问题时间、区分问题文章、默认高清原图、默认站外直链
|
||||
// @description 移除登录弹窗、默认收起回答、一键收起回答、收起当前回答/评论(点击两侧空白处)、快捷回到顶部(右键两侧空白处)、屏蔽用户 (发布的内容)、屏蔽关键词(标题/评论)、屏蔽盐选内容、展开问题描述、置顶显示时间、显示问题时间、区分问题文章、直达问题按钮、默认高清原图、默认站外直链
|
||||
// @match *://www.zhihu.com/*
|
||||
// @match *://zhuanlan.zhihu.com/*
|
||||
// @icon https://static.zhihu.com/heifetz/favicon.ico
|
||||
@@ -36,6 +36,7 @@ var menu_ALL = [
|
||||
['menu_publishTop', '置顶显示时间', '置顶显示时间', true],
|
||||
['menu_allTime', '完整显示时间', '完整显示时间', true],
|
||||
['menu_typeTips', '区分问题文章', '区分问题文章', true],
|
||||
['menu_toQuestion', '直达问题按钮', '直达问题按钮', true],
|
||||
['menu_directLink', '默认站外直链', '默认站外直链', true]
|
||||
], menu_ID = [];
|
||||
for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
|
||||
@@ -851,20 +852,6 @@ function blockYanXuan() {
|
||||
// 区分问题文章
|
||||
function addTypeTips() {
|
||||
if (!menu_value('menu_typeTips')) return
|
||||
let patt_zhuanlan = /zhuanlan.zhihu.com/,
|
||||
patt_question = /question\/\d+/,
|
||||
patt_question_answer = /answer\/\d+/,
|
||||
patt_video = /\/zvideo\//,
|
||||
patt_tip = /zhihu_e_tips/;
|
||||
|
||||
const typeTips = (mutationsList, observer) => {
|
||||
for (const mutation of mutationsList) {
|
||||
for (const target of mutation.addedNodes) {
|
||||
if (target.nodeType != 1) return
|
||||
addTypeTips_(target.querySelector('h2.ContentItem-title a'));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 一开始加载的信息流
|
||||
if (location.pathname === '/search') {
|
||||
@@ -874,26 +861,73 @@ function addTypeTips() {
|
||||
}
|
||||
|
||||
// 后续加载的信息流
|
||||
const observer = new MutationObserver(typeTips);
|
||||
const observer = new MutationObserver(mutationsList => {
|
||||
for (const mutation of mutationsList) {
|
||||
for (const target of mutation.addedNodes) {
|
||||
if (target.nodeType != 1) return
|
||||
addTypeTips_(target.querySelector('h2.ContentItem-title a'));
|
||||
}
|
||||
}
|
||||
});
|
||||
observer.observe(document, { childList: true, subtree: true });
|
||||
|
||||
function addTypeTips_(titleA) {
|
||||
if (!titleA) return
|
||||
if (!patt_tip.test(titleA.innerHTML)) {
|
||||
if (patt_zhuanlan.test(titleA.href)) { // 如果是文章
|
||||
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
|
||||
} else if (patt_question.test(titleA.href)) { // 如果是问题
|
||||
if (!titleA.dataset.tooltip) { // 排除用户名后面的蓝标、黄标等链接
|
||||
if (patt_question_answer.test(titleA.href)) { // 如果是指向回答的问题(而非指向纯问题的链接)
|
||||
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
|
||||
} else {
|
||||
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
|
||||
}
|
||||
}
|
||||
} else if (patt_video.test(titleA.href)) { // 如果是视频
|
||||
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
|
||||
if (!titleA) return // 判断是否为真
|
||||
if (titleA.querySelector('small.zhihu_e_tips')) return // 判断是否已添加
|
||||
|
||||
let patt_zhuanlan = /zhuanlan.zhihu.com/,
|
||||
patt_question = /question\/\d+/,
|
||||
patt_question_answer = /answer\/\d+/,
|
||||
patt_video = /\/zvideo\//;
|
||||
if (patt_zhuanlan.test(titleA.href)) { // 如果是文章
|
||||
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
|
||||
} else if (patt_question.test(titleA.href)) { // 如果是问题
|
||||
if (!titleA.dataset.tooltip) { // 排除用户名后面的蓝标、黄标等链接
|
||||
if (patt_question_answer.test(titleA.href)) { // 如果是指向回答的问题(而非指向纯问题的链接)
|
||||
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
|
||||
} else {
|
||||
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
|
||||
}
|
||||
}
|
||||
} else if (patt_video.test(titleA.href)) { // 如果是视频
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 直达问题按钮
|
||||
function addToQuestion() {
|
||||
if (!menu_value('menu_toQuestion')) return
|
||||
|
||||
// 添加按钮样式
|
||||
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;}`;
|
||||
|
||||
// 一开始加载的信息流
|
||||
if (location.pathname === '/search') {
|
||||
setTimeout(function(){document.querySelectorAll('h2.ContentItem-title a').forEach(function(item){addTypeTips_(item);})}, 2000);
|
||||
} else {
|
||||
document.querySelectorAll('h2.ContentItem-title a').forEach(function(item){addTypeTips_(item);})
|
||||
}
|
||||
|
||||
// 后续加载的信息流
|
||||
const observer = new MutationObserver(mutationsList => {
|
||||
for (const mutation of mutationsList) {
|
||||
for (const target of mutation.addedNodes) {
|
||||
if (target.nodeType != 1) return
|
||||
addTypeTips_(target.querySelector('h2.ContentItem-title a'));
|
||||
}
|
||||
}
|
||||
});
|
||||
observer.observe(document, { childList: true, subtree: true });
|
||||
|
||||
function addTypeTips_(titleA) {
|
||||
if (!titleA) return // 判断是否为真
|
||||
if (titleA.parentElement.querySelector('a.zhihu_e_toQuestion')) return // 判断是否已添加
|
||||
if (/answer\/\d+/.test(titleA.href)) { // 如果是指向回答的问题(而非指向纯问题的链接)
|
||||
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>`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1274,6 +1308,7 @@ function questionInvitation(){
|
||||
collapsedNowAnswer('.Search-container'); // 收起当前回答 + 快捷返回顶部
|
||||
setInterval(topTime_search, 300); // 置顶显示时间
|
||||
addTypeTips(); // 区分问题文章
|
||||
addToQuestion(); // 直达问题按钮
|
||||
blockUsers('search'); // 屏蔽指定用户
|
||||
blockKeywords('search'); // 屏蔽指定关键词
|
||||
} else if (location.pathname.indexOf('/topic/') > -1) { // 话题页 //
|
||||
@@ -1282,6 +1317,7 @@ function questionInvitation(){
|
||||
collapsedNowAnswer('.ContentLayout'); // 收起当前回答 + 快捷返回顶部
|
||||
setInterval(topTime_people, 300); // 置顶显示时间
|
||||
addTypeTips(); // 区分问题文章
|
||||
addToQuestion(); // 直达问题按钮
|
||||
blockUsers('topic'); // 屏蔽指定用户
|
||||
blockKeywords('topic'); // 屏蔽指定关键词
|
||||
}
|
||||
@@ -1295,7 +1331,7 @@ function questionInvitation(){
|
||||
setInterval(topTime_zhuanlan, 300); // 置顶显示时间
|
||||
blockUsers(); // 屏蔽指定用户
|
||||
} else if (location.pathname.indexOf('/people/') > -1 || location.href.indexOf('org') > -1) { // 用户主页 //
|
||||
if (location.pathname.split('/').length === 3) addTypeTips(); // 区分问题文章
|
||||
if (location.pathname.split('/').length === 3) addTypeTips();addToQuestion(); // 区分问题文章、直达问题按钮
|
||||
collapsedNowAnswer('main div'); // 收起当前回答 + 快捷返回顶部
|
||||
collapsedNowAnswer('.Profile-main'); // 收起当前回答 + 快捷返回顶部
|
||||
setInterval(topTime_people, 300); // 置顶显示时间
|
||||
@@ -1306,6 +1342,7 @@ function questionInvitation(){
|
||||
collapsedNowAnswer('.Topstory-container'); // 收起当前回答 + 快捷返回顶部
|
||||
setInterval(topTime_index, 300); // 置顶显示时间
|
||||
addTypeTips(); // 区分问题文章
|
||||
addToQuestion(); // 直达问题按钮
|
||||
blockUsers('index'); // 屏蔽指定用户
|
||||
blockKeywords('index'); // 屏蔽指定关键词
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user