// ==UserScript== // @name Zhihu Blocklist Helper // @name:zh 知乎黑名单助手 // @namespace https://github.com/mixterjim // @version 0.7 // @author MixterJim // @description 完善知乎黑名单功能,屏蔽黑名单用户回答 // @homepage https://greasyfork.org/zh-CN/scripts/412738-zhihu-blocklist-helper // @icon https://static.zhihu.com/static/favicon.ico // @supportURL https://gist.github.com/mixterjim/a1f4d28e7aafcff12d4fa1d18db85fb6 // @include *://www.zhihu.com/* // @connect www.zhihu.com // @run-at document-body // @grant GM_getValue // @grant GM_setValue // @grant GM_deleteValue // @nocompat Chrome // @license GPL-3.0 // @create 2020.08.20 // @note 2020.08.31-V0.7 增加设置面板 // @thanks Google Closure // @downloadURL https://update.greasyfork.icu/scripts/412738/Zhihu%20Blocklist%20Helper.user.js // @updateURL https://update.greasyfork.icu/scripts/412738/Zhihu%20Blocklist%20Helper.meta.js // ==/UserScript== (function() { var zbh = { array: { // * // * Removes all duplicates from an array. // * // * Runtime: N, // * Worstcase space: 2N // * // * @param {Array} arr The array from which to remove. // * duplicates. removeDuplicates: function(arr) { let seen = {}, cursorInsert = 0, cursorRead = 0; while (cursorRead < arr.length) { let current = arr[cursorRead++]; if (!Object.prototype.hasOwnProperty.call(seen, current)) { seen[current] = true; arr[cursorInsert++] = current; } }; arr.length = cursorInsert; }, /** * Removes the first occurrence of a particular value from an array. * @param {IArrayLike} arr Array from which to remove * value. * @param {T} obj Object to remove. * @template T */ remove: function(arr, obj) { var i = arr.indexOf(obj); if (i >= 0) { Array.prototype.splice.call(arr, i, 1); } }, }, /** * Get blocked id from API. * * @param {Array} arr The array in which to save the results. * @param {string} url An link for which we are getting blocked id. * @param {number|string} offset The index at where we start to * get the blocked id. */ getBlockedIdList: function(arr, url, offset) { let request = new XMLHttpRequest(); request.onload = function() { request.response.data.forEach(function(item, index, array) { arr.push(item.id); }); if (request.response.paging.totals > arr.length) { zbh.getBlockedIdList(arr, url, arr.length); } else { zbh.array.removeDuplicates(arr); GM_setValue("blockedIdList", arr); } }; request.open("GET", url + offset, true); request.responseType = 'json'; request.send(); }, /** * Set block button. * * @param {string} name The author name. * @param {string} isBlocked Is the author blocded? * @param {object} button The block botton. */ blockButton: function(name, isBlocked, button) { let request = new XMLHttpRequest(); if (isBlocked === "true") { request.open("DELETE", "https://www.zhihu.com/api/v4/members/" + name + "/actions/block", true); button.textContent = "Block"; button.setAttribute('blocked', "false"); button.className = "Button Button--primary Button--blue"; zbh.array.remove(blockedIdList, button.id) } else { request.open("POST", "https://www.zhihu.com/api/v4/members/" + name + "/actions/block", true); button.textContent = "Blocked"; button.setAttribute('blocked', "true"); button.className = "Button Button--primary Button--red"; blockedIdList.push(button.id); } request.send(); GM_setValue("blockedIdList", blockedIdList); console.log(blockedIdList) }, settingButtonClick: function() { if (document.querySelector(".setting-panel").style.display == "block") { document.querySelector(".setting-panel").style.display = "none"; } else { document.querySelector(".isBlockHidden").checked = Config.isBlockHidden; document.querySelector(".isBlockTempShow").checked = Config.isBlockTempShow; document.querySelector(".isBlockBtnNotHidden").checked = Config.isBlockBtnNotHidden; document.querySelector(".isBlockPost").checked = Config.isBlockPost; document.querySelector(".setting-panel").style.display = "block"; } }, hidenAnswer: function() { let answerItemList = document.querySelectorAll(".List-item"); while (index < answerItemList.length) { let item = answerItemList[index].querySelector(".ContentItem.AnswerItem"); let name = answerItemList[index].querySelector(".UserLink-link").href.slice(29); let id = JSON.parse(item.getAttribute("data-za-extra-module")).card.content.author_member_hash_id; if (blockedIdList.indexOf(id) != -1) { item.hidden = true; item.querySelector(".AuthorInfo.AnswerItem-authorInfo.AnswerItem-authorInfo--related").innerHTML += ''; answerItemList[index].innerHTML += '
Block by ' + JSON.parse(item.getAttribute("data-zop")).authorName + '
'; // answerItemList[index].querySelector(".blockShow").onclick=function(){answerItemList[this.id].querySelector(".ContentItem.AnswerItem").hidden = false;this.hidden = true}; // 取消隐藏后原 click 侦听器消失 } else { item.querySelector(".AuthorInfo.AnswerItem-authorInfo.AnswerItem-authorInfo--related").innerHTML += ''; } item.querySelector(".Button.Button--primary").onclick = function() { zbh.blockButton(this.value, this.getAttribute("blocked"), this); }; index++; }; return index; }, hidenPost: function() { let itemList = document.querySelectorAll(".Card.TopstoryItem.TopstoryItem-isRecommend"); while (questionIndex < itemList.length) { let item = itemList[questionIndex].firstChild; try { let itemType = JSON.parse(item.getAttribute("data-za-extra-module")).card.content.type; if (itemType != "Answer") { let data = JSON.parse(item.firstChild.getAttribute("data-zop")); let authorName, postName; if (data != null) { authorName = data.authorName; postName = data.title; } else { authorName = "视频"; postName = item.querySelector(".ContentItem-title").textContent; } let title = authorName + " 《" + postName + "》"; item.firstChild.hidden = true; console.log(questionIndex, "hidden") item.innerHTML += '
Block by ' + title + '
'; itemList[questionIndex].querySelector(".blockShow").onclick = function() { itemList[this.id].firstChild.firstChild.hidden = false; this.hidden = true }; // 取消隐藏后原 click 侦听器消失 } } catch (e) { /* 此处应该是广告 */ questionIndex++; continue; } questionIndex++; } }, zbhsettingButton: function() { let settingButton = `
`; let settingPanel = `
知乎黑名单助手
  • 取消 保存
    `; document.querySelector(".AppHeader-userInfo").insertAdjacentHTML("afterbegin", settingButton); document.querySelector(".Setting").addEventListener("click", zbh.settingButtonClick); document.querySelector(".Sticky.AppHeader.AppHeader--old").insertAdjacentHTML("afterend", settingPanel); document.querySelector(".cancel").addEventListener("click", function() { document.querySelector(".setting-panel").style.display = "none"; }); document.querySelector(".save").addEventListener("click", function() { Config.isBlockHidden = document.querySelector(".isBlockHidden").checked; Config.isBlockTempShow = document.querySelector(".isBlockTempShow").checked; Config.isBlockBtnNotHidden = document.querySelector(".isBlockBtnNotHidden").checked; Config.isBlockPost = document.querySelector(".isBlockPost").checked; GM_setValue("Config", Config); document.querySelector(".setting-panel").style.display = "none"; }); document.querySelector(".clear").addEventListener("click", function() { GM_deleteValue("blockedIdList"); GM_deleteValue("Config"); location.reload(); // document.querySelector(".setting-panel").style.display = "none"; }); }, }; /*--------------------------------Main-----------------------------------*/ var Config = GM_getValue("Config", { isBlockHidden: false, // 是否隐藏已拦截的条目 isBlockTempShow: false, //是否允许临时显示拦截条目 isBlockBtnNotHidden: false, // 是否隐藏拉黑按钮 isBlockPost: true, //是否屏蔽专栏 }); var getBlockedUserUrl = "/api/v3/settings/blocked_users?limit=20&offset="; var blockedIdList = GM_getValue("blockedIdList", []); if (blockedIdList.length === 0) { console.log("Loading"); zbh.getBlockedIdList(blockedIdList, getBlockedUserUrl, blockedIdList.length); } // if (/.*zhihu.com\/question.*/.test(document.URL)) { // // if (/.*zhihu.com\/question.*/.test(document.URL) && !/answer/.test(document.URL)) { // var index = 0; // setInterval(zbh.hidenAnswer, 1000); // document.addEventListener('readystatechange', function() { // document.querySelectorAll(".QuestionMainAction.ViewAll-QuestionMainAction").forEach(function(item, index, array) { // item.addEventListener('click',function(){ // window.location.href=item.getAttribute("href") // }) // console.log(item) // }); // }); // console.log("Start hidenAnswer") // } if (/.*zhihu.com\/question.*/.test(document.URL)) { // if (/.*zhihu.com\/question.*/.test(document.URL) && !/answer/.test(document.URL)) { var index = 0; setInterval(zbh.hidenAnswer, 1000); document.addEventListener('readystatechange', function() { var viewAllButton = document.getElementsByClassName("QuestionMainAction ViewAll-QuestionMainAction") for(var i=0;i