Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 51 additions & 24 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,32 @@
});
}

/**
* Finds a tab by its host.
* @param {puppeteer.Browser} browser

Check failure on line 433 in src/Client.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `·`

Check failure on line 433 in src/Client.js

View workflow job for this annotation

GitHub Actions / Tests

Delete `·`
* @param {string} targetHost

Check failure on line 434 in src/Client.js

View workflow job for this annotation

GitHub Actions / ESLint

Delete `·`

Check failure on line 434 in src/Client.js

View workflow job for this annotation

GitHub Actions / Tests

Delete `·`
* @returns {Promise<puppeteer.Page | null>}
*/
async findTabByHost(browser, targetHost) {
const pages = await browser.pages(); // List all existing tabs

const foundPage = pages.find(page => {

Check failure on line 440 in src/Client.js

View workflow job for this annotation

GitHub Actions / ESLint

Replace `page` with `(page)`

Check failure on line 440 in src/Client.js

View workflow job for this annotation

GitHub Actions / Tests

Replace `page` with `(page)`
try {
const url = new URL(page.url());
return url.hostname === targetHost;
} catch (e) {

Check failure on line 444 in src/Client.js

View workflow job for this annotation

GitHub Actions / ESLint

'e' is defined but never used. Allowed unused caught errors must match /^ignoredError/u

Check failure on line 444 in src/Client.js

View workflow job for this annotation

GitHub Actions / Tests

'e' is defined but never used. Allowed unused caught errors must match /^ignoredError/u
return null; // Skip if URL is invalid (e.g., 'about:blank')
}
});

if (foundPage) {
console.log(`Tab found with host: ${targetHost}`);
return foundPage;
}

console.log(`No tab found for host: ${targetHost}`);
return null;
}
/**
* Sets up events and requirements, kicks off authentication request
*/
Expand All @@ -452,7 +478,8 @@
(puppeteerOpts.browserWSEndpoint || puppeteerOpts.browserURL)
) {
browser = await puppeteer.connect(puppeteerOpts);
page = await browser.newPage();
page = await this.findTabByHost(browser, 'web.whatsapp.com');
if (!page) page = await browser.newPage();
} else {
const browserArgs = [...(puppeteerOpts.args || [])];
if (
Expand Down Expand Up @@ -1400,8 +1427,8 @@
content instanceof Buttons,
content instanceof List,
Array.isArray(content) &&
content.length > 0 &&
content[0] instanceof Contact,
content.length > 0 &&

Check failure on line 1430 in src/Client.js

View workflow job for this annotation

GitHub Actions / ESLint

Insert `····`

Check failure on line 1430 in src/Client.js

View workflow job for this annotation

GitHub Actions / Tests

Insert `····`
content[0] instanceof Contact,

Check failure on line 1431 in src/Client.js

View workflow job for this annotation

GitHub Actions / ESLint

Insert `····`

Check failure on line 1431 in src/Client.js

View workflow job for this annotation

GitHub Actions / Tests

Insert `····`
].includes(true)
) {
console.warn(
Expand All @@ -1422,8 +1449,8 @@
content instanceof Buttons,
content instanceof List,
Array.isArray(content) &&
content.length > 0 &&
content[0] instanceof Contact,
content.length > 0 &&

Check failure on line 1452 in src/Client.js

View workflow job for this annotation

GitHub Actions / ESLint

Insert `····`

Check failure on line 1452 in src/Client.js

View workflow job for this annotation

GitHub Actions / Tests

Insert `····`
content[0] instanceof Contact,

Check failure on line 1453 in src/Client.js

View workflow job for this annotation

GitHub Actions / ESLint

Insert `····`

Check failure on line 1453 in src/Client.js

View workflow job for this annotation

GitHub Actions / Tests

Insert `····`
].includes(true)
) {
console.warn(
Expand Down Expand Up @@ -1805,10 +1832,10 @@
return !pinnedMsgs.length
? []
: await Promise.all(
pinnedMsgs.map((msg) =>
window.WWebJS.getMessageModel(msg),
),
);
pinnedMsgs.map((msg) =>

Check failure on line 1835 in src/Client.js

View workflow job for this annotation

GitHub Actions / ESLint

Insert `··`

Check failure on line 1835 in src/Client.js

View workflow job for this annotation

GitHub Actions / Tests

Insert `··`
window.WWebJS.getMessageModel(msg),

Check failure on line 1836 in src/Client.js

View workflow job for this annotation

GitHub Actions / ESLint

Insert `··`

Check failure on line 1836 in src/Client.js

View workflow job for this annotation

GitHub Actions / Tests

Insert `··`
),
);
}, chatId);

return pinnedMsgs.map((msg) => new Message(this, msg));
Expand Down Expand Up @@ -2110,9 +2137,9 @@
.Chat.find(chatId));
action === 'MUTE'
? await chat.mute.mute({
expiration: unmuteDateTs,
sendDevice: true,
})
expiration: unmuteDateTs,
sendDevice: true,
})
: await chat.mute.unmute({ sendDevice: true });
return {
isMuted: chat.mute.expiration !== 0,
Expand Down Expand Up @@ -2396,9 +2423,9 @@
window
.require('WAWebCollections')
.Chat.get(participant.wid) ||
(await window
.require('WAWebCollections')
.Chat.find(participant.wid)),
(await window
.require('WAWebCollections')
.Chat.find(participant.wid)),
createGroupResult.wid._serialized,
createGroupResult.subject,
participant.invite_code,
Expand Down Expand Up @@ -2663,11 +2690,11 @@

countryCodes =
countryCodes.length === 1 &&
countryCodes[0] === currentRegion
countryCodes[0] === currentRegion
? countryCodes
: countryCodes.filter((code) =>
Object.keys(countryCodesIso).includes(code),
);
Object.keys(countryCodesIso).includes(code),
);

const viewTypeMapping = {
0: 'RECOMMENDED',
Expand Down Expand Up @@ -2706,12 +2733,12 @@

return channels
? await Promise.all(
channels.map((channel) =>
window.WWebJS.getChatModel(channel, {
isChannel: true,
}),
),
)
channels.map((channel) =>
window.WWebJS.getChatModel(channel, {
isChannel: true,
}),
),
)
: [];
},
searchOptions,
Expand Down
Loading