// ==UserScript== // @name 知乎免登录 // @namespace https://www.zhihu.com/ // @version 0.10.2 // @description 去除知乎烦人的登录提示,安卓端免app浏览 // @author System // @match *://*.zhihu.com/* // @run-at document-start // @connect api.zhihu.com // @license GPL-3.0-only // @note 20220812-0.10.2 移除右下登录浮窗,自动折叠盐选内容 // @note 20220812-0.10.1 修复移动端评论区关闭按钮 // @note 20220114-0.10.0 去除链接重定向;去除奇怪的搜索词;解决部分按钮显示页面崩溃问题;解决油猴插件兼容性问题#113512;删除未使用的代码 // @note 20210707-0.9.9 去除移动端可能不支持的可选链语法,提高兼容性 // @note 20210706-0.9.8 解决iPad专栏无法滚动问题 // @note 20210706-0.9.7 添加移动端专栏防APP跳转;修复移动端专栏无法滚动问题;移除失效功能:隐私保护用户回答显示 // @note 20210706-0.8.6 添加高清图加载(触碰图片时触发);添加移动端搜索按钮 // @note 20210706-0.8.4 修复弹窗屏蔽 // @downloadURL https://update.greasyfork.icu/scripts/396171/%E7%9F%A5%E4%B9%8E%E5%85%8D%E7%99%BB%E5%BD%95.user.js // @updateURL https://update.greasyfork.icu/scripts/396171/%E7%9F%A5%E4%B9%8E%E5%85%8D%E7%99%BB%E5%BD%95.meta.js // ==/UserScript== (function () { "use strict"; const isMobile = /(Android|iPhone|iPad)/i.test(navigator.userAgent); const isZhuanlan = location.host.startsWith('zhuanlan'); const querySelectorAllAndSelf = (target, selector) => [...(target.matches(selector) ? [target] : []), ...target.querySelectorAll(selector)]; { new MutationObserver((events, observer) => events.forEach((e) => e.addedNodes.forEach((target) => { if (target && target.nodeType == 1) { [ () => { //屏蔽右下角弹窗 if(target.querySelector("button")?.innerHTML?.includes('登录')){ target.style.display = 'none'; } }, () =>{ //折叠盐选内容 if(target.matches('.List-item')&&target.querySelector(".KfeCollection-AnswerTopCard-Container")){ target.querySelector('.RichContent-collapsedText').click(); } }, () => { //屏蔽登录弹窗 if (target.querySelector(".signFlowModal")) { target.style.display = 'none'; document.documentElement.removeAttribute('style') target.querySelector(".Modal-closeButton").click(); } }, () => { //移除链接重定向 querySelectorAllAndSelf(target, 'a[href*="link.zhihu.com"]').forEach(link => { const match = /link\.zhihu\.com\/\?target=(.+)$/.exec(link.href); if (match) { link.href = decodeURIComponent(match[1]); } }); }, () => { //移除奇怪的搜索词 querySelectorAllAndSelf(target, 'a[href*="www.zhihu.com/search"]').forEach(link => { const svg = link.querySelector('svg'); if (svg) { link.removeAttribute('href'); link.removeAttribute('class'); svg.remove(); } }) } ].forEach(func => { try { func(); } catch (error) { console.error(error); } }); } }))).observe(document.documentElement, { childList: true, subtree: true }); } //END //简单模板渲染器 function render(temp, data) { // console.log(render,temp,data) const get = (value) => value .split(".") .reduce((prev, cur) => (prev ? prev[cur.trim()] : null), data); return temp .replace(/{%\s*if\s+(.*?)%}([\s\S]*?){%\s*fi\s*%}/gm, (match, v1, v2) => get(v1) ? v2 : "" ) .replace(/{{\s*(.+?)\s*}}/g, (match, value) => get(value)); } //主页推荐内容显示 function ReplaceHomePage() { const body_html = `
  • 首页
刷新
`; const Hotitem = `
{{index}}
{{target.title}}
{{target.excerpt}}
{{detail_text}}
{{target.title}}
`; if ( location.href.endsWith("://www.zhihu.com/") || location.href.includes("://www.zhihu.com/signin") ) { window.addEventListener("DOMContentLoaded", () => { document.body.innerHTML = body_html; let is_end = false, update = 0, loading = false; const target = document.querySelector("div.scroll"); const pad = (num, n) => (Array(n).join(0) + num).slice(-n); function load(reload) { update = new Date().getTime(); loading = true; const xhr = new XMLHttpRequest(); xhr.open( "GET", "https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=50&desktop=true" ); xhr.addEventListener("load", function () { const data = JSON.parse(this.response); is_end = data.paging.is_end; const parser = new DOMParser(); data.data .map((val, i) => ((val.index = pad(i + 1, 2)), val)) .map((val) => render(Hotitem, val)) .forEach((val) => { const doc = parser.parseFromString(val, "text/html"); target.append(doc.body.children[0]); }); loading = false; }); xhr.send(); } const scroll = () => { if (is_end || loading || Date.now() - update < 100 || target.scrollHeight - window.scrollY > 1000) { return; } load(); }; window.addEventListener("scroll", scroll); const reload = () => { [...target.children].forEach((e) => e.remove()); load(true); }; document .querySelector("img.icon-refresh") .addEventListener("click", reload); scroll(); }); } } //END //隐私保护用户信息显示 function NoHiddenUser() { window.addEventListener('click', e => e.target.nodeName == "SPAN" && e.target.classList.contains('UserLink') && window.open('https://www.zhihu.com/people/' + JSON.parse(e.path.find(e => e.classList.contains('AnswerItem')).dataset.zaExtraModule).card.content.author_member_hash_id, '_blank')); window.addEventListener('load', e => { const css = document.createElement('style'); css.innerHTML = `span.UserLink{cursor: pointer;}`; document.head.append(css); }); } //END function AddSearchButton() { window.addEventListener("load", () => { if (location.pathname == '/search') return; const node = document.querySelector('.MobileAppHeader-actions'); if (!node) return; const link = document.createElement('a'); link.href = '/search?type=content&q='; link.className = "MobileAppHeader-downloadLink"; link.innerHTML = "搜索"; node.insertAdjacentElement('afterBegin', link); }); } function ChangeUA() { Object.defineProperties((unsafeWindow || window).navigator, { userAgent: { value: "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1" }, platform: { value: 'Mac' } }); } function RawImageLoader() { document.addEventListener('pointerdown', e => e.target.nodeName == 'IMG' && e.target.dataset.original && (e.target.src = e.target.dataset.original)) } function ZhuanlanNoApp() { document.addEventListener('click', e => e.path.find(e => e.classList && e.classList.contains('Post-Sub')) && e.stopPropagation(), true); } function NoCopyBlocker() { document.addEventListener('copy', e => e.stopImmediatePropagation(), true); } //修复移动端关闭按钮消失 function FixCloseButton() { window.addEventListener('load', e => { const css = document.createElement('style'); css.innerHTML = `[aria-label="关闭"]{right:0 !important;top:2em !important;} [aria-label="关闭"]>svg{fill:black;} div{max-width:100%;}`; document.head.append(css); }); } //END if (isMobile) { FixCloseButton(); AddSearchButton(); if (isZhuanlan) { ZhuanlanNoApp() } else { ChangeUA(); } } else { ReplaceHomePage(); } NoHiddenUser(); RawImageLoader(); NoCopyBlocker(); })();