Add ability to select multiple tabs
parent
c08e6473e9
commit
a06d4ee1a1
|
|
@ -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) {
|
||||||
// We need to create a new ScrapeResults object and fill it with our data, because the get_posts()
|
// Send 'grab_post' to the content script on the active tabs
|
||||||
// method gets 'lost' when sent from the contentscript to the popup (it gets JSON.stringified and any prototype defines are lost there)
|
const x = await browser.tabs.sendMessage(selectedTabId, new BrowserCommand("grab_post"));
|
||||||
const res: ScrapeResults = Object.assign(new ScrapeResults(), x);
|
// 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)
|
||||||
|
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!
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue