feat: add more functionality

This commit is contained in:
2026-09-17 04:35:18 +08:00
parent 3fe95e925e
commit e3d55e4b7e
13 changed files with 189 additions and 184 deletions
+33 -54
View File
@@ -2,7 +2,7 @@
// ==UserScript==
// @name WhatsApp Web Bridge
// @namespace https://rul.sh/
// @version 0.0.6
// @version 0.0.7
// @description WhatsApp Web WebSocket bridge
// @match https://web.whatsapp.com/*
// @updateURL https://git.rul.sh/khairul169/whatsapp-bridge/raw/branch/main/dist/whatsapp-bridge.user.js
@@ -201,7 +201,7 @@
from: serializeId(message.from),
to: serializeId(message.to),
author: serializeId(message.author),
fromMe: message.id?.fromMe ?? message.fromMe ?? false,
fromMe: message.id?.fromMe || message.fromMe || false,
body: message.body ?? null,
caption: message.caption ?? null,
type: message.type ?? null,
@@ -230,8 +230,22 @@
}
// src/userscript/index.ts
var WS_URL = "wss://wa-bridge.rul.sh/ws?bridge=1";
var socket;
var consts = {
DEVICE_ID: String(Date.now()),
DEVICE_NAME: ""
};
function init() {
try {
const _consts = localStorage.getItem("wa-bridge-consts");
if (!_consts) {
localStorage.setItem("wa-bridge-consts", JSON.stringify(consts));
} else {
consts = JSON.parse(_consts);
}
} catch (_) {
}
}
function send(message) {
if (socket?.readyState !== WebSocket.OPEN) {
throw new Error("WebSocket is not connected");
@@ -262,7 +276,9 @@
case "status":
return {
authenticated: WPP.conn.isAuthenticated(),
ready: WPP.isReady
ready: WPP.isReady,
deviceId: consts.DEVICE_ID,
deviceName: consts.DEVICE_NAME
};
case "listChats": {
const chats = await WPP.chat.list({
@@ -279,7 +295,7 @@
}
case "listGroups": {
const groups = await WPP.group.getAllGroups();
return groups.filter(Boolean).map(serializeGroupMetadata);
return groups.filter((i) => i.groupMetadata != null).map((i) => serializeGroupMetadata(i.groupMetadata));
}
case "getMessages": {
const messages = await WPP.chat.getMessages(request.params.chatId, {
@@ -289,12 +305,6 @@
});
return messages.filter(Boolean).map(serializeMessage);
}
case "searchMessages":
return searchMessages(
request.params.query,
request.params.chatId,
request.params.limit ?? 100
);
case "sendText": {
const result = await WPP.chat.sendTextMessage(
request.params.chatId,
@@ -311,68 +321,36 @@
case "getMedia":
const media = await WPP.chat.downloadMedia(request.params).then(WPP.util.blobToBase64);
return media;
case "exec":
const fn = new Function(request.params);
return fn();
default:
throw new Error(`Unknown method: ${request.method}`);
}
}
async function searchMessages(query, chatId, limit = 100) {
const normalized = query.toLowerCase();
let chats;
if (chatId) {
const chat = WPP.chat.get(chatId);
chats = chat ? [chat] : [];
} else {
chats = await WPP.chat.list();
}
const results = [];
for (const chat of chats) {
if (results.length >= limit) {
break;
}
const id = chat?.id?._serialized ?? chat?.id ?? chat?.wid?._serialized;
if (!id) {
continue;
}
try {
const messages = await WPP.chat.getMessages(id, {
count: 100
});
for (const message of messages) {
const body = String(message.body ?? message.caption ?? "");
if (body.toLowerCase().includes(normalized)) {
results.push({
chatId: id,
message: serialize(message)
});
if (results.length >= limit) {
break;
}
}
}
} catch (err) {
console.warn("Failed searching chat", id, err);
}
}
return results;
}
function connect() {
if (socket && (socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING)) {
return;
}
socket = new WebSocket(WS_URL);
socket = new WebSocket("wss://wa-bridge.rul.sh/ws");
socket.onopen = () => {
console.log("[WA Bridge] connected");
console.log("[WA Bridge] connected", "wss://wa-bridge.rul.sh/ws");
send({
type: "event",
event: "ready",
data: {
authenticated: WPP.conn.isAuthenticated()
authenticated: WPP.conn.isAuthenticated(),
deviceId: consts.DEVICE_ID,
deviceName: consts.DEVICE_NAME
}
});
};
socket.onmessage = async (event) => {
try {
const request = JSON.parse(event.data);
if (request.type === "ping") {
send({ type: "pong" });
}
if (request.type !== "request") {
return;
}
@@ -415,6 +393,7 @@
});
});
}
init();
start();
})();
//# sourceMappingURL=whatsapp-bridge.user.js.map
+2 -2
View File
File diff suppressed because one or more lines are too long