11// ==UserScript==
22// @name 知乎增强
3- // @version 1.4.7
3+ // @version 1.4.8
44// @author X.I.U
55// @description 移除登录弹窗、一键收起回答、收起当前回答/评论(点击两侧空白处)、快捷回到顶部(右键两侧空白处)、屏蔽指定用户、屏蔽指定关键词(标题)、屏蔽盐选内容、置顶显示时间、显示问题时间、区分问题文章、默认高清原图、默认站外直链
66// @match *://www.zhihu.com/*
77// @match *://zhuanlan.zhihu.com/*
88// @icon https://static.zhihu.com/heifetz/favicon.ico
9+ // @grant GM_xmlhttpRequest
910// @grant GM_registerMenuCommand
1011// @grant GM_unregisterMenuCommand
1112// @grant GM_openInTab
@@ -218,7 +219,7 @@ function isElementInViewport(el) {
218219// 自定义屏蔽用户
219220function customBlockUsers ( ) {
220221 let nowBlockUsers = '' ;
221- menu_value ( 'menu_customBlockUsers' ) . forEach ( function ( item ) { nowBlockUsers = nowBlockUsers + '|' + item } )
222+ menu_value ( 'menu_customBlockUsers' ) . forEach ( function ( item ) { nowBlockUsers += '|' + item } )
222223 let newBlockUsers = prompt ( '编辑 [自定义屏蔽用户]\n(不同用户名之间使用 "|" 分隔,例如:用户A|用户B|用户C )' , nowBlockUsers . replace ( '|' , '' ) ) ;
223224 if ( newBlockUsers === '' ) {
224225 GM_setValue ( 'menu_customBlockUsers' , [ ] ) ;
@@ -353,10 +354,12 @@ function blockUsers(type) {
353354
354355 // 添加屏蔽用户按钮(点赞、回复等按钮后面)
355356 if ( item1 ) {
356- let footer = item1 . parentNode . parentNode . parentNode . parentNode . parentNode . parentNode . querySelector ( '.CommentItemV2-metaSibling > .CommentItemV2-footer' ) ;
357- if ( footer && ! footer . lastElementChild . dataset . name ) {
358- footer . insertAdjacentHTML ( 'beforeend' , `<button type="button" data-name="${ item1 . alt } " class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" fill="currentColor" viewBox="0 0 24 24" width="16" height="16" style="transform: rotate(180deg); margin-right: 5px;"><path d="M18.376 5.624c-3.498-3.499-9.254-3.499-12.752 0-3.499 3.498-3.499 9.254 0 12.752 3.498 3.499 9.254 3.499 12.752 0 3.499-3.498 3.499-9.14 0-12.752zm-1.693 1.693c2.37 2.37 2.596 6.094.678 8.69l-9.367-9.48c2.708-1.919 6.32-1.58 8.69.79zm-9.48 9.48c-2.37-2.37-2.595-6.095-.676-8.69l9.48 9.48c-2.822 1.918-6.433 1.58-8.803-.79z" fill-rule="evenodd"></path></svg></span>屏蔽用户</button>` ) ;
359- footer . lastElementChild . onclick = function ( ) { blockUsers_button_add ( this . dataset . name ) }
357+ let footer = item1 . parentNode . parentNode . parentNode . parentNode . parentNode . parentNode . querySelector ( '.CommentItemV2-metaSibling > .CommentItemV2-footer' ) ,
358+ userid = item1 . parentNode ;
359+ if ( userid && footer && ! footer . lastElementChild . dataset . name ) {
360+ userid = userid . href . split ( '/' ) [ 4 ] ;
361+ footer . insertAdjacentHTML ( 'beforeend' , `<button type="button" data-name="${ item1 . alt } " data-userid="${ userid } " class="Button CommentItemV2-hoverBtn Button--plain"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Like" fill="currentColor" viewBox="0 0 24 24" width="16" height="16" style="transform: rotate(180deg); margin-right: 5px;"><path d="M18.376 5.624c-3.498-3.499-9.254-3.499-12.752 0-3.499 3.498-3.499 9.254 0 12.752 3.498 3.499 9.254 3.499 12.752 0 3.499-3.498 3.499-9.14 0-12.752zm-1.693 1.693c2.37 2.37 2.596 6.094.678 8.69l-9.367-9.48c2.708-1.919 6.32-1.58 8.69.79zm-9.48 9.48c-2.37-2.37-2.595-6.095-.676-8.69l9.48 9.48c-2.822 1.918-6.433 1.58-8.803-.79z" fill-rule="evenodd"></path></svg></span>屏蔽用户</button>` ) ;
362+ footer . lastElementChild . onclick = function ( ) { blockUsers_button_add ( this . dataset . name , this . dataset . userid , false ) }
360363 }
361364 }
362365 } )
@@ -370,9 +373,11 @@ function blockUsers(type) {
370373 let blockUsers = e => {
371374 if ( e . target . innerHTML && e . target . querySelector ( '.MemberButtonGroup.ProfileButtonGroup.HoverCard-buttons' ) ) {
372375 let item = e . target . querySelector ( '.MemberButtonGroup.ProfileButtonGroup.HoverCard-buttons' ) ,
373- name = item . parentNode . parentNode . querySelector ( 'a.UserLink-link' ) . innerText ;
374- item . insertAdjacentHTML ( 'beforeend' , `<button type="button" data-name="${ name } " class="Button FollowButton Button--primary Button--red" style="width: 100%;margin: 7px 0 0 0;"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Plus FollowButton-icon" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><path d="M18.376 5.624c-3.498-3.499-9.254-3.499-12.752 0-3.499 3.498-3.499 9.254 0 12.752 3.498 3.499 9.254 3.499 12.752 0 3.499-3.498 3.499-9.14 0-12.752zm-1.693 1.693c2.37 2.37 2.596 6.094.678 8.69l-9.367-9.48c2.708-1.919 6.32-1.58 8.69.79zm-9.48 9.48c-2.37-2.37-2.595-6.095-.676-8.69l9.48 9.48c-2.822 1.918-6.433 1.58-8.803-.79z" fill-rule="evenodd"></path></svg></span>屏蔽用户</button>` ) ;
375- item . lastElementChild . onclick = function ( ) { blockUsers_button_add ( this . dataset . name ) }
376+ item1 = item . parentNode . parentNode . querySelector ( 'a.UserLink-link' ) ,
377+ name = item1 . innerText ,
378+ userid = item1 . href . split ( '/' ) [ 4 ] ;
379+ item . insertAdjacentHTML ( 'beforeend' , `<button type="button" data-name="${ item1 . innerText } " data-userid="${ userid } " class="Button FollowButton Button--primary Button--red" style="width: 100%;margin: 7px 0 0 0;"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Plus FollowButton-icon" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><path d="M18.376 5.624c-3.498-3.499-9.254-3.499-12.752 0-3.499 3.498-3.499 9.254 0 12.752 3.498 3.499 9.254 3.499 12.752 0 3.499-3.498 3.499-9.14 0-12.752zm-1.693 1.693c2.37 2.37 2.596 6.094.678 8.69l-9.367-9.48c2.708-1.919 6.32-1.58 8.69.79zm-9.48 9.48c-2.37-2.37-2.595-6.095-.676-8.69l9.48 9.48c-2.822 1.918-6.433 1.58-8.803-.79z" fill-rule="evenodd"></path></svg></span>屏蔽用户</button>` ) ;
380+ item . lastElementChild . onclick = function ( ) { blockUsers_button_add ( this . dataset . name , this . dataset . userid , false ) }
376381 }
377382 }
378383 document . addEventListener ( 'DOMNodeInserted' , blockUsers ) ; // 监听插入事件
@@ -382,40 +387,81 @@ function blockUsers(type) {
382387 function blockUsers_button_people ( ) {
383388 let item = document . querySelector ( '.MemberButtonGroup.ProfileButtonGroup.ProfileHeader-buttons' ) , // 获取按钮元素位置
384389 name = document . querySelector ( '.ProfileHeader-name' ) . firstChild . textContent , // 获取用户名
385- users = menu_value ( 'menu_customBlockUsers' ) ; // 读取屏蔽列表
390+ users = menu_value ( 'menu_customBlockUsers' ) , // 读取屏蔽列表
391+ userid = location . href . split ( '/' ) [ 4 ] ;
386392 for ( let num = 0 ; num < users . length ; num ++ ) { // 判断是否已存在
387- if ( users [ num ] === name ) {
388- item . insertAdjacentHTML ( 'beforeend' , `<button type="button" data-name="${ name } " disabled class="Button FollowButton Button--primary Button--red" style="margin: 0 0 0 12px;"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Plus FollowButton-icon" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><path d="M18.376 5.624c-3.498-3.499-9.254-3.499-12.752 0-3.499 3.498-3.499 9.254 0 12.752 3.498 3.499 9.254 3.499 12.752 0 3.499-3.498 3.499-9.14 0-12.752zm-1.693 1.693c2.37 2.37 2.596 6.094.678 8.69l-9.367-9.48c2.708-1.919 6.32-1.58 8.69.79zm-9.48 9.48c-2.37-2.37-2.595-6.095-.676-8.69l9.48 9.48c-2.822 1.918-6.433 1.58-8.803-.79z" fill-rule="evenodd"></path></svg></span>已屏蔽</button>` ) ;
393+ if ( users [ num ] === name ) { // 已存在
394+ document . querySelectorAll ( '.Button.Button--primary.Button--red' ) . forEach ( function ( item ) { item . style . display = 'none' ; } ) // 隐藏知乎自带的已屏蔽按钮
395+ item . insertAdjacentHTML ( 'beforeend' , `<button type="button" data-name="${ name } " data-userid="${ userid } " class="Button FollowButton Button--primary Button--red" style="margin: 0 0 0 12px;"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Plus FollowButton-icon" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><path d="M18.376 5.624c-3.498-3.499-9.254-3.499-12.752 0-3.499 3.498-3.499 9.254 0 12.752 3.498 3.499 9.254 3.499 12.752 0 3.499-3.498 3.499-9.14 0-12.752zm-1.693 1.693c2.37 2.37 2.596 6.094.678 8.69l-9.367-9.48c2.708-1.919 6.32-1.58 8.69.79zm-9.48 9.48c-2.37-2.37-2.595-6.095-.676-8.69l9.48 9.48c-2.822 1.918-6.433 1.58-8.803-.79z" fill-rule="evenodd"></path></svg></span>取消屏蔽</button>` ) ;
396+ item . lastElementChild . onclick = function ( ) { blockUsers_button_del ( this . dataset . name , this . dataset . userid , true ) }
389397 return
390398 }
391399 } ;
392400 if ( item ) {
393- item . insertAdjacentHTML ( 'beforeend' , `<button type="button" data-name="${ name } " class="Button FollowButton Button--primary Button--red" style="margin: 0 0 0 12px;"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Plus FollowButton-icon" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><path d="M18.376 5.624c-3.498-3.499-9.254-3.499-12.752 0-3.499 3.498-3.499 9.254 0 12.752 3.498 3.499 9.254 3.499 12.752 0 3.499-3.498 3.499-9.14 0-12.752zm-1.693 1.693c2.37 2.37 2.596 6.094.678 8.69l-9.367-9.48c2.708-1.919 6.32-1.58 8.69.79zm-9.48 9.48c-2.37-2.37-2.595-6.095-.676-8.69l9.48 9.48c-2.822 1.918-6.433 1.58-8.803-.79z" fill-rule="evenodd"></path></svg></span>屏蔽用户</button>` ) ;
394- item . lastElementChild . onclick = function ( ) { blockUsers_button_add ( this . dataset . name ) }
401+ item . insertAdjacentHTML ( 'beforeend' , `<button type="button" data-name="${ name } " data-userid=" ${ userid } " class="Button FollowButton Button--primary Button--red" style="margin: 0 0 0 12px;"><span style="display: inline-flex; align-items: center;"><svg class="Zi Zi--Plus FollowButton-icon" fill="currentColor" viewBox="0 0 24 24" width="1.2em" height="1.2em"><path d="M18.376 5.624c-3.498-3.499-9.254-3.499-12.752 0-3.499 3.498-3.499 9.254 0 12.752 3.498 3.499 9.254 3.499 12.752 0 3.499-3.498 3.499-9.14 0-12.752zm-1.693 1.693c2.37 2.37 2.596 6.094.678 8.69l-9.367-9.48c2.708-1.919 6.32-1.58 8.69.79zm-9.48 9.48c-2.37-2.37-2.595-6.095-.676-8.69l9.48 9.48c-2.822 1.918-6.433 1.58-8.803-.79z" fill-rule="evenodd"></path></svg></span>屏蔽用户</button>` ) ;
402+ item . lastElementChild . onclick = function ( ) { blockUsers_button_add ( this . dataset . name , this . dataset . userid , true ) }
395403 }
396404 }
397405
398- // 屏蔽用户按钮绑定事件
399- function blockUsers_button_add ( name ) {
400- if ( ! name ) return
406+ // 屏蔽用户按钮绑定事件(添加)
407+ function blockUsers_button_add ( name , userid , reload ) {
408+ if ( ! name || ! userid ) return
401409 let users = menu_value ( 'menu_customBlockUsers' ) ; // 读取屏蔽列表
402410 for ( let num = 0 ; num < users . length ; num ++ ) { // 判断是否已存在
403411 if ( users [ num ] === name ) {
404- GM_notification ( { text : `该用户已经被屏蔽啦~` , timeout : 3000 } ) ;
412+ GM_notification ( { text : `该用户已经被屏蔽啦,无需重复屏蔽 ~` , timeout : 3000 } ) ;
405413 return
406414 }
407415 } ;
408416 users . push ( name ) ; // 追加用户名
409417 GM_setValue ( 'menu_customBlockUsers' , users ) ; // 写入屏蔽列表
410- GM_notification ( { text : `该用户已被屏蔽~\n刷新网页后生效~` , timeout : 3000 } ) ;
418+
419+ GM_xmlhttpRequest ( { // 加入知乎自带的黑名单(和本脚本互补~
420+ url : `https://www.zhihu.com/api/v4/members/${ userid } /actions/block` ,
421+ method : 'POST' ,
422+ timeout : 2000
423+ } ) ;
424+
425+ if ( reload ) {
426+ setTimeout ( function ( ) { location . reload ( ) } , 200 ) ; // 刷新网页,延迟一下,避免知乎反应慢~
427+ } else {
428+ GM_notification ( { text : `该用户已被屏蔽~\n刷新网页后生效~` , timeout : 3000 } ) ;
429+ }
430+ }
431+
432+
433+ // 屏蔽用户按钮绑定事件(删除)
434+ function blockUsers_button_del ( name , userid , reload ) {
435+ if ( ! name || ! userid ) return
436+ let users = menu_value ( 'menu_customBlockUsers' ) ; // 读取屏蔽列表
437+ for ( let num = 0 ; num < users . length ; num ++ ) { // 判断是否已存在
438+ if ( users [ num ] === name ) {
439+ users . splice ( num , 1 ) ; // 移除用户名
440+ GM_setValue ( 'menu_customBlockUsers' , users ) ; // 写入屏蔽列表
441+
442+ GM_xmlhttpRequest ( { // 移除知乎自带的黑名单
443+ url : `https://www.zhihu.com/api/v4/members/${ userid } /actions/block` ,
444+ method : 'DELETE' ,
445+ timeout : 2000
446+ } ) ;
447+
448+ if ( reload ) {
449+ setTimeout ( function ( ) { location . reload ( ) } , 200 ) ; // 刷新网页,延迟一下,避免知乎反应慢~
450+ } else {
451+ GM_notification ( { text : `该用户已取消屏蔽啦~\n刷新网页后生效~` , timeout : 3000 } ) ;
452+ }
453+ return
454+ }
455+ } ;
456+ GM_notification ( { text : `没有在屏蔽列表中找到该用户...` , timeout : 3000 } ) ;
411457 }
412458}
413459
414460
415461// 自定义屏蔽关键词(标题)
416462function customBlockKeywords ( ) {
417463 let nowBlockKeywords = '' ;
418- menu_value ( 'menu_customBlockKeywords' ) . forEach ( function ( item ) { nowBlockKeywords = nowBlockKeywords + '|' + item } )
464+ menu_value ( 'menu_customBlockKeywords' ) . forEach ( function ( item ) { nowBlockKeywords += '|' + item } )
419465 let newBlockKeywords = prompt ( '编辑 [自定义屏蔽关键词]\n(不同关键词之间使用 "|" 分隔,例如:关键词A|关键词B|关键词C \n(关键词不区分大小写' , nowBlockKeywords . replace ( '|' , '' ) ) ;
420466 if ( newBlockKeywords === '' ) {
421467 GM_setValue ( 'menu_customBlockKeywords' , [ ] ) ;
0 commit comments