From a3a111b96978551a43e1d5146726ea4d606b7270 Mon Sep 17 00:00:00 2001 From: blackmatrix7 <27717518+blackmatrix7@users.noreply.github.com> Date: Wed, 15 Dec 2021 23:20:29 +0800 Subject: [PATCH] =?UTF-8?q?[=E7=9F=A5=E4=B9=8E=E5=8A=A9=E6=89=8B]=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E5=9C=A8BoxJS=E4=B8=AD=E9=85=8D=E7=BD=AE=E5=B1=8F?= =?UTF-8?q?=E8=94=BD=E5=85=B3=E9=94=AE=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/boxjs.json | 148 ++- script/zhihu/README.md | 14 +- script/zhihu/zhihu_plus.js | 1514 ++++++++++++++++------------- script/zhihu/zhihu_plus.lnplugin | 4 +- script/zhihu/zhihu_plus.qxrewrite | 2 + script/zhihu/zhihu_plus.sgmodule | 2 + 6 files changed, 997 insertions(+), 687 deletions(-) diff --git a/script/boxjs.json b/script/boxjs.json index 7bab1f118..1ee697571 100644 --- a/script/boxjs.json +++ b/script/boxjs.json @@ -34,6 +34,24 @@ "repo": "https://github.com/blackmatrix7/ios_rule_script/tree/master/script/bilibili", "icons": ["https://raw.githubusercontent.com/Orz-3/mini/master/Alpha/bilibili.png", "https://raw.githubusercontent.com/Orz-3/mini/master/Color/bilibili.png"] }, + { + "id": "blackmatrix7.didachuxing", + "name": "嘀嗒出行", + "keys": ["dida_cid", "dida_cookie", "dida_useragent", "dida_access_token"], + "author": "@blackmatrix7", + "repo": "https://github.com/blackmatrix7/ios_rule_script/tree/master/script/didachuxing", + "icons": ["https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/script/didachuxing/didachuxing.png", "https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/script/didachuxing/didachuxing.png"], + "script": "https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/script/didachuxing/didachuxing_plus.js" + }, + { + "id": "blackmatrix7.didichuxing", + "name": "滴滴出行", + "keys": ["didi_token", "didi_city_id", "didi_lottery_id", "didi_my_source_id", "didi_group_id", "didi_finance_channel_id", "didi_finance_activity_id"], + "author": "@blackmatrix7", + "repo": "https://github.com/blackmatrix7/ios_rule_script/tree/master/script/didichuxing", + "icons": ["https://raw.githubusercontent.com/Orz-3/mini/master/Alpha/didi.png", "https://raw.githubusercontent.com/Orz-3/mini/master/Color/didi.png"], + "script": "https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/script/didichuxing/didi_checkin.js" + }, { "id": "blackmatrix7.dingdong", "name": "叮咚买菜", @@ -140,7 +158,11 @@ "author": "@blackmatrix7", "repo": "https://github.com/blackmatrix7/ios_rule_script/tree/master/script/zhihu", "icons": ["https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/script/zhihu/zhihu.png", "https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/script/zhihu/zhihu.png"], - "descs_html": ["

具体功能说明查看README.md

", "

✨为Plus版本专属功能,❓为已过时或即将淘汰功能

", "

切换Lite、Plus版本,或出现异常请执行数据清理

"], + "descs_html": [ + "

具体功能说明查看README.md

", + "

✨为Plus版本专属功能,❓为已过时或即将淘汰功能

", + "

切换Lite、Plus版本,或出现异常请执行数据清理

" + ], "scripts": [ { "name": "知乎助手数据清理", @@ -232,6 +254,13 @@ "type": "boolean", "desc": "知乎7.x及以上版本无效" }, + { + "id": "zhihu_keyword_block", + "name": "知乎屏蔽关键词", + "val": "", + "type": "textarea", + "desc": "必须是JSON格式,Key为用户Id,不要修改。格式不正确可能导致执行异常。" + }, { "id": "magicjs_loglevel", "name": "日志等级", @@ -250,6 +279,10 @@ "key": "ERROR", "label": "ERROR" }, + { + "key": "CRITICAL", + "label": "CRITICAL" + }, { "key": "DEBUG", "label": "DEBUG" @@ -299,6 +332,35 @@ "placeholder": "", "autoGrow": true, "desc": "除非无法正常获取Sid,否则保持现在的值,不要修改" + }, + { + "id": "magicjs_loglevel", + "name": "日志等级", + "val": "INFO", + "type": "radios", + "items": [ + { + "key": "INFO", + "label": "INFO" + }, + { + "key": "WARNING", + "label": "WARNING" + }, + { + "key": "ERROR", + "label": "ERROR" + }, + { + "key": "CRITICAL", + "label": "CRITICAL" + }, + { + "key": "DEBUG", + "label": "DEBUG" + } + ], + "desc": "出现异常时请选择DEBUG提交日志" } ], "author": "@blackmatrix7", @@ -314,21 +376,50 @@ { "id": "blackmatrix7.applestore", "name": "AppleStore", - "keys": ["iphone_model", "apple_store_region", "applestore_bark_url", "iphone_stock"], + "keys": ["goods_model", "applestore_region", "applestore_bark_url", "applestore_stock", "onlinestore_stock"], "author": "@blackmatrix7", "repo": "https://github.com/blackmatrix7/ios_rule_script/tree/master/script/applestore", "settings": [ { - "id": "iphone_model", - "name": "iPhone型号", - "val": "", - "type": "input", - "placeholder": "MLDE3CH/A;MLDG3CH/A", - "autoGrow": true, - "desc": "需要监控的iPhone型号,多款以;分隔,移动端建议只监控一个" + "id": "applestore_type", + "name": "商店类型", + "val": "APPLESTORE", + "type": "radios", + "items": [ + { + "key": "AppleStore零售店", + "label": "INFO" + }, + { + "key": "Apple官网", + "label": "WARNING" + }, + { + "key": "ERROR", + "label": "ERROR" + }, + { + "key": "CRITICAL", + "label": "CRITICAL" + }, + { + "key": "DEBUG", + "label": "DEBUG" + } + ], + "desc": "出现异常时请选择DEBUG提交日志" }, { - "id": "apple_store_region", + "id": "goods_model", + "name": "商品型号", + "val": "", + "type": "input", + "placeholder": "Z0YQ#MKMR3CH/A,MJXA3FE/A#AppleWatch 7 石墨色不锈钢;MLDE3CH/A", + "autoGrow": true, + "desc": "需要监控的商品型号,option用#分隔,自定义名称在option后以#分隔。多款以;分隔,移动端建议只监控一个。" + }, + { + "id": "applestore_region", "name": "监控地区", "val": "", "type": "input", @@ -344,12 +435,41 @@ "placeholder": "https://api.day.app/VXTWvaQ18N29bsQAg7DgkT", "autoGrow": true, "desc": "使用Bark推送到其他设备,比如iPad执行监控,推送结果到iPhone" + }, + { + "id": "applestore_settings_notify_soldout", + "name": "查询无货也进行通知", + "val": false, + "type": "boolean", + "desc": "可能会发起大量推送,建议仅在测试时开启" + }, + { + "id": "magicjs_loglevel", + "name": "日志等级", + "val": "INFO", + "type": "radios", + "items": [ + { + "key": "INFO", + "label": "INFO" + }, + { + "key": "WARNING", + "label": "WARNING" + }, + { + "key": "ERROR", + "label": "ERROR" + }, + { + "key": "DEBUG", + "label": "DEBUG" + } + ], + "desc": "出现异常时请选择DEBUG提交日志" } ], - "icons": [ - "https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/script/applestore/icon/applestore_dark.png", - "https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/script/applestore/icon/applestore.png" - ] + "icons": ["https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/script/applestore/icon/applestore_dark.png", "https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/script/applestore/icon/applestore.png"] } ] } diff --git a/script/zhihu/README.md b/script/zhihu/README.md index e2a766c73..02bcac520 100644 --- a/script/zhihu/README.md +++ b/script/zhihu/README.md @@ -37,12 +37,12 @@ 27. 新旧版评论区黑名单用户过滤✨ 28. 屏蔽转发的黑名单用户的想法✨ 29. 推荐页屏蔽关键词解锁✨ -30. 知乎网页版去广告 [#21](https://github.com/blackmatrix7/ios_rule_script/issues/21)❓ -31. 去除热搜内的广告❓ -32. 屏蔽推荐页中的盐选推荐内容❓ -33. 拦截知乎下发的配置,如皮肤(alpha)✨❓ -34. 减少推荐页内的视频(alpha)✨❓ -35. 去除推荐页的付费推荐内容✨❓ +30. ~~知乎网页版去广告 [#21](https://github.com/blackmatrix7/ios_rule_script/issues/21)~~❓ +31. ~~去除热搜内的广告~~❓ +32. ~~屏蔽推荐页中的盐选推荐内容~~❓ +33. ~~拦截知乎下发的配置,如皮肤(alpha)~~✨❓ +34. ~~减少推荐页内的视频(alpha)~~✨❓ +35. ~~去除推荐页的付费推荐内容~~✨❓ ## 最近更新 @@ -101,7 +101,7 @@ Lite版本请确保所有Plus版本的选项都为关闭状态,以减少不必 此功能并非真正解锁会员特权,只是借用知乎APP的操作界面,通过脚本实现的关键词屏蔽。如果你已经是尊贵的盐选会员,建议关闭此功能,使用盐选会员自带的关键词屏蔽。 -知乎助手的关键词解锁额外提供了正则表达式的屏蔽功能,可以实现一些较复杂的判断逻辑,及提高屏蔽效率。比如关键词设置`男士.*(沐浴露|香水)+`,表示只有“男士”和“沐浴露”或“香水”共同出现时才进行屏蔽。而`(羊毛|优惠|红包)+`,表示“羊毛”、“优惠”、“红包”出现任意一个关键词就进行屏蔽,可以替代原先的羊毛”、“优惠”、“红包”三个关键词,减少关键词数量,提高屏蔽效率。 +**知乎助手的关键词解锁额外提供了正则表达式的屏蔽功能,可以实现一些较复杂的判断逻辑,及提高屏蔽效率。**比如关键词设置`男士.*(沐浴露|香水)+`,表示只有“男士”和“沐浴露”或“香水”共同出现时才进行屏蔽。而`(羊毛|优惠|红包)+`,表示“羊毛”、“优惠”、“红包”出现任意一个关键词就进行屏蔽,可以替代原先的羊毛”、“优惠”、“红包”三个关键词,减少关键词数量,提高屏蔽效率。 diff --git a/script/zhihu/zhihu_plus.js b/script/zhihu/zhihu_plus.js index 1dfa9e352..d445d7213 100644 --- a/script/zhihu/zhihu_plus.js +++ b/script/zhihu/zhihu_plus.js @@ -12,584 +12,65 @@ let magicJS = MagicJS(scriptName, "INFO"); if (magicJS.isResponse) { switch (true) { // 回答内容优化 - case magicJS.read("zhihu_settings_answer_tip") != false && /^https?:\/\/www\.zhihu\.com\/appview\/v2\/answer\/.*(entry=(?!(preload-topstory|preload-search|preload-subscription)))?/.test(magicJS.request.url): - try { - let html = magicJS.response.body; - // 付费内容提醒 - if ((html.indexOf("查看完整内容") >= 0 || html.indexOf("查看全部章节") >= 0) && html.indexOf("paid") >= 0) { - let matchStr = html.match(/(richText[^<]*>)(.)/)[1]; - let start = html.lastIndexOf(matchStr) + matchStr.length; - let insertText = - '
知乎助手 · 本文为付费内容
'; - response = { body: html.slice(0, start) + insertText + html.slice(start) }; - } - // 营销推广提醒 - else if (html.indexOf("ad-link-card") >= 0 || html.indexOf("xg.zhihu.com") >= 0 || html.indexOf("知乎营销平台") >= 0) { - let matchStr = html.match(/(richText[^<]*>)(.)/)[1]; - let start = html.lastIndexOf(matchStr) + matchStr.length; - let insertText = - '
知乎助手 · 本文含有营销推广
'; - response = { body: html.slice(0, start) + insertText + html.slice(start) }; - } - // 购物推广提醒 - else if (html.indexOf("mcn-link-card") >= 0) { - let matchStr = html.match(/(richText[^<]*>)(.)/)[1]; - let start = html.lastIndexOf(matchStr) + matchStr.length; - let insertText = - '
知乎助手 · 本文含有购物推广
'; - response = { body: html.slice(0, start) + insertText + html.slice(start) }; - } - // 彩蛋 - else if (Math.floor(Math.random() * 200) == 7) { - let matchStr = html.match(/(richText[^<]*>)(.)/)[1]; - let start = html.lastIndexOf(matchStr) + matchStr.length; - let insertText = - '
知乎助手 · 本文为免费内容
'; - response = { body: html.slice(0, start) + insertText + html.slice(start) }; - } - } catch (err) { - magicJS.logError(`知乎付费内容提醒出现异常:${err}`); - } + case magicJS.read("zhihu_settings_answer_tip") != false && + /^https?:\/\/www\.zhihu\.com\/appview\/v2\/answer\/.*(entry=(?!(preload-topstory|preload-search|preload-subscription)))?/.test(magicJS.request.url): + response = modifyAnswer(response); break; // 处理登录用户信息 case /^https:\/\/api\.zhihu\.com\/people\/self/.test(magicJS.request.url): - try { - let obj = JSON.parse(magicJS.response.body); - magicJS.logDebug(`用户登录用户信息,接口响应:${magicJS.response.body}`); - if (obj && obj["id"] && obj.hasOwnProperty("vip_info") && obj["vip_info"].hasOwnProperty("is_vip")) { - const userInfo = { - id: obj["id"], - is_vip: obj["vip_info"]["is_vip"] ? obj["vip_info"]["is_vip"] !== undefined : false, - }; - magicJS.logDebug(`当前用户id:${obj["id"]},是否为VIP:${obj["vip_info"]["is_vip"]}`); - magicJS.write(currentUserInfoKey, userInfo); - // 在知乎APP显示VIP,仅自己可见,无实际用途 - if (magicJS.read("zhihu_settings_fake_vip") == true && obj["vip_info"]["is_vip"] === false) { - obj["vip_info"]["is_vip"] = true; - obj["vip_info"]["vip_icon"] = { - url: "https://pic1.zhimg.com/v2-4812630bc27d642f7cafcd6cdeca3d7a_r.png", - night_mode_url: "https://pic1.zhimg.com/v2-c9686ff064ea3579730756ac6c289978_r.png", - }; - obj["vip_info"]["entrance"] = { - icon: { - url: "https://pic1.zhimg.com/v2-5b7012c8c22fd520f5677305e1e551bf.png", - night_mode_url: "https://pic1.zhimg.com/v2-e51e3252d7a2cb016a70879defd5da0b.png", - }, - title: "我的盐选会员", - expires_day: "2033-12-24", - sub_title: null, - button_text: "你好,知乎!", - jump_url: "zhihu://vip/purchase", - button_jump_url: "zhihu://vip/purchase", - sub_title_new: null, - identity: "svip", - }; - obj["vip_info"]["entrance_new"] = { - left_button: { - title: "精选会员内容", - description: "为您严选好内容", - jump_url: "zhihu://market/home", - }, - right_button: { - title: "我的盐选会员", - description: "畅享 10w+ 优质内容", - jump_url: "zhihu://vip/my", - }, - }; - obj["vip_info"]["entrance_v2"] = { - title: "我的盐选会员", - sub_title: "畅享 10w+ 优质内容", - jump_url: "zhihu://vip/my", - button_text: "查看权益", - }; - response = { body: JSON.stringify(obj) }; - } - } else { - magicJS.logWarning(`没有获取到本次登录用户信息,如未对功能造成影响,请忽略此日志。`); - } - } catch (err) { - magicJS.logError(`知乎获取当前用户信息出现异常:${err}`); - } + response = processUserInfo(response); break; // 黑名单增强 - 浏览黑名单用户信息时自动加入脚本黑名单 case magicJS.read("zhihu_settings_blocked_users") != false && /^https?:\/\/api\.zhihu\.com\/people\/((?!self).)*$/.test(magicJS.request.url): - try { - let obj = JSON.parse(magicJS.response.body); - // 删除MCN信息 - delete obj["mcn_user_info"]; - response = { body: JSON.stringify(obj) }; - // 如已是黑名单用户,但不在脚本黑名单中,则自动加入 - if (obj.name && obj.id && obj.is_blocking === true) { - const userInfo = GetUserInfo(); - let customBlockedUsers = magicJS.read(blockedUsersKey, userInfo.id); - customBlockedUsers = typeof customBlockedUsers === "object" && !!customBlockedUsers ? customBlockedUsers : {}; - if (!customBlockedUsers[obj.name]) { - magicJS.logDebug(`当前需要加入黑名单的用户Id:${obj["id"]},用户名:${obj["name"]}`); - customBlockedUsers[obj["name"]] = obj["id"]; - magicJS.write(blockedUsersKey, customBlockedUsers, userInfo.id); - magicJS.logDebug(`${obj["name"]}写入脚本黑名单成功,当前脚本黑名单数据:${JSON.stringify(customBlockedUsers)}`); - magicJS.notify(`已自动将用户“${obj["name"]}”写入脚本黑名单。`); - } - } - } catch (err) { - magicJS.logError(`知乎去除MCN信息出现异常:${err}`); - } + response = autoInsertBlackList(response); break; // 推荐去广告与黑名单增强 case /^https:\/\/api\.zhihu\.com\/topstory\/recommend\?/.test(magicJS.request.url): - try { - // 默认关闭 - const settings_remove_yanxuan = magicJS.read("zhihu_settings_remove_yanxuan") == true; - const settings_recommend_stream = magicJS.read("zhihu_settings_recommend_stream") == true; - // 默认开启 - const settings_blocked_keywords = magicJS.read("zhihu_settings_blocked_keywords") != false; - const settings_blocked_users = magicJS.read("zhihu_settings_blocked_users") != false; - const user_info = GetUserInfo(); - - let keywords = magicJS.read(keywordBlockKey, user_info.id); - keywords = settings_blocked_keywords && !!keywords ? keywords : []; - let customBlockedUsers = magicJS.read(blockedUsersKey, user_info.id); - customBlockedUsers = settings_blocked_users && !!customBlockedUsers ? customBlockedUsers : {}; - - const dataFilter = (element) => { - let elementStr = JSON.stringify(element); - // 是否为广告 - let isAd = element["card_type"] === "slot_event_card" || element["card_type"] === "slot_video_event_card" || element.hasOwnProperty("ad"); - // 是否为流媒体 element["extra"]["type"] === "drama" || element["extra"]["type"] === "zvideo") || element["extra"]["type"] === "Video" || element["common_card"]["style"] === "BIG_IMAGE") - let isStream = isAd != true && settings_recommend_stream && elementStr.search(/"(type|style)+"\s?:\s?"(drama|zvideo|Video|BIG_IMAGE)+"/i) >= 0; - // 是否为盐选推荐 - let isYanXuan = isStream != true && settings_remove_yanxuan && elementStr.indexOf("https://pic2.zhimg.com/80/v2-c46fc8ec4c4e9ffc8f846ae0d8158a80_1440w.png") >= 0; - // 是否匹配脚本关键词过滤 - let matchKeyword = false; - if (isStream != true && settings_blocked_keywords) { - for (let i = 0; i < keywords.length; i++) { - if (elementStr.search(keywords[i]) >= 0) { - if (magicJS.isDebug){ - let elementTitle = element.common_card.feed_content.title.panel_text; - let elementContent = element.common_card.feed_content.content.panel_text; - let actionUrl = ""; - try { - actionUrl = element.common_card.feed_content.title.action.intent_url; - } catch {} - magicJS.logDebug(`匹配关键字:\n${keywords[i]}\n标题:\n${elementTitle}\n内容:\n${elementContent}`); - magicJS.notifyDebug(scriptName, `关键字:${keywords[i]}`, `${elementTitle}\n${elementContent}`, actionUrl); - } - matchKeyword = true; - break; - } - } - } - // 是否为黑名单用户 - let isBlockedUser = false; - try { - isBlockedUser = matchKeyword != true && settings_blocked_users && customBlockedUsers && element["common_card"]["feed_content"]["source_line"]["elements"][1]["text"]["panel_text"] in customBlockedUsers; - } catch { - isBlockedUser = false; - } - return !(isAd || isStream || isYanXuan || matchKeyword || isBlockedUser); - }; - - // 修复number类型精度丢失 - let obj = JSON.parse(magicJS.response.body.replace(/(\w+"+\s?):\s?(\d{15,})/g, '$1:"$2"')); - obj["data"] = obj["data"].filter(dataFilter); - response = { body: JSON.stringify(obj) }; - } catch (err) { - magicJS.logError(`知乎推荐列表去广告出现异常:${err}`); - } + response = removeRecommendAds(response); break; // 关注列表去广告 case /^https?:\/\/api\.zhihu\.com\/moments(\/|\?)?(recommend|action=|feed_type=)(?!\/people)/.test(magicJS.request.url): - try { - // 修复number类型精度丢失 - let obj = JSON.parse(magicJS.response.body.replace(/(\w+"+\s?):\s?(\d{15,})/g, '$1:"$2"')); - const user_info = GetUserInfo(); - let customBlockedUsers = magicJS.read(blockedUsersKey, user_info.id); - customBlockedUsers = !!customBlockedUsers ? customBlockedUsers : {}; - let data = []; - - const settings_moments_stream = magicJS.read("zhihu_settings_moments_stream") == true; - const settings_blocked_users = magicJS.read("zhihu_settings_blocked_users") != false; - - for (let i = 0; i < obj["data"].length; i++) { - // let element = targetIdFix(obj["data"][i]); - let element = obj["data"][i]; - if (!element["ad"] && !element["adjson"] && !element["ad_list"]) { - // 判断转发的想法是否含有黑名单用户 - if (settings_blocked_users && element.target && element.target.origin_pin && element.target.origin_pin.author && customBlockedUsers[element.target.origin_pin.author.name]) { - magicJS.notifyDebug(`屏蔽“${element.target.author.name}”转发黑名单用户“${element.target.origin_pin.author.name}”的想法。`); - } - // 屏蔽关注页的“最新视频” - else if (!settings_moments_stream || element["type"] != "videos") { - data.push(element); - } - } - } - obj["data"] = data; - response = { body: JSON.stringify(obj) }; - } catch (err) { - magicJS.logError(`知乎关注列表去广告出现异常:${err}`); - } + response = removeMomentsAds(response); break; // 回答列表去广告与黑名单增强 case /^https?:\/\/api\.zhihu\.com\/v4\/questions/.test(magicJS.request.url): - try { - const userInfo = GetUserInfo(); - let customBlockedUsers = magicJS.read(blockedUsersKey, userInfo.id); - customBlockedUsers = !!customBlockedUsers ? customBlockedUsers : {}; - let obj = JSON.parse(magicJS.response.body); - magicJS.logDebug(`当前黑名单列表: ${JSON.stringify(customBlockedUsers)}`); - delete obj["ad_info"]; - delete obj["roundtable_info"]; - let data = obj["data"].filter((element) => { - return !customBlockedUsers[element["author"]["name"]]; - }); - obj["data"] = data; - response = { body: JSON.stringify(obj) }; - } catch (err) { - magicJS.logError(`知乎回答列表去广告出现异常:${err}`); - } + response = removeQuestionsAds(response); break; // 知乎V5版本评论去广告及黑名单增强 case /^https?:\/\/api\.zhihu\.com\/comment_v5\/(answers|pins|comments?|articles)\/\d+\/(root|child)_comment/.test(magicJS.request.url): - try { - if (!!magicJS.response.body) { - let obj = JSON.parse(magicJS.response.body); - obj["ad_info"] = {}; - // 屏蔽黑名单用户 - if (magicJS.read("zhihu_settings_blocked_users") != false) { - let user_info = GetUserInfo(); - let customBlockedUsers = magicJS.read(blockedUsersKey, user_info.id); - customBlockedUsers = !!customBlockedUsers ? customBlockedUsers : {}; - let newComments = []; - let blockCommentIdObj = {}; - obj.data.forEach((comment) => { - // 评论人昵称 - let commentUserName = comment.author.name; - // 回复哪个人的评论(仅适用于独立子评论页面请求) - let replyUserName = ""; - if (comment.reply_to_author && comment.reply_to_author && comment.reply_to_author.name) { - replyUserName = comment.reply_to_author.name; - } - if (customBlockedUsers[commentUserName] || customBlockedUsers[replyUserName]) { - if (customBlockedUsers[commentUserName] && !replyUserName && magicJS.request.url.indexOf("root_comment") > 0) { - magicJS.notifyDebug(`屏蔽黑名单用户“${commentUserName}”的主评论。`); - } else if (customBlockedUsers[commentUserName] && !replyUserName && magicJS.request.url.indexOf("child_comment") > 0) { - magicJS.notifyDebug(`屏蔽黑名单用户“${commentUserName}”的子评论。`); - } else if (customBlockedUsers[commentUserName] && replyUserName && magicJS.request.url.indexOf("child_comment") > 0) { - magicJS.notifyDebug(`屏蔽黑名单用户“${commentUserName}”回复“${replyUserName}”的子评论。`); - } else { - magicJS.notifyDebug(`屏蔽“${commentUserName}”回复黑名单用户“${replyUserName}”的子评论。`); - } - blockCommentIdObj[comment.id] = commentUserName; - // 主评论数量-1,仅适用于root_comment主评论页面请求 - if (obj.counts && obj.counts.total_counts) { - obj.counts.total_counts -= 1; - } - // 子评论数量-1,仅适用于child_comment子评论页面请求 - if (obj.paging && obj.paging.totals) { - obj.paging.totals -= 1; - } - if (obj.root && obj.root.child_comment_count) { - obj.root.child_comment_count -= 1; - } - } else { - if (comment.child_comments) { - let newChildComments = []; - comment.child_comments.forEach((childComment) => { - let childCommentUserName = childComment.author.name; - if (customBlockedUsers[childCommentUserName] || blockCommentIdObj[childComment.reply_comment_id]) { - if (customBlockedUsers[childCommentUserName]) { - magicJS.notifyDebug(`屏蔽黑名单用户“${childCommentUserName}”的子评论。`); - blockCommentIdObj[childComment.id] = childCommentUserName; - } else { - magicJS.notifyDebug(`屏蔽“${childCommentUserName}”回复黑名单用户“${blockCommentIdObj[childComment.reply_comment_id]}”的子评论。`); - } - comment.child_comment_count -= 1; - } else { - newChildComments.push(childComment); - } - }); - comment.child_comments = newChildComments; - } - newComments.push(comment); - } - }); - obj.data = newComments; - } - response = { body: JSON.stringify(obj) }; - } - } catch (err) { - magicJS.logError(`去除知乎评论广告出现异常:${err}`); - } + response = removeCommentV5Ads(response); break; // 知乎旧版回答中的评论黑名单增强 case /^https?:\/\/api\.zhihu\.com\/(answers|pins|comments?|articles)\/\d+\/(root|child)_comments/.test(magicJS.request.url): - try { - if (!!magicJS.response.body) { - // 评论区去广告 - let obj = JSON.parse(magicJS.response.body); - if (magicJS.read("zhihu_settings_blocked_users") != false) { - // 屏蔽黑名单用户 - let user_info = GetUserInfo(); - let customBlockedUsers = magicJS.read(blockedUsersKey, user_info.id); - let newData = []; - obj.data.forEach((comment) => { - // 评论人昵称 - let commentUserName = comment.author.member.name; - // 回复哪个人的评论(仅适用于独立子评论页面请求) - let replyUserName = ""; - if (comment.reply_to_author && comment.reply_to_author.member && comment.reply_to_author.member.name) { - replyUserName = comment.reply_to_author.member.name; - } - if (customBlockedUsers[commentUserName] || customBlockedUsers[replyUserName]) { - if (customBlockedUsers[commentUserName] && !replyUserName && magicJS.request.url.indexOf("root_comment") > 0) { - magicJS.notifyDebug(`屏蔽黑名单用户“${commentUserName}”的主评论。`); - } else if (customBlockedUsers[commentUserName] && !replyUserName && magicJS.request.url.indexOf("child_comment") > 0) { - magicJS.notifyDebug(`屏蔽黑名单用户“${commentUserName}”的子评论。`); - } else if (customBlockedUsers[commentUserName] && replyUserName && magicJS.request.url.indexOf("child_comment") > 0) { - magicJS.notifyDebug(`屏蔽黑名单用户“${commentUserName}”回复“${replyUserName}”的子评论。`); - } else { - magicJS.notifyDebug(`屏蔽“${commentUserName}”回复黑名单用户“${replyUserName}”的子评论。`); - } - // 减少主评论页面中的评论总数(仅适用于独立的主评论页面请求) - if (obj.common_counts) { - obj.common_counts -= 1; - } - // 减少子评论页面中的评论总数(仅适用于独立子评论页面请求) - if (obj.paging && obj.paging.totals) { - obj.paging.totals -= 1; - } - } else { - // 屏蔽子评论中的黑名单用户(仅适用于独立的主评论页面请求) - if (comment.child_comments) { - let newChildComments = []; - comment.child_comments.forEach((childComment) => { - if (customBlockedUsers[childComment.author.member.name] || customBlockedUsers[childComment.reply_to_author.member.name]) { - if (customBlockedUsers[childComment.author.member.name]) { - magicJS.notifyDebug(`屏蔽黑名单用户“${childComment.author.member.name}”的主评论。`); - } else { - magicJS.notifyDebug(`屏蔽“${childComment.author.member.name}”回复黑名单用户“${childComment.reply_to_author.member.name}”的子评论。`); - } - comment.child_comment_count -= 1; - } else { - newChildComments.push(childComment); - } - }); - comment.child_comments = newChildComments; - } - newData.push(comment); - } - }); - obj.data = newData; - } - response = { body: JSON.stringify(obj) }; - } - } catch (err) { - magicJS.logError(`去除知乎评论广告出现异常:${err}`); - } + response = removeCommentAds(response); break; // 知乎热榜去广告 case magicJS.read("zhihu_settings_hot_list") != false && /^https?:\/\/api\.zhihu\.com\/topstory\/hot-lists?(\?|\/)/.test(magicJS.request.url): - try { - if (!!magicJS.response.body) { - let obj = JSON.parse(magicJS.response.body); - let data = obj["data"].filter((e) => { - return e["type"] === "hot_list_feed" || e["type"] === "hot_list_feed_video"; - }); - obj["data"] = data; - response = { body: JSON.stringify(obj) }; - } - } catch (err) { - magicJS.logError(`去除知乎热搜广告出现异常:${err}`); - } - break; - // 黑名单管理 - case magicJS.read("zhihu_settings_blocked_users") != false && /^https?:\/\/api\.zhihu\.com\/settings\/blocked_users/.test(magicJS.request.url): - const userInfo = GetUserInfo(); - let defaultBlockedUsers = {}; - let customBlockedUsers = magicJS.read(blockedUsersKey, userInfo.id); - customBlockedUsers = typeof customBlockedUsers === "object" && !!customBlockedUsers ? customBlockedUsers : {}; - defaultAnswerBlockedUsers.forEach((element) => { - customBlockedUsers[element] = "00000000000000000000000000000000"; - defaultBlockedUsers[element] = "00000000000000000000000000000000"; - }); - magicJS.logDebug(`当前用户id:${userInfo.id},脚本黑名单:${JSON.stringify(customBlockedUsers)}`); - // 获取黑名单 - if (magicJS.request.method == "GET") { - try { - // 加载黑名单首页时,清空历史黑名单,仅保留脚本默认黑名单 - if (magicJS.request.url.indexOf("offset") < 0) { - customBlockedUsers = defaultBlockedUsers; - magicJS.logDebug("脚本黑名单已清空,请滑动至黑名单末尾保证重新获取完成。"); - magicJS.notify("脚本黑名单已清空,请滑动至黑名单末尾保证重新获取完成。"); - } - let obj = JSON.parse(magicJS.response.body); - if (!!obj["data"]) { - magicJS.logDebug(`本次滑动获取的黑名单信息:${JSON.stringify(obj["data"])}`); - obj["data"].forEach((element) => { - if (element["name"] != "[已重置]") { - customBlockedUsers[element["name"]] = element["id"]; - } - }); - magicJS.write(blockedUsersKey, customBlockedUsers, userInfo.id); - if (obj["paging"]["is_end"] == true) { - magicJS.notify(`获取脚本黑名单结束,当前黑名单共${Object.keys(customBlockedUsers).length - defaultAnswerBlockedUsers.length}人。\n脚本内置黑名单${defaultAnswerBlockedUsers.length}人。`); - magicJS.logDebug(`脚本黑名单内容:${JSON.stringify(customBlockedUsers)}。`); - } - } else { - magicJS.logWarning(`获取黑名单失败,接口响应不合法:${magicJS.response.body}`); - } - } catch (err) { - magicJS.del(blockedUsersKey); - magicJS.logError(`获取黑名单失败,异常信息:${err}`); - magicJS.notify("获取黑名单失败,执行异常,已清空黑名单。"); - } - } - // 写入黑名单 - else if (magicJS.request.method == "POST") { - try { - let obj = JSON.parse(magicJS.response.body); - if (obj.hasOwnProperty("name") && obj.hasOwnProperty("id")) { - magicJS.logDebug(`当前需要加入黑名单的用户Id:${obj["id"]},用户名:${obj["name"]}`); - if (obj["id"]) { - customBlockedUsers[obj["name"]] = obj["id"]; - magicJS.write(blockedUsersKey, customBlockedUsers, userInfo.id); - magicJS.logDebug(`${obj["name"]}写入脚本黑名单成功,当前脚本黑名单数据:${JSON.stringify(customBlockedUsers)}`); - magicJS.notify(`已将用户“${obj["name"]}”写入脚本黑名单。`); - } else { - magicJS.logError(`${obj["name"]}写入脚本黑名单失败,没有获取到用户Id。`); - magicJS.notify(`将用户“${obj["name"]}”写入脚本黑名单失败!`); - } - } else { - magicJS.logWarning(`写入黑名单失败,接口响应不合法:${magicJS.response.body}`); - magicJS.notify("写入脚本黑名单失败,接口返回不合法。"); - } - } catch (err) { - magicJS.logError(`写入黑名单失败,异常信息:${err}`); - magicJS.notify("写入脚本黑名单失败,执行异常,请查阅日志。"); - } - } - // 移出黑名单 - else if (magicJS.request.method == "DELETE") { - try { - let obj = JSON.parse(magicJS.response.body); - if (obj.success) { - let user_id = magicJS.request.url.match(/https?:\/\/api\.zhihu\.com\/settings\/blocked_users\/([0-9a-zA-Z]*)/)[1]; - if (user_id) { - magicJS.logDebug(`当前需要移出黑名单的用户Id:${user_id}`); - for (let username in customBlockedUsers) { - if (customBlockedUsers[username] == user_id) { - delete customBlockedUsers[username]; - magicJS.write(blockedUsersKey, customBlockedUsers, userInfo.id); - magicJS.logDebug(`${username}移出脚本黑名单成功,当前脚本黑名单数据:${JSON.stringify(customBlockedUsers)}`); - magicJS.notify(`已将用户“${username}”移出脚本黑名单!`); - break; - } - } - } else { - magicJS.logError("将用户移出脚本黑名单失败!\n建议从设置中刷新黑名单数据。"); - magicJS.notify(`将用户移出脚本黑名单失败,没有获取到用户Id。\n建议从设置中刷新黑名单数据。`); - } - } else { - magicJS.logWarning(`移出黑名单失败,接口响应不合法:${magicJS.response.body}`); - magicJS.notify("移出脚本黑名单失败,接口返回不合法。"); - } - } catch (err) { - magicJS.logError(`移出黑名单失败,异常信息:${err}`); - magicJS.notify("移出脚本黑名单失败,执行异常,请查阅日志。"); - } - } + response = removeHotListAds(response); break; // 拦截官方账号推广消息 case magicJS.read("zhihu_settings_sys_msg") != false && /^https?:\/\/api\.zhihu\.com\/notifications\/v3\/timeline\/entry\/system_message/.test(magicJS.request.url): - try { - const sysmsg_blacklist = ["知乎小伙伴", "知乎视频", "知乎团队", "知乎礼券", "知乎读书会团队"]; - let obj = JSON.parse(magicJS.response.body); - let data = obj["data"].filter((element) => { - return sysmsg_blacklist.indexOf(element["content"]["title"]) < 0; - }); - obj["data"] = data; - response = { body: JSON.stringify(obj) }; - } catch (err) { - magicJS.logError(`知乎拦截官方账号推广消息出现异常:${err}`); - } + response = removeSysMsgAds(response); break; // 屏蔽官方营销消息 case magicJS.read("zhihu_settings_sys_msg") != false && /^https?:\/\/api\.zhihu\.com\/notifications\/v3\/message/.test(magicJS.request.url): - try { - let obj = JSON.parse(magicJS.response.body); - let newItems = []; - for (let item of obj["data"]) { - if (item["detail_title"] === "官方帐号消息") { - let unread_count = item["unread_count"]; - if (unread_count > 0) { - item["content"]["text"] = "未读消息" + unread_count + "条"; - } else { - item["content"]["text"] = "全部消息已读"; - } - item["is_read"] = true; - item["unread_count"] = 0; - newItems.push(item); - } else if (item["detail_title"] !== "知乎活动助手") { - newItems.push(item); - } - } - obj["data"] = newItems; - response = { body: JSON.stringify(obj) }; - } catch (err) { - magicJS.logError(`知乎屏蔽官方营销消息出现异常:${err}`); - } + response = removeMarketingMsg(response); break; // 去除预置关键字广告 case magicJS.read("zhihu_settings_preset_words") == true && /^https?:\/\/api\.zhihu\.com\/search\/preset_words\?/.test(magicJS.request.url): - try { - if (!!magicJS.response.body) { - magicJS.logDebug(`预置关键字返回:${magicJS.response.body}`); - let obj = JSON.parse(magicJS.response.body); - if (obj.hasOwnProperty("preset_words") && obj["preset_words"]["words"]) { - let words = obj["preset_words"]["words"].filter((element) => { - return element["type"] !== "ad"; - }); - obj["preset_words"]["words"] = words; - response = { body: JSON.stringify(obj) }; - } - } - } catch (err) { - magicJS.logError(`知乎去除预置关键字广告出现异常:${err}`); - } + response = removeKeywordAds(response); break; // 优化知乎软件配置 case magicJS.read("zhihu_settings_app_conf") == true && /^https?:\/\/appcloud2\.zhihu\.com\/v\d+\/config/.test(magicJS.request.url): - try { - if (!!magicJS.response.body) { - let obj = JSON.parse(magicJS.response.body); - let tab_infos = obj["config"]["homepage_feed_tab"]["tab_infos"].filter((e) => { - if (e.tab_type === "activity_tab") { - e.end_time = (Date.parse(new Date()) - 120000).toString().substr(0, 10); - return true; - } else { - return false; - } - }); - obj["config"]["homepage_feed_tab"]["tab_infos"] = tab_infos; - obj["config"]["zvideo_max_number"] = 1; - response = { body: JSON.stringify(obj) }; - } - } catch (err) { - magicJS.logError(`优化知乎软件配置出现异常:${err}`); - } + response = modifyAppConfig(response); break; // 知乎热搜去广告 case magicJS.read("zhihu_settings_hot_search") == true && /^https?:\/\/api\.zhihu\.com\/search\/top_search\/tabs\/hot\/items/.test(magicJS.request.url): - try { - if (!!magicJS.response.body) { - let obj = JSON.parse(magicJS.response.body); - obj["commercial_data"] = []; - response = { body: JSON.stringify(obj) }; - } - } catch (err) { - magicJS.logError(`去除知乎热搜广告出现异常:${err}`); - } + response = removeHotSearchAds(response); + break; + // 黑名单管理 + case magicJS.read("zhihu_settings_blocked_users") != false && /^https?:\/\/api\.zhihu\.com\/settings\/blocked_users/.test(magicJS.request.url): + manageBlackUser(); break; default: break; @@ -597,131 +78,7 @@ let magicJS = MagicJS(scriptName, "INFO"); } else if (magicJS.isRequest) { // 知乎屏蔽关键词解锁 if (magicJS.read("zhihu_settings_blocked_keywords") != false && /^https?:\/\/api\.zhihu\.com\/feed-root\/block/.test(magicJS.request.url) === true) { - try { - const userInfo = GetUserInfo(); - // 获取屏蔽关键词列表 - if (magicJS.request.method === "GET") { - let keywords = magicJS.read(keywordBlockKey, userInfo.id); - if (!keywords) { - keywords = []; - } - let headers = { - "Cache-Control": "no-cache, no-store, must-revalidate, private, max-age=0", - "Connection": "keep-alive", - "Content-Type": "application/json;charset=utf-8", - "Pragma": "no-cache", - "Referrer-Policy": "no-referrer-when-downgrade", - "Server": "CLOUD ELB 1.0.0", - "Vary": "Accept-Encoding", - "X-Cache-Lookup": "Cache Miss", - "x-cdn-provider": "tencent", - }; - let body = JSON.stringify({ - success: true, - is_vip: true, - kw_min_length: 2, - kw_max_length: 100, - kw_max_count: keywordMaxCount, - data: keywords, - }); - if (magicJS.isQuanX) { - response = { body: body, headers: headers, status: "HTTP/1.1 200 OK" }; - } else { - response = { response: { body: body, headers: headers, status: 200 } }; - } - magicJS.logDebug(`获取本地脚本屏蔽关键词:\n${keywords.join("、")}`); - } - // 添加屏蔽关键词 - else if (magicJS.request.method === "POST") { - if (!!magicJS.request.body) { - // 构造 response headers - let headers = { - "Cache-Control": "no-cache, no-store, must-revalidate, private, max-age=0", - "Connection": "keep-alive", - "Content-Type": "application/json;charset=utf-8", - "Pragma": "no-cache", - "Referrer-Policy": "no-referrer-when-downgrade", - "Server": "CLOUD ELB 1.0.0", - "Vary": "Accept-Encoding", - "X-Cache-Lookup": "Cache Miss", - "x-cdn-provider": "tencent", - }; - // 读取关键词 - let keyword = decodeURIComponent(magicJS.request.body).match(/keyword=(.*)/)[1]; - let keywords = magicJS.read(keywordBlockKey, userInfo.id); - if (!keywords) { - keywords = []; - } - // 判断关键词是否存在 - let keywordExists = false; - for (let i = 0; i < keywords.length; i++) { - if (keyword === keywords[i]) { - keywordExists = true; - } - } - // 不存在添加,存在返回异常 - if (keywordExists === false) { - keywords.push(keyword); - magicJS.write(keywordBlockKey, keywords, userInfo.id); - let body = JSON.stringify({ success: true }); - if (magicJS.isQuanX) { - response = { body: body, headers: headers, status: "HTTP/1.1 200 OK" }; - } else { - response = { response: { body: body, headers: headers, status: 200 } }; - } - magicJS.logDebug(`添加本地脚本屏蔽关键词“${keyword}”`); - } else { - let body = JSON.stringify({ - error: { - message: "关键词已存在", - code: 100002, - }, - }); - if (magicJS.isQuanX) { - response = { - body: body, - headers: headers, - status: "HTTP/1.1 400 Bad Request", - }; - } else { - response = { response: { body: body, headers: headers, status: 400 } }; - } - } - } - } - // 删除屏蔽关键词 - else if (magicJS.request.method === "DELETE") { - let keyword = decodeURIComponent(magicJS.request.url).match(/keyword=(.*)/)[1]; - let keywords = magicJS.read(keywordBlockKey, userInfo.id); - if (!keywords) { - keywords = []; - } - keywords = keywords.filter((e) => { - return e != keyword; - }); - magicJS.write(keywordBlockKey, keywords, userInfo.id); - let headers = { - "Cache-Control": "no-cache, no-store, must-revalidate, private, max-age=0", - "Connection": "keep-alive", - "Content-Type": "application/json;charset=utf-8", - "Pragma": "no-cache", - "Referrer-Policy": "no-referrer-when-downgrade", - "Server": "CLOUD ELB 1.0.0", - "Vary": "Accept-Encoding", - "X-Cache-Lookup": "Cache Miss", - "x-cdn-provider": "tencent", - }; - let body = JSON.stringify({ success: true }); - if (magicJS.isQuanX) { - response = { body: body, headers: headers, status: "HTTP/1.1 200 OK" }; - } else { - response = { response: { body: body, headers: headers, status: 200 } }; - } - magicJS.logDebug(`删除本地脚本屏蔽关键词:“${keyword}”`); - } - } catch (err) { - magicJS.logError(`知乎关键词屏蔽操作出现异常:${err}`); - } + response = unlockBlockedKeywords(response); } } else { magicJS.write(currentUserInfoKey, ""); @@ -736,7 +93,834 @@ let magicJS = MagicJS(scriptName, "INFO"); } })(); -function GetUserInfo() { +/** + * 屏蔽官方营销消息 + * + * @param {*} response + * @return {*} + */ +function removeMarketingMsg(response) { + try { + let obj = JSON.parse(magicJS.response.body); + let newItems = []; + for (let item of obj["data"]) { + if (item["detail_title"] === "官方帐号消息") { + let unread_count = item["unread_count"]; + if (unread_count > 0) { + item["content"]["text"] = "未读消息" + unread_count + "条"; + } else { + item["content"]["text"] = "全部消息已读"; + } + item["is_read"] = true; + item["unread_count"] = 0; + newItems.push(item); + } else if (item["detail_title"] !== "知乎活动助手") { + newItems.push(item); + } + } + obj["data"] = newItems; + response = { body: JSON.stringify(obj) }; + } catch (err) { + magicJS.logError(`知乎屏蔽官方营销消息出现异常:${err}`); + } + return response; +} + +/** + * 知乎屏蔽关键词解锁 + * + * @param {*} response + * @return {*} + */ +function unlockBlockedKeywords(response) { + try { + const userInfo = getUserInfo(); + // 获取屏蔽关键词列表 + if (magicJS.request.method === "GET") { + let keywords = magicJS.read(keywordBlockKey, userInfo.id); + if (!keywords) { + keywords = []; + } + let headers = { + "Cache-Control": "no-cache, no-store, must-revalidate, private, max-age=0", + "Connection": "keep-alive", + "Content-Type": "application/json;charset=utf-8", + "Pragma": "no-cache", + "Referrer-Policy": "no-referrer-when-downgrade", + "Server": "CLOUD ELB 1.0.0", + "Vary": "Accept-Encoding", + "X-Cache-Lookup": "Cache Miss", + "x-cdn-provider": "tencent", + }; + let body = JSON.stringify({ + success: true, + is_vip: true, + kw_min_length: 2, + kw_max_length: 100, + kw_max_count: keywordMaxCount, + data: keywords, + }); + if (magicJS.isQuanX) { + response = { body: body, headers: headers, status: "HTTP/1.1 200 OK" }; + } else { + response = { response: { body: body, headers: headers, status: 200 } }; + } + magicJS.logDebug(`获取本地脚本屏蔽关键词:\n${keywords.join("、")}`); + } + + // 添加屏蔽关键词 + else if (magicJS.request.method === "POST") { + if (!!magicJS.request.body) { + // 构造 response headers + let headers = { + "Cache-Control": "no-cache, no-store, must-revalidate, private, max-age=0", + "Connection": "keep-alive", + "Content-Type": "application/json;charset=utf-8", + "Pragma": "no-cache", + "Referrer-Policy": "no-referrer-when-downgrade", + "Server": "CLOUD ELB 1.0.0", + "Vary": "Accept-Encoding", + "X-Cache-Lookup": "Cache Miss", + "x-cdn-provider": "tencent", + }; + // 读取关键词 + let keyword = decodeURIComponent(magicJS.request.body).match(/keyword=(.*)/)[1]; + let keywords = magicJS.read(keywordBlockKey, userInfo.id); + if (!keywords) { + keywords = []; + } + // 判断关键词是否存在 + let keywordExists = false; + for (let i = 0; i < keywords.length; i++) { + if (keyword === keywords[i]) { + keywordExists = true; + } + } + // 不存在添加,存在返回异常 + if (keywordExists === false) { + keywords.push(keyword); + magicJS.write(keywordBlockKey, keywords, userInfo.id); + let body = JSON.stringify({ success: true }); + if (magicJS.isQuanX) { + response = { body: body, headers: headers, status: "HTTP/1.1 200 OK" }; + } else { + response = { response: { body: body, headers: headers, status: 200 } }; + } + magicJS.logDebug(`添加本地脚本屏蔽关键词“${keyword}”`); + } else { + let body = JSON.stringify({ + error: { + message: "关键词已存在", + code: 100002, + }, + }); + if (magicJS.isQuanX) { + response = { + body: body, + headers: headers, + status: "HTTP/1.1 400 Bad Request", + }; + } else { + response = { response: { body: body, headers: headers, status: 400 } }; + } + } + } + } + + // 删除屏蔽关键词 + else if (magicJS.request.method === "DELETE") { + let keyword = decodeURIComponent(magicJS.request.url).match(/keyword=(.*)/)[1]; + let keywords = magicJS.read(keywordBlockKey, userInfo.id); + if (!keywords) { + keywords = []; + } + keywords = keywords.filter((e) => { + return e != keyword; + }); + magicJS.write(keywordBlockKey, keywords, userInfo.id); + let headers = { + "Cache-Control": "no-cache, no-store, must-revalidate, private, max-age=0", + "Connection": "keep-alive", + "Content-Type": "application/json;charset=utf-8", + "Pragma": "no-cache", + "Referrer-Policy": "no-referrer-when-downgrade", + "Server": "CLOUD ELB 1.0.0", + "Vary": "Accept-Encoding", + "X-Cache-Lookup": "Cache Miss", + "x-cdn-provider": "tencent", + }; + let body = JSON.stringify({ success: true }); + if (magicJS.isQuanX) { + response = { body: body, headers: headers, status: "HTTP/1.1 200 OK" }; + } else { + response = { response: { body: body, headers: headers, status: 200 } }; + } + magicJS.logDebug(`删除本地脚本屏蔽关键词:“${keyword}”`); + } + } catch (err) { + magicJS.logError(`知乎关键词屏蔽操作出现异常:${err}`); + } + return response; +} + +/** + * 知乎热搜去广告 + * + * @param {*} response + * @return {*} + */ +function removeHotSearchAds(response) { + try { + if (!!magicJS.response.body) { + let obj = JSON.parse(magicJS.response.body); + obj["commercial_data"] = []; + response = { body: JSON.stringify(obj) }; + } + } catch (err) { + magicJS.logError(`去除知乎热搜广告出现异常:${err}`); + } + return response; +} + +/** + * 优化知乎软件配置 + * + * @param {*} response + * @return {*} + */ +function modifyAppConfig(response) { + try { + if (!!magicJS.response.body) { + let obj = JSON.parse(magicJS.response.body); + let tab_infos = obj["config"]["homepage_feed_tab"]["tab_infos"].filter((e) => { + if (e.tab_type === "activity_tab") { + e.end_time = (Date.parse(new Date()) - 120000).toString().substr(0, 10); + return true; + } else { + return false; + } + }); + obj["config"]["homepage_feed_tab"]["tab_infos"] = tab_infos; + obj["config"]["zvideo_max_number"] = 1; + response = { body: JSON.stringify(obj) }; + } + } catch (err) { + magicJS.logError(`优化知乎软件配置出现异常:${err}`); + } + return response; +} + +/** + * 去除预置关键字广告 + * + * @param {*} response + * @return {*} + */ +function removeKeywordAds(response) { + try { + if (!!magicJS.response.body) { + magicJS.logDebug(`预置关键字返回:${magicJS.response.body}`); + let obj = JSON.parse(magicJS.response.body); + if (obj.hasOwnProperty("preset_words") && obj["preset_words"]["words"]) { + let words = obj["preset_words"]["words"].filter((element) => { + return element["type"] !== "ad"; + }); + obj["preset_words"]["words"] = words; + response = { body: JSON.stringify(obj) }; + } + } + } catch (err) { + magicJS.logError(`知乎去除预置关键字广告出现异常:${err}`); + } + return response; +} + +/** + * 拦截官方账号推广消息 + * + * @param {*} response + * @return {*} + */ +function removeSysMsgAds(response) { + try { + const sysmsg_blacklist = ["知乎小伙伴", "知乎视频", "知乎团队", "知乎礼券", "知乎读书会团队"]; + let obj = JSON.parse(magicJS.response.body); + let data = obj["data"].filter((element) => { + return sysmsg_blacklist.indexOf(element["content"]["title"]) < 0; + }); + obj["data"] = data; + response = { body: JSON.stringify(obj) }; + } catch (err) { + magicJS.logError(`知乎拦截官方账号推广消息出现异常:${err}`); + } + return response; +} + +/** + * 知乎热榜去广告 + * + * @param {*} response + * @return {*} + */ +function removeHotListAds(response) { + try { + if (!!magicJS.response.body) { + let obj = JSON.parse(magicJS.response.body); + let data = obj["data"].filter((e) => { + return e["type"] === "hot_list_feed" || e["type"] === "hot_list_feed_video"; + }); + obj["data"] = data; + response = { body: JSON.stringify(obj) }; + } + } catch (err) { + magicJS.logError(`去除知乎热搜广告出现异常:${err}`); + } + return response; +} + +/** + * 知乎旧版回答中的评论黑名单增强 + * + * @param {*} response + * @return {*} + */ +function removeCommentAds(response) { + try { + if (!!magicJS.response.body) { + // 评论区去广告 + let obj = JSON.parse(magicJS.response.body); + if (magicJS.read("zhihu_settings_blocked_users") != false) { + // 屏蔽黑名单用户 + let user_info = getUserInfo(); + let customBlockedUsers = magicJS.read(blockedUsersKey, user_info.id); + let newData = []; + obj.data.forEach((comment) => { + // 评论人昵称 + let commentUserName = comment.author.member.name; + // 回复哪个人的评论(仅适用于独立子评论页面请求) + let replyUserName = ""; + if (comment.reply_to_author && comment.reply_to_author.member && comment.reply_to_author.member.name) { + replyUserName = comment.reply_to_author.member.name; + } + if (customBlockedUsers[commentUserName] || customBlockedUsers[replyUserName]) { + if (customBlockedUsers[commentUserName] && !replyUserName && magicJS.request.url.indexOf("root_comment") > 0) { + magicJS.notifyDebug(`屏蔽黑名单用户“${commentUserName}”的主评论。`); + } else if (customBlockedUsers[commentUserName] && !replyUserName && magicJS.request.url.indexOf("child_comment") > 0) { + magicJS.notifyDebug(`屏蔽黑名单用户“${commentUserName}”的子评论。`); + } else if (customBlockedUsers[commentUserName] && replyUserName && magicJS.request.url.indexOf("child_comment") > 0) { + magicJS.notifyDebug(`屏蔽黑名单用户“${commentUserName}”回复“${replyUserName}”的子评论。`); + } else { + magicJS.notifyDebug(`屏蔽“${commentUserName}”回复黑名单用户“${replyUserName}”的子评论。`); + } + // 减少主评论页面中的评论总数(仅适用于独立的主评论页面请求) + if (obj.common_counts) { + obj.common_counts -= 1; + } + // 减少子评论页面中的评论总数(仅适用于独立子评论页面请求) + if (obj.paging && obj.paging.totals) { + obj.paging.totals -= 1; + } + } else { + // 屏蔽子评论中的黑名单用户(仅适用于独立的主评论页面请求) + if (comment.child_comments) { + let newChildComments = []; + comment.child_comments.forEach((childComment) => { + if (customBlockedUsers[childComment.author.member.name] || customBlockedUsers[childComment.reply_to_author.member.name]) { + if (customBlockedUsers[childComment.author.member.name]) { + magicJS.notifyDebug(`屏蔽黑名单用户“${childComment.author.member.name}”的主评论。`); + } else { + magicJS.notifyDebug(`屏蔽“${childComment.author.member.name}”回复黑名单用户“${childComment.reply_to_author.member.name}”的子评论。`); + } + comment.child_comment_count -= 1; + } else { + newChildComments.push(childComment); + } + }); + comment.child_comments = newChildComments; + } + newData.push(comment); + } + }); + obj.data = newData; + } + response = { body: JSON.stringify(obj) }; + } + } catch (err) { + magicJS.logError(`去除知乎评论广告出现异常:${err}`); + } + return response; +} + +/** + * 知乎V5版本评论去广告及黑名单增强 + * + * @param {*} response + * @return {*} + */ +function removeCommentV5Ads(response) { + try { + if (!!magicJS.response.body) { + let obj = JSON.parse(magicJS.response.body); + obj["ad_info"] = {}; + // 屏蔽黑名单用户 + if (magicJS.read("zhihu_settings_blocked_users") != false) { + let user_info = getUserInfo(); + let customBlockedUsers = magicJS.read(blockedUsersKey, user_info.id); + customBlockedUsers = !!customBlockedUsers ? customBlockedUsers : {}; + let newComments = []; + let blockCommentIdObj = {}; + obj.data.forEach((comment) => { + // 评论人昵称 + let commentUserName = comment.author.name; + // 回复哪个人的评论(仅适用于独立子评论页面请求) + let replyUserName = ""; + if (comment.reply_to_author && comment.reply_to_author && comment.reply_to_author.name) { + replyUserName = comment.reply_to_author.name; + } + if (customBlockedUsers[commentUserName] || customBlockedUsers[replyUserName]) { + if (customBlockedUsers[commentUserName] && !replyUserName && magicJS.request.url.indexOf("root_comment") > 0) { + magicJS.notifyDebug(`屏蔽黑名单用户“${commentUserName}”的主评论。`); + } else if (customBlockedUsers[commentUserName] && !replyUserName && magicJS.request.url.indexOf("child_comment") > 0) { + magicJS.notifyDebug(`屏蔽黑名单用户“${commentUserName}”的子评论。`); + } else if (customBlockedUsers[commentUserName] && replyUserName && magicJS.request.url.indexOf("child_comment") > 0) { + magicJS.notifyDebug(`屏蔽黑名单用户“${commentUserName}”回复“${replyUserName}”的子评论。`); + } else { + magicJS.notifyDebug(`屏蔽“${commentUserName}”回复黑名单用户“${replyUserName}”的子评论。`); + } + blockCommentIdObj[comment.id] = commentUserName; + // 主评论数量-1,仅适用于root_comment主评论页面请求 + if (obj.counts && obj.counts.total_counts) { + obj.counts.total_counts -= 1; + } + // 子评论数量-1,仅适用于child_comment子评论页面请求 + if (obj.paging && obj.paging.totals) { + obj.paging.totals -= 1; + } + if (obj.root && obj.root.child_comment_count) { + obj.root.child_comment_count -= 1; + } + } else { + if (comment.child_comments) { + let newChildComments = []; + comment.child_comments.forEach((childComment) => { + let childCommentUserName = childComment.author.name; + if (customBlockedUsers[childCommentUserName] || blockCommentIdObj[childComment.reply_comment_id]) { + if (customBlockedUsers[childCommentUserName]) { + magicJS.notifyDebug(`屏蔽黑名单用户“${childCommentUserName}”的子评论。`); + blockCommentIdObj[childComment.id] = childCommentUserName; + } else { + magicJS.notifyDebug(`屏蔽“${childCommentUserName}”回复黑名单用户“${blockCommentIdObj[childComment.reply_comment_id]}”的子评论。`); + } + comment.child_comment_count -= 1; + } else { + newChildComments.push(childComment); + } + }); + comment.child_comments = newChildComments; + } + newComments.push(comment); + } + }); + obj.data = newComments; + } + response = { body: JSON.stringify(obj) }; + } + } catch (err) { + magicJS.logError(`去除知乎评论广告出现异常:${err}`); + } + return response; +} + +/** + * 回答列表去广告与黑名单增强 + * + * @param {*} response + * @return {*} + */ +function removeQuestionsAds(response) { + try { + const userInfo = getUserInfo(); + let customBlockedUsers = magicJS.read(blockedUsersKey, userInfo.id); + customBlockedUsers = !!customBlockedUsers ? customBlockedUsers : {}; + let obj = JSON.parse(magicJS.response.body); + magicJS.logDebug(`当前黑名单列表: ${JSON.stringify(customBlockedUsers)}`); + delete obj["ad_info"]; + delete obj["roundtable_info"]; + let data = obj["data"].filter((element) => { + return !customBlockedUsers[element["author"]["name"]]; + }); + obj["data"] = data; + response = { body: JSON.stringify(obj) }; + } catch (err) { + magicJS.logError(`知乎回答列表去广告出现异常:${err}`); + } + return response; +} + +/** + * 关注列表去广告 + * + * @param {*} response + * @return {*} + */ +function removeMomentsAds(response) { + try { + let obj = JSON.parse(magicJS.response.body.replace(/(\w+"+\s?):\s?(\d{15,})/g, '$1:"$2"')); + const user_info = getUserInfo(); + let customBlockedUsers = magicJS.read(blockedUsersKey, user_info.id); + customBlockedUsers = !!customBlockedUsers ? customBlockedUsers : {}; + let data = []; + + const settings_moments_stream = magicJS.read("zhihu_settings_moments_stream") == true; + const settings_blocked_users = magicJS.read("zhihu_settings_blocked_users") != false; + + for (let i = 0; i < obj["data"].length; i++) { + // let element = targetIdFix(obj["data"][i]); + let element = obj["data"][i]; + if (!element["ad"] && !element["adjson"] && !element["ad_list"]) { + // 判断转发的想法是否含有黑名单用户 + if (settings_blocked_users && element.target && element.target.origin_pin && element.target.origin_pin.author && customBlockedUsers[element.target.origin_pin.author.name]) { + magicJS.notifyDebug(`屏蔽“${element.target.author.name}”转发黑名单用户“${element.target.origin_pin.author.name}”的想法。`); + } + + // 屏蔽关注页的“最新视频” + else if (!settings_moments_stream || element["type"] != "videos") { + data.push(element); + } + } + } + obj["data"] = data; + response = { body: JSON.stringify(obj) }; + } catch (err) { + magicJS.logError(`知乎关注列表去广告出现异常:${err}`); + } + return response; +} + +/** + * 推荐去广告与黑名单增强 + * + * @param {*} response + * @return {*} + */ +function removeRecommendAds(response) { + try { + const settings_remove_yanxuan = magicJS.read("zhihu_settings_remove_yanxuan") == true; + const settings_recommend_stream = magicJS.read("zhihu_settings_recommend_stream") == true; + // 默认开启 + const settings_blocked_keywords = magicJS.read("zhihu_settings_blocked_keywords") != false; + const settings_blocked_users = magicJS.read("zhihu_settings_blocked_users") != false; + const user_info = getUserInfo(); + + let keywords = magicJS.read(keywordBlockKey, user_info.id); + keywords = settings_blocked_keywords && !!keywords ? keywords : []; + let customBlockedUsers = magicJS.read(blockedUsersKey, user_info.id); + customBlockedUsers = settings_blocked_users && !!customBlockedUsers ? customBlockedUsers : {}; + + const dataFilter = (element) => { + let elementStr = JSON.stringify(element); + // 是否为广告 + let isAd = element["card_type"] === "slot_event_card" || element["card_type"] === "slot_video_event_card" || element.hasOwnProperty("ad"); + // 是否为流媒体 element["extra"]["type"] === "drama" || element["extra"]["type"] === "zvideo") || element["extra"]["type"] === "Video" || element["common_card"]["style"] === "BIG_IMAGE") + let isStream = isAd != true && settings_recommend_stream && elementStr.search(/"(type|style)+"\s?:\s?"(drama|zvideo|Video|BIG_IMAGE)+"/i) >= 0; + // 是否为盐选推荐 + let isYanXuan = isStream != true && settings_remove_yanxuan && elementStr.indexOf("https://pic2.zhimg.com/80/v2-c46fc8ec4c4e9ffc8f846ae0d8158a80_1440w.png") >= 0; + // 是否匹配脚本关键词过滤 + let matchKeyword = false; + if (isStream != true && settings_blocked_keywords) { + for (let i = 0; i < keywords.length; i++) { + if (elementStr.search(keywords[i]) >= 0) { + if (magicJS.isDebug) { + let elementTitle = element.common_card.feed_content.title.panel_text; + let elementContent = element.common_card.feed_content.content.panel_text; + let actionUrl = ""; + try { + actionUrl = element.common_card.feed_content.title.action.intent_url; + } catch {} + magicJS.logDebug(`匹配关键字:\n${keywords[i]}\n标题:\n${elementTitle}\n内容:\n${elementContent}`); + magicJS.notifyDebug(scriptName, `关键字:${keywords[i]}`, `${elementTitle}\n${elementContent}`, actionUrl); + } + matchKeyword = true; + break; + } + } + } + // 是否为黑名单用户 + let isBlockedUser = false; + try { + isBlockedUser = + matchKeyword != true && settings_blocked_users && customBlockedUsers && element["common_card"]["feed_content"]["source_line"]["elements"][1]["text"]["panel_text"] in customBlockedUsers; + } catch { + isBlockedUser = false; + } + return !(isAd || isStream || isYanXuan || matchKeyword || isBlockedUser); + }; + + // 修复number类型精度丢失 + let obj = JSON.parse(magicJS.response.body.replace(/(\w+"+\s?):\s?(\d{15,})/g, '$1:"$2"')); + obj["data"] = obj["data"].filter(dataFilter); + response = { body: JSON.stringify(obj) }; + } catch (err) { + magicJS.logError(`知乎推荐列表去广告出现异常:${err}`); + } + return response; +} + +/** + * 黑名单增强 - 浏览黑名单用户信息时自动加入脚本黑名单 + * + * @param {*} response + * @return {*} + */ +function autoInsertBlackList(response) { + try { + let obj = JSON.parse(magicJS.response.body); + // 删除MCN信息 + delete obj["mcn_user_info"]; + response = { body: JSON.stringify(obj) }; + // 如已是黑名单用户,但不在脚本黑名单中,则自动加入 + if (obj.name && obj.id && obj.is_blocking === true) { + const userInfo = getUserInfo(); + let customBlockedUsers = magicJS.read(blockedUsersKey, userInfo.id); + customBlockedUsers = typeof customBlockedUsers === "object" && !!customBlockedUsers ? customBlockedUsers : {}; + if (!customBlockedUsers[obj.name]) { + magicJS.logDebug(`当前需要加入黑名单的用户Id:${obj["id"]},用户名:${obj["name"]}`); + customBlockedUsers[obj["name"]] = obj["id"]; + magicJS.write(blockedUsersKey, customBlockedUsers, userInfo.id); + magicJS.logDebug(`${obj["name"]}写入脚本黑名单成功,当前脚本黑名单数据:${JSON.stringify(customBlockedUsers)}`); + magicJS.notify(`已自动将用户“${obj["name"]}”写入脚本黑名单。`); + } + } + } catch (err) { + magicJS.logError(`知乎去除MCN信息出现异常:${err}`); + } + return response; +} + +/** + * 处理登录用户信息 + * + * @param {*} response + * @return {*} + */ +function processUserInfo(response) { + try { + let obj = JSON.parse(magicJS.response.body); + magicJS.logDebug(`用户登录用户信息,接口响应:${magicJS.response.body}`); + if (obj && obj["id"] && obj.hasOwnProperty("vip_info") && obj["vip_info"].hasOwnProperty("is_vip")) { + const userInfo = { + id: obj["id"], + is_vip: obj["vip_info"]["is_vip"] ? obj["vip_info"]["is_vip"] !== undefined : false, + }; + magicJS.logDebug(`当前用户id:${obj["id"]},是否为VIP:${obj["vip_info"]["is_vip"]}`); + magicJS.write(currentUserInfoKey, userInfo); + // 在知乎APP显示VIP,仅自己可见,无实际用途 + if (magicJS.read("zhihu_settings_fake_vip") == true && obj["vip_info"]["is_vip"] === false) { + obj["vip_info"]["is_vip"] = true; + obj["vip_info"]["vip_icon"] = { + url: "https://pic1.zhimg.com/v2-4812630bc27d642f7cafcd6cdeca3d7a_r.png", + night_mode_url: "https://pic1.zhimg.com/v2-c9686ff064ea3579730756ac6c289978_r.png", + }; + obj["vip_info"]["entrance"] = { + icon: { + url: "https://pic1.zhimg.com/v2-5b7012c8c22fd520f5677305e1e551bf.png", + night_mode_url: "https://pic1.zhimg.com/v2-e51e3252d7a2cb016a70879defd5da0b.png", + }, + title: "我的盐选会员", + expires_day: "2033-12-24", + sub_title: null, + button_text: "你好,知乎!", + jump_url: "zhihu://vip/purchase", + button_jump_url: "zhihu://vip/purchase", + sub_title_new: null, + identity: "svip", + }; + obj["vip_info"]["entrance_new"] = { + left_button: { + title: "精选会员内容", + description: "为您严选好内容", + jump_url: "zhihu://market/home", + }, + right_button: { + title: "我的盐选会员", + description: "畅享 10w+ 优质内容", + jump_url: "zhihu://vip/my", + }, + }; + obj["vip_info"]["entrance_v2"] = { + title: "我的盐选会员", + sub_title: "畅享 10w+ 优质内容", + jump_url: "zhihu://vip/my", + button_text: "查看权益", + }; + response = { body: JSON.stringify(obj) }; + } + } else { + magicJS.logWarning(`没有获取到本次登录用户信息,如未对功能造成影响,请忽略此日志。`); + } + } catch (err) { + magicJS.logError(`知乎获取当前用户信息出现异常:${err}`); + } + return response; +} + +/** + * 回答内容优化 + * + * @param {*} response + * @return {*} + */ +function modifyAnswer(response) { + try { + let html = magicJS.response.body; + // 付费内容提醒 + if ((html.indexOf("查看完整内容") >= 0 || html.indexOf("查看全部章节") >= 0) && html.indexOf("paid") >= 0) { + let matchStr = html.match(/(richText[^<]*>)(.)/)[1]; + let start = html.lastIndexOf(matchStr) + matchStr.length; + let insertText = + '
知乎助手 · 本文为付费内容
'; + response = { body: html.slice(0, start) + insertText + html.slice(start) }; + } + + // 营销推广提醒 + else if (html.indexOf("ad-link-card") >= 0 || html.indexOf("xg.zhihu.com") >= 0 || html.indexOf("知乎营销平台") >= 0) { + let matchStr = html.match(/(richText[^<]*>)(.)/)[1]; + let start = html.lastIndexOf(matchStr) + matchStr.length; + let insertText = + '
知乎助手 · 本文含有营销推广
'; + response = { body: html.slice(0, start) + insertText + html.slice(start) }; + } + + // 购物推广提醒 + else if (html.indexOf("mcn-link-card") >= 0) { + let matchStr = html.match(/(richText[^<]*>)(.)/)[1]; + let start = html.lastIndexOf(matchStr) + matchStr.length; + let insertText = + '
知乎助手 · 本文含有购物推广
'; + response = { body: html.slice(0, start) + insertText + html.slice(start) }; + } + + // 彩蛋 + else if (Math.floor(Math.random() * 200) == 7) { + let matchStr = html.match(/(richText[^<]*>)(.)/)[1]; + let start = html.lastIndexOf(matchStr) + matchStr.length; + let insertText = + '
知乎助手 · 本文为免费内容
'; + response = { body: html.slice(0, start) + insertText + html.slice(start) }; + } + } catch (err) { + magicJS.logError(`知乎付费内容提醒出现异常:${err}`); + } + return response; +} + +/** + * 黑名单管理 + * + */ +function manageBlackUser() { + const userInfo = getUserInfo(); + let defaultBlockedUsers = {}; + let customBlockedUsers = magicJS.read(blockedUsersKey, userInfo.id); + customBlockedUsers = typeof customBlockedUsers === "object" && !!customBlockedUsers ? customBlockedUsers : {}; + defaultAnswerBlockedUsers.forEach((element) => { + customBlockedUsers[element] = "00000000000000000000000000000000"; + defaultBlockedUsers[element] = "00000000000000000000000000000000"; + }); + magicJS.logDebug(`当前用户id:${userInfo.id},脚本黑名单:${JSON.stringify(customBlockedUsers)}`); + // 获取黑名单 + if (magicJS.request.method == "GET") { + try { + // 加载黑名单首页时,清空历史黑名单,仅保留脚本默认黑名单 + if (magicJS.request.url.indexOf("offset") < 0) { + customBlockedUsers = defaultBlockedUsers; + magicJS.logDebug("脚本黑名单已清空,请滑动至黑名单末尾保证重新获取完成。"); + magicJS.notify("脚本黑名单已清空,请滑动至黑名单末尾保证重新获取完成。"); + } + let obj = JSON.parse(magicJS.response.body); + if (!!obj["data"]) { + magicJS.logDebug(`本次滑动获取的黑名单信息:${JSON.stringify(obj["data"])}`); + obj["data"].forEach((element) => { + if (element["name"] != "[已重置]") { + customBlockedUsers[element["name"]] = element["id"]; + } + }); + magicJS.write(blockedUsersKey, customBlockedUsers, userInfo.id); + if (obj["paging"]["is_end"] == true) { + magicJS.notify(`获取脚本黑名单结束,当前黑名单共${Object.keys(customBlockedUsers).length - defaultAnswerBlockedUsers.length}人。\n脚本内置黑名单${defaultAnswerBlockedUsers.length}人。`); + magicJS.logDebug(`脚本黑名单内容:${JSON.stringify(customBlockedUsers)}。`); + } + } else { + magicJS.logWarning(`获取黑名单失败,接口响应不合法:${magicJS.response.body}`); + } + } catch (err) { + magicJS.del(blockedUsersKey); + magicJS.logError(`获取黑名单失败,异常信息:${err}`); + magicJS.notify("获取黑名单失败,执行异常,已清空黑名单。"); + } + } + // 写入黑名单 + else if (magicJS.request.method == "POST") { + try { + let obj = JSON.parse(magicJS.response.body); + if (obj.hasOwnProperty("name") && obj.hasOwnProperty("id")) { + magicJS.logDebug(`当前需要加入黑名单的用户Id:${obj["id"]},用户名:${obj["name"]}`); + if (obj["id"]) { + customBlockedUsers[obj["name"]] = obj["id"]; + magicJS.write(blockedUsersKey, customBlockedUsers, userInfo.id); + magicJS.logDebug(`${obj["name"]}写入脚本黑名单成功,当前脚本黑名单数据:${JSON.stringify(customBlockedUsers)}`); + magicJS.notify(`已将用户“${obj["name"]}”写入脚本黑名单。`); + } else { + magicJS.logError(`${obj["name"]}写入脚本黑名单失败,没有获取到用户Id。`); + magicJS.notify(`将用户“${obj["name"]}”写入脚本黑名单失败!`); + } + } else { + magicJS.logWarning(`写入黑名单失败,接口响应不合法:${magicJS.response.body}`); + magicJS.notify("写入脚本黑名单失败,接口返回不合法。"); + } + } catch (err) { + magicJS.logError(`写入黑名单失败,异常信息:${err}`); + magicJS.notify("写入脚本黑名单失败,执行异常,请查阅日志。"); + } + } + // 移出黑名单 + else if (magicJS.request.method == "DELETE") { + try { + let obj = JSON.parse(magicJS.response.body); + if (obj.success) { + let user_id = magicJS.request.url.match(/https?:\/\/api\.zhihu\.com\/settings\/blocked_users\/([0-9a-zA-Z]*)/)[1]; + if (user_id) { + magicJS.logDebug(`当前需要移出黑名单的用户Id:${user_id}`); + for (let username in customBlockedUsers) { + if (customBlockedUsers[username] == user_id) { + delete customBlockedUsers[username]; + magicJS.write(blockedUsersKey, customBlockedUsers, userInfo.id); + magicJS.logDebug(`${username}移出脚本黑名单成功,当前脚本黑名单数据:${JSON.stringify(customBlockedUsers)}`); + magicJS.notify(`已将用户“${username}”移出脚本黑名单!`); + break; + } + } + } else { + magicJS.logError("将用户移出脚本黑名单失败!\n建议从设置中刷新黑名单数据。"); + magicJS.notify(`将用户移出脚本黑名单失败,没有获取到用户Id。\n建议从设置中刷新黑名单数据。`); + } + } else { + magicJS.logWarning(`移出黑名单失败,接口响应不合法:${magicJS.response.body}`); + magicJS.notify("移出脚本黑名单失败,接口返回不合法。"); + } + } catch (err) { + magicJS.logError(`移出黑名单失败,异常信息:${err}`); + magicJS.notify("移出脚本黑名单失败,执行异常,请查阅日志。"); + } + } +} + +/** + * 获取用户信息 + * + * @return {*} + */ +function getUserInfo() { let defaultUserInfo = { id: "default", is_vip: false }; try { const userInfo = magicJS.read(currentUserInfoKey); diff --git a/script/zhihu/zhihu_plus.lnplugin b/script/zhihu/zhihu_plus.lnplugin index 071aae36e..0acea975b 100644 --- a/script/zhihu/zhihu_plus.lnplugin +++ b/script/zhihu/zhihu_plus.lnplugin @@ -4,7 +4,7 @@ # 知乎去广告 DOMAIN,118.89.204.198,REJECT IP-CIDR,118.89.204.198/32,REJECT,no-resolve -IP-CIDR6,2402:4e00:1200:ed00:0:9089:6dac:96b6/128,REJECT-DROP,no-resolve +IP-CIDR6,2402:4e00:1200:ed00:0:9089:6dac:96b6/128,REJECT,no-resolve DOMAIN,appcloud2.in.zhihu.com,REJECT DOMAIN,mqtt.zhihu.com,REJECT DOMAIN,sugar.zhihu.com,REJECT @@ -40,6 +40,8 @@ URL-REGEX,^https?:\/\/www\.zhihu\.com\/api\/v\d+\/brand\/question/\d+/card\?,REJ ^https?:\/\/api\.zhihu\.com\/ab\/api\/v1\/products\/zhihu\/platforms\/ios\/config - reject-200 # 屏蔽知乎“我的”页面推荐开通会员的卡片 ^https?:\/\/api\.zhihu\.com\/people\/self\/new_user_card - reject-200 +# 知乎去除Tab页关注人头像 +^https?:\/\/api\.zhihu\.com\/moments\/tab_v2 - reject-dict [Script] http-request ^https?:\/\/api\.zhihu\.com\/feed-root\/block requires-body=1,script-path=https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/script/zhihu/zhihu_plus.js,tag=知乎_屏蔽关键字解锁 diff --git a/script/zhihu/zhihu_plus.qxrewrite b/script/zhihu/zhihu_plus.qxrewrite index 0a4fbd2e7..0d0a65ca7 100644 --- a/script/zhihu/zhihu_plus.qxrewrite +++ b/script/zhihu/zhihu_plus.qxrewrite @@ -44,6 +44,8 @@ ^https?:\/\/api\.zhihu\.com\/ab\/api\/v1\/products\/zhihu\/platforms\/ios\/config url reject-200 # 屏蔽知乎“我的”页面推荐开通会员的卡片 ^https?:\/\/api\.zhihu\.com\/people\/self\/new_user_card url reject-200 +# 知乎去除Tab页关注人头像 +^https?:\/\/api\.zhihu\.com\/moments\/tab_v2 url reject-dict # hostname = www.zhihu.com,api.zhihu.com,appcloud2.zhihu.com,zhuanlan.zhihu.com hostname = *.zhihu.com diff --git a/script/zhihu/zhihu_plus.sgmodule b/script/zhihu/zhihu_plus.sgmodule index dfa80067d..2f975be90 100644 --- a/script/zhihu/zhihu_plus.sgmodule +++ b/script/zhihu/zhihu_plus.sgmodule @@ -12,6 +12,8 @@ RULE-SET,https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/s ^https?:\/\/api\.zhihu\.com\/people\/self\/new_user_card - reject [Map Local] +# 知乎去除Tab页关注人头像 +^https?:\/\/api\.zhihu\.com\/moments\/tab_v2 data="https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/blank/blank_dict.json" # 知乎去除最常访问 ^https?:\/\/api\.zhihu\.com\/moments\/recent data="https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/blank/blank_dict.json" # 知乎其他广告拦截