Skip to content

Instantly share code, notes, and snippets.

@edgardmessias
Created December 15, 2020 19:46
Show Gist options
  • Save edgardmessias/d0c7778a033c89d94847332167973f8a to your computer and use it in GitHub Desktop.
Save edgardmessias/d0c7778a033c89d94847332167973f8a to your computer and use it in GitHub Desktop.
/**
* Whatsapp injection script.
*
* You can use this script to get any module from Whatsapp Web page
*/
// Set the default webpackJson
window["webpackJsonp"] = window["webpackJsonp"] || [];
(function () {
let pendents = [];
let interval = null;
function searchModule(modules, condition) {
for (let idx in modules) {
if (typeof modules[idx] !== "object" || modules[idx] === null) {
continue;
}
let first = Object.values(modules[idx])[0];
if (typeof first !== "object" || !first.exports) {
continue;
}
for (let idx2 in modules[idx]) {
let module = modules(idx2);
if (!module) {
continue;
}
try {
let neededModule = condition(module);
if (neededModule) {
return neededModule;
}
} catch (error) {}
}
}
}
function checkPendingStores(modules) {
pendents = pendents.filter((p) => {
const condition = p.condition;
const resolve = p.resolve;
const module = searchModule(modules, condition);
if (module) {
resolve(module);
return false;
}
return true;
});
if (!pendents.length) {
clearInterval(interval);
interval = null;
return;
}
}
function injectParasite() {
const parasite = `parasite${Date.now()}`;
window["webpackJsonp"].push([
[parasite],
{
[parasite]: (x, y, z) => checkPendingStores(z),
},
[[parasite]],
]);
}
function loadParasite() {
injectParasite();
if (interval) {
return;
}
/**
* Check new modules after injection
*/
interval = setInterval(() => {
try {
const last = window["webpackJsonp"].length - 1;
if (!/^parasite/.test(window["webpackJsonp"][last][0][0])) {
injectParasite();
}
} catch (e) {}
}, 100);
}
function getStore(condition, timeout = 60000) {
const promise = new Promise((resolve, reject) => {
const data = {
condition,
resolve,
};
pendents.push(data);
if (timeout > 0) {
setTimeout(() => {
pendents = pendents.filter((p) => p !== data);
reject();
}, timeout);
}
});
loadParasite();
return promise;
}
// Expose getStore function
window.getStore = getStore;
})();
// Whatsapp send and onMessage log
window
.getStore((module) =>
module.STATE && module.STREAM && module.default ? module.default : null
)
.then((state) => {
const log_data = (name, e) => {
const d = e.binaryOpts ? e.binaryOpts?.debugObj : e.data;
if (typeof d === "undefined") {
return;
}
console.log(`${name} (${e.tag}) `, JSON.stringify(d));
};
const originalBasicSend = state._basicSend;
state._basicSend = (e, t) => {
const ret = originalBasicSend.call(state, e, t);
log_data("_basicSend", e);
return ret;
};
const originalSend = state._send;
state._send = (e) => {
const ret = originalSend.call(state, e);
log_data("_send", e);
return ret;
};
const originalOnMessage = state.onMessage;
state.onMessage = (e) => {
log_data("onMessage", e);
const ret = originalOnMessage.call(state, e);
return ret;
};
});
/*
// Whatsapp return messages log
window
.getStore((module) =>
module.default && module.default.msgFindQuery ? module.default : null
)
.then((m) => {
const msgFindQuery = m.msgFindQuery;
m.msgFindQuery = async (...args) => {
const ret = await msgFindQuery.apply(m, args);
console.log(`Messages: `, JSON.stringify(ret));
return ret;
};
});
*/
@yusufusta
Copy link

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment