Rules-For-Quantumult-X/Scripts/AdBlock/Other/Tieba-2.js

107 lines
118 KiB
JavaScript
Raw Normal View History

2023-09-04 18:02:16 +08:00
// protobuf.jsv7.2.3(做了部分修改 i.util.global.protobuf=i -> protobuf=i.util.global.protobuf=i)
let protobuf;!function(g){"use strict";!function(r,e,t){var i=function t(i){var n=e[i];return n||r[i][0].call(n=e[i]={exports:{}},t,n,n.exports),n.exports}(t[0]);protobuf=i.util.global.protobuf=i,"function"==typeof define&&define.amd&&define(["long"],function(t){return t&&t.isLong&&(i.util.Long=t,i.configure()),i}),"object"==typeof module&&module&&module.exports&&(module.exports=i)}({1:[function(t,i,n){i.exports=function(t,i){var n=Array(arguments.length-1),s=0,r=2,u=!0;for(;r<arguments.length;)n[s++]=arguments[r++];return new Promise(function(r,e){n[s]=function(t){if(u)if(u=!1,t)e(t);else{for(var i=Array(arguments.length-1),n=0;n<i.length;)i[n++]=arguments[n];r.apply(null,i)}};try{t.apply(i||null,n)}catch(t){u&&(u=!1,e(t))}})}},{}],2:[function(t,i,n){n.length=function(t){var i=t.length;if(!i)return 0;for(var n=0;1<--i%4&&"="==(t[0|i]||"");)++n;return Math.ceil(3*t.length)/4-n};for(var f=Array(64),h=Array(123),r=0;r<64;)h[f[r]=r<26?r+65:r<52?r+71:r<62?r-4:r-59|43]=r++;n.encode=function(t,i,n){for(var r,e=null,s=[],u=0,o=0;i<n;){var h=t[i++];switch(o){case 0:s[u++]=f[h>>2],r=(3&h)<<4,o=1;break;case 1:s[u++]=f[r|h>>4],r=(15&h)<<2,o=2;break;case 2:s[u++]=f[r|h>>6],s[u++]=f[63&h],o=0}8191<u&&((e=e||[]).push(String.fromCharCode.apply(String,s)),u=0)}return o&&(s[u++]=f[r],s[u++]=61,1===o&&(s[u++]=61)),e?(u&&e.push(String.fromCharCode.apply(String,s.slice(0,u))),e.join("")):String.fromCharCode.apply(String,s.slice(0,u))};var c="invalid encoding";n.decode=function(t,i,n){for(var r,e=n,s=0,u=0;u<t.length;){var o=t.charCodeAt(u++);if(61==o&&1<s)break;if((o=h[o])===g)throw Error(c);switch(s){case 0:r=o,s=1;break;case 1:i[n++]=r<<2|(48&o)>>4,r=o,s=2;break;case 2:i[n++]=(15&r)<<4|(60&o)>>2,r=o,s=3;break;case 3:i[n++]=(3&r)<<6|o,s=0}}if(1===s)throw Error(c);return n-e},n.test=function(t){return/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(t)}},{}],3:[function(t,i,n){function a(i,n){"string"==typeof i&&(n=i,i=g);var h=[];function f(t){if("string"!=typeof t){var i=c();if(a.verbose&&console.log("codegen: "+i),i="return "+i,t){for(var n=Object.keys(t),r=Array(n.length+1),e=Array(n.length),s=0;s<n.length;)r[s]=n[s],e[s]=t[n[s++]];return r[s]=i,Function.apply(null,r).apply(null,e)}return Function(i)()}for(var u=Array(arguments.length-1),o=0;o<u.length;)u[o]=arguments[++o];if(o=0,t=t.replace(/%([%dfijs])/g,function(t,i){var n=u[o++];switch(i){case"d":case"f":return""+ +(""+n);case"i":return""+Math.floor(n);case"j":return JSON.stringify(n);case"s":return""+n}return"%"}),o!==u.length)throw Error("parameter count mismatch");return h.push(t),f}function c(t){return"function "+(t||n||"")+"("+(i&&i.join(",")||"")+"){\n "+h.join("\n ")+"\n}"}return f.toString=c,f}(i.exports=a).verbose=!1},{}],4:[function(t,i,n){function r(){this.t={}}(i.exports=r).prototype.on=function(t,i,n){return(this.t[t]||(this.t[t]=[])).push({fn:i,ctx:n||this}),this},r.prototype.off=function(t,i){if(t===g)this.t={};else if(i===g)this.t[t]=[];else for(var n=this.t[t],r=0;r<n.length;)n[r].fn===i?n.splice(r,1):++r;return this},r.prototype.emit=function(t){var i=this.t[t];if(i){for(var n=[],r=1;r<arguments.length;)n.push(arguments[r++]);for(r=0;r<i.length;)i[r].fn.apply(i[r++].ctx,n)}return this}},{}],5:[function(t,i,n){i.exports=o;var s=t(1),u=t(7)("fs");function o(n,r,e){return r="function"==typeof r?(e=r,{}):r||{},e?!r.xhr&&u&&u.readFile?u.readFile(n,function(t,i){return t&&"undefined"!=typeof XMLHttpRequest?o.xhr(n,r,e):t?e(t):e(null,r.binary?i:i.toString("utf8"))}):o.xhr(n,r,e):s(o,this,n,r)}o.xhr=function(t,n,r){var e=new XMLHttpRequest;e.onreadystatechange=function(){if(4!==e.readyState)return g;if(0!==e.status&&200!==e.status)return r(Error("status "+e.status));if(n.binary){if(!(t=e.response))for(var t=[],i=0;i<e.responseText.length;++i)t.push(255&e.responseText.charCodeAt(i));return r(null,"undefined"!=typeof Uint8Array?new Uint8Array(t):t)}return r(null,e.responseText)},n.binary&&("overrideMimeType"in e&&e.overrideMimeType("text/plain; charset=x-user-defined"),e.responseType="arraybuffer"),e.open("GET",t),e.send()}},{1:1,7:7}],6:[function(t,
// pbjs .proto转Json 注意下划线转为了驼峰 已去除goodsInfo BusinessPromot LiveFuseForumData
const tiebaJson = {"nested":{"model":{"nested":{"Abstract":{"fields":{"text":{"type":"string","id":2},"type":{"type":"int32","id":1}}},"ActionControl":{"fields":{"name":{"type":"string","id":2},"textColor":{"type":"string","id":3},"textColorPressed":{"type":"string","id":4},"url":{"type":"string","id":1}}},"AdCloseInfo":{"fields":{"actionControl":{"type":"ActionControl","id":5},"reasons":{"rule":"repeated","type":"string","id":3},"supportClose":{"type":"int32","id":1}}},"Advertisement":{"fields":{"advertisementId":{"type":"string","id":5},"clickStatisticsUrl":{"type":"string","id":7},"deeplink":{"type":"string","id":9},"displayAdIcon":{"type":"string","id":12},"floatingText":{"type":"string","id":8},"jumpLink":{"type":"string","id":4},"packageName":{"type":"string","id":11},"pic":{"type":"string","id":2},"picClick":{"type":"string","id":3},"scheme":{"type":"string","id":10},"time":{"type":"int64","id":1},"viewStatisticsUrl":{"type":"string","id":6}}},"AgreeBanner":{"fields":{"iconUrl":{"type":"string","id":2},"text":{"type":"string","id":1},"topAgreeUser":{"rule":"repeated","type":"SimpleUser","id":4},"url":{"type":"string","id":3}}},"SimpleUser":{"fields":{"portrait":{"type":"string","id":8},"userId":{"type":"int64","id":1},"userName":{"type":"string","id":5}}},"Agree":{"fields":{"agreeNum":{"type":"int64","id":1},"agreeType":{"type":"int32","id":3},"diffAgreeNum":{"type":"int64","id":5},"disagreeNum":{"type":"int64","id":4},"hasAgree":{"type":"int32","id":2},"lzAgree":{"type":"int32","id":6}}},"AlaChallengeInfo":{"fields":{"challengeId":{"type":"int64","id":1},"rivalHlsUrl":{"type":"string","id":5},"rivalRtmpUrl":{"type":"string","id":4},"rivalSession":{"type":"string","id":3},"winningNum":{"type":"int64","id":2}}},"AlaLiveInfo":{"fields":{"audienceCount":{"type":"uint32","id":14},"challengeInfo":{"type":"AlaChallengeInfo","id":25},"cover":{"type":"string","id":2},"coverWide":{"type":"string","id":48},"description":{"type":"string","id":11},"dislikeInfo":{"rule":"repeated","type":"DislikeInfo","id":43},"firstHeadline":{"type":"string","id":34},"groupId":{"type":"uint64","id":6},"hlsUrl":{"type":"string","id":5},"liveFrom":{"type":"int32","id":29},"liveId":{"type":"uint64","id":1},"liveStatus":{"type":"int32","id":18},"liveType":{"type":"uint32","id":15},"openRecomDuration":{"type":"uint32","id":41},"openRecomFans":{"type":"uint32","id":40},"openRecomLocation":{"type":"uint32","id":39},"openRecomReason":{"type":"uint32","id":38},"pbDisplayType":{"type":"uint32","id":36},"recomReason":{"type":"string","id":37},"roomId":{"type":"uint64","id":44},"routerType":{"type":"string","id":32},"rtmpUrl":{"type":"string","id":4},"screenDirection":{"type":"uint32","id":16},"secondHeadline":{"type":"string","id":35},"sessionId":{"type":"string","id":3},"shareInfo":{"type":"AlaShareInfo","id":19},"thirdAppId":{"type":"string","id":21},"thirdLiveType":{"type":"string","id":33},"thirdRoomId":{"type":"string","id":31},"userInfo":{"type":"AlaUserInfo","id":12},"yyExt":{"type":"YyExt","id":49},"labelName":{"type":"string","id":17},"userLabel":{"type":"UniversalLabel","id":51}}},"DislikeInfo":{"fields":{"dislikeId":{"type":"uint32","id":2},"dislikeReason":{"type":"string","id":1},"extra":{"type":"string","id":3}}},"AlaShareInfo":{"fields":{"shareCount":{"type":"int32","id":3},"shareUserCount":{"type":"int32","id":2}}},"AlaUserInfo":{"fields":{"alaId":{"type":"uint64","id":1},"anchorFans":{"type":"int32","id":24},"anchorLive":{"type":"uint64","id":7},"enterLive":{"type":"uint64","id":6},"lat":{"type":"double","id":12},"liveId":{"type":"uint64","id":9},"liveStatus":{"type":"uint32","id":8},"lng":{"type":"double","id":11},"location":{"type":"string","id":10},"portrait":{"type":"string","id":3},"userId":{"type":"uint64","id":23},"userName":{"type":"string","id":2},"yyExt":{"type":"YyExt","id":25}}},"YyExt":{"fields":{"iconUrl":{"type":"string","id":8},"isYyGame":{"type":"int32","id":5},"rankShow":{"type":"string","id":7},"sid":{"type":"string","id":1},"ssid":{"type":"string","id":2},"streamInfo":{"type":"string","id":6},"templateId":{"type":"st
const tiebaRoot = protobuf.Root.fromJSON(tiebaJson);
const url = $request.url;
const method = $request.method;
const isQuanX = typeof $task !== "undefined";
const binaryBody = isQuanX ? new Uint8Array($response.bodyBytes) : $response.body;
let body;
console.log(`2023-06-15`);
if($request.method !== 'POST'){
$notification.post('贴吧proto去广告脚本错误', "请求方法不为POST:", url);
}
if (url.includes("frs/page")) {
console.log('贴吧-FrsPage');
// model.frs.FrsPageResIdl 并不是json中的完整路径,但可以使用
let frsPageResIdlType = tiebaRoot.lookupType("model.frs.FrsPageResIdl");
let frsPageResIdlObj = frsPageResIdlType.decode(binaryBody);
frsPageResIdlObj.data.threadList = removeLive(frsPageResIdlObj.data.threadList);
if(frsPageResIdlObj.data.activityhead?.isAd){
console.log('frs去除吧内header图片广告');
frsPageResIdlObj.data.activityhead = null;
} else {
console.log('无需处理activityhead');
}
body = frsPageResIdlType.encode(frsPageResIdlObj).finish();
} else if (url.includes("frs/threadlist")) {
console.log('贴吧-threadlist');
let threadListResIdlType = tiebaRoot.lookupType("model.threadlist.ThreadListResIdl");
let threadListResIdlObj = threadListResIdlType.decode(binaryBody);
body = threadListResIdlType.encode(threadListResIdlObj).finish();
} else if (url.includes("pb/page")) {
console.log('贴吧-PbPage');
let pbPageResIdlType = tiebaRoot.lookupType("model.pb.PbPageResIdl");
let pbPageResIdlObj = pbPageResIdlType.decode(binaryBody);
if(pbPageResIdlObj.data.postList?.length){
for (const post of pbPageResIdlObj.data.postList) {
if(post.outerItem){
console.log('outer_item去除');
post.outerItem = null;
}
}
} else {
console.log('无需处理postList中的outer_item');
}
if(pbPageResIdlObj.data.recomAlaInfo?.liveId){
console.log('帖子详情页推荐的直播广告去除');
pbPageResIdlObj.data.recomAlaInfo = null;
} else {
console.log('帖子详情页无直播广告');
}
body = pbPageResIdlType.encode(pbPageResIdlObj).finish();
} else if (url.includes("excellent/personalized")) {
console.log('贴吧-personalized');
let personalizedResIdlType = tiebaRoot.lookupType("model.personalized.PersonalizedResIdl");
let personalizedResIdlObj = personalizedResIdlType.decode(binaryBody);
personalizedResIdlObj.data.threadList = removeLive(personalizedResIdlObj.data.threadList);
if(personalizedResIdlObj.data.liveAnswer){
console.log('去除推荐页上方的banner广告');
personalizedResIdlObj.data.liveAnswer = null;
} else {
console.log('推荐页无banner广告');
}
body = personalizedResIdlType.encode(personalizedResIdlObj).finish();
} else if (url.includes("frs/generalTabList")) {
console.log('贴吧-generalTabList');
let generalTabListResIdlType = tiebaRoot.lookupType("model.generaltablelist.GeneralTabListResIdl");
let generalTabListResIdlObj = generalTabListResIdlType.decode(binaryBody);
body = generalTabListResIdlType.encode(generalTabListResIdlObj).finish();
} else {
$notification.post('贴吧proto去广告脚本错误', "url匹配错误:", url);
}
// body.byteLength 和 body.buffer.byteLength 不一定相同 (如帖子没有回复/少量回复时)
console.log(`${body.byteLength}---${body.buffer.byteLength}`);
if(isQuanX){
$done({bodyBytes: body.buffer.slice(body.byteOffset, body.byteLength + body.byteOffset)});
} else {
$done({body});
}
function removeLive(threadList) {
let newThreadList = threadList;
const beforeLength = threadList?.length;
if(beforeLength){
newThreadList = threadList.filter(item => {
if(item.alaInfo){
console.log('去除推荐的直播帖子');
return false;
}
return true;
});
if(beforeLength === newThreadList.length){
console.log("无推荐的直播帖子");
}
} else {
console.log('无需处理threadList');
}
return newThreadList;
}