Skip to content

Commit

Permalink
perf: 优化hack函数
Browse files Browse the repository at this point in the history
  • Loading branch information
Haochen Duan committed May 14, 2024
1 parent 369d649 commit f007190
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
script.
/**
* Fix Pocket 48's verification problem for UA and client-type.
*/
/** Fix Pocket 48's verification problem for UA and client-type. */
(function() {
/**
* @param { Record<string, any> } obj
* @param { (k: string, v: any) => boolean } callback
*/
function objectSome(obj, callback) {
let result = false;

Expand All @@ -17,11 +19,15 @@ script.
return result;
}

globalThis.WebSocket2 = WebSocket;
/**
* Rewrite the WebSocket's send method to fix the verification problem.
* @class HACK_INTERCEPTS_SEND_Websocket
*/
class HACK_INTERCEPTS_SEND_Websocket extends WebSocket {}

WebSocket2.prototype.$_SEND = WebSocket2.prototype.send;
HACK_INTERCEPTS_SEND_Websocket.prototype.ORIGINAL_send = HACK_INTERCEPTS_SEND_Websocket.prototype.send;

WebSocket2.prototype.send = function () {
HACK_INTERCEPTS_SEND_Websocket.prototype.send = function() {
if (/3:::/.test(arguments[0])) {
const message = arguments[0].replace(/3:::/, '');
let data = null;
Expand All @@ -35,13 +41,15 @@ script.
if (/Property/i.test(Q.t) && Q.v && objectSome(Q.v, (k, v) => /Electron/i.test(v))) {
Q.v['3'] = 2;
Q.v['42'] = 'PocketFans201807/24020203';
arguments[0] = `3:::${JSON.stringify(data)}`;
arguments[0] = `3:::${ JSON.stringify(data) }`;
break;
}
}
}
}

return this.$_SEND.apply(this, arguments);
}
return this.ORIGINAL_send.apply(this, arguments);
};

globalThis.HACK_INTERCEPTS_SEND_Websocket = HACK_INTERCEPTS_SEND_Websocket;
})();
2 changes: 1 addition & 1 deletion packages/qqtools/src/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ html
include components/basic/loading/loading

include components/basic/wasm/wasm
include components/basic/WebSocket2/WebSocket2
include components/basic/HACK_INTERCEPTS_SEND_Websocket/HACK_INTERCEPTS_SEND_Websocket

if htmlWebpackPlugin.options.mode === 'development'
script(src=imports.dll)
3 changes: 2 additions & 1 deletion scripts/fixTypesError.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const nodeModules = path.join(cwd, 'node_modules');
async function replaceWebsocket(fp) {
const filePath = path.join(nodeModules, fp);
const file = await fsP.readFile(filePath, { encoding: 'utf8' });
const newFile = file.replace(/window\.WebSocket/g, 'window.WebSocket2||window.WebSocket');
const newFile = file.replace(
/window\.WebSocket/g, 'window.HACK_INTERCEPTS_SEND_Websocket||window.WebSocket');

await fsP.writeFile(filePath, newFile, { encoding: 'utf8' });
}
Expand Down

0 comments on commit f007190

Please sign in to comment.