Add ability to select multiple tabs

pull/57/merge
G1org1owo 2025-04-27 23:40:50 +02:00 committed by Neo
parent 0f83292e86
commit 3d243252cd
1 changed files with 25 additions and 12 deletions

View File

@ -73,32 +73,45 @@ function openOptionsPage() {
browser.runtime.openOptionsPage(); browser.runtime.openOptionsPage();
} }
async function getActiveTabId(): Promise<number> { async function getSelectedTabIds(): Promise<number[]> {
const activeTabs = await browser.tabs.query({ const selectedTabs = await browser.tabs.query({
active: true, highlighted: true,
currentWindow: true, currentWindow: true,
}); });
if (activeTabs.length > 0 && activeTabs[0].id) return activeTabs[0].id; if (selectedTabs.length > 0 && selectedTabs[0].id) {
return selectedTabs
.filter(tab => tab.id)
.map(tab => tab.id as number)
}
throw new Error("No active tab."); throw new Error("No active tab.");
} }
async function grabPost() { async function grabPost() {
// Get active tab // Get active tab
const activeTabId = await getActiveTabId(); const selectedTabIds = await getSelectedTabIds();
// Send 'grab_post' to the content script on the active tab let scrapeResults: ScrapeResults = new ScrapeResults();
const x = await browser.tabs.sendMessage(activeTabId, new BrowserCommand("grab_post")); for(const selectedTabId of selectedTabIds) {
// Send 'grab_post' to the content script on the active tabs
const x = await browser.tabs.sendMessage(selectedTabId, new BrowserCommand("grab_post"));
// We need to create a new ScrapeResults object and fill it with our data, because the get_posts() // We need to create a new ScrapeResults object and fill it with our data, because the get_posts()
// method gets 'lost' when sent from the contentscript to the popup (it gets JSON.stringified and any prototype defines are lost there) // method gets 'lost' when sent from the contentscript to the popup (it gets JSON.stringified and any prototype defines are lost there)
const res: ScrapeResults = Object.assign(new ScrapeResults(), x); const results: ScrapeResults = Object.assign(new ScrapeResults(), x);
console.dir(res); for(const result of results.results) {
const res = scrapeResults.results.find((r) => r.engine === result.engine);
if(res) res.posts.push(...result.posts);
else scrapeResults.results.push(result);
}
}
console.dir(scrapeResults);
// Clear current posts array // Clear current posts array
pop.posts.splice(0); pop.posts.splice(0);
for (const result of res.results) { for (const result of scrapeResults.results) {
for (const i in result.posts) { for (const i in result.posts) {
const vm = new ScrapedPostDetails(result.posts[i]); const vm = new ScrapedPostDetails(result.posts[i]);
const name = result.posts[i].name ?? `Post ${parseInt(i) + 1}`; // parseInt() is required! const name = result.posts[i].name ?? `Post ${parseInt(i) + 1}`; // parseInt() is required!