mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-27 13:29:34 +07:00
* Add tabCapture sample * Add README * Update receiver.html * Fix variable loss caused by idle * Use windows.create * Remove unnecessary logs * Update README * Update api-samples/tabCapture/README.md Co-authored-by: Oliver Dunk <oliver@oliverdunk.com> * Update api-samples/tabCapture/README.md Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com> * Update api-samples/tabCapture/README.md Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com> * Update api-samples/tabCapture/README.md Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com> * Rename background.js * Remove unnecessary async function * Move the script to the body * Update api-samples/tabCapture/README.md Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com> * Update README.md * Update api-samples/tabCapture/manifest.json Co-authored-by: Oliver Dunk <oliver@oliverdunk.com> --------- Co-authored-by: Oliver Dunk <oliver@oliverdunk.com> Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>
37 lines
981 B
JavaScript
37 lines
981 B
JavaScript
async function closePrevReceiverTab() {
|
|
const tabs = await chrome.tabs.query({
|
|
url: chrome.runtime.getURL('receiver.html')
|
|
});
|
|
|
|
await Promise.all(tabs.map((tab) => chrome.tabs.remove(tab.id)));
|
|
}
|
|
|
|
chrome.action.onClicked.addListener(async (tab) => {
|
|
const currentTabId = tab.id;
|
|
|
|
await closePrevReceiverTab();
|
|
|
|
// Open a new tab with the receiver.html page
|
|
const { tabs } = await chrome.windows.create({
|
|
url: chrome.runtime.getURL('receiver.html')
|
|
});
|
|
|
|
const receiverTabId = tabs[0].id;
|
|
|
|
// Wait for the receiver tab to load
|
|
await new Promise((resolve) => {
|
|
chrome.tabs.onUpdated.addListener(function listener(tabId, info) {
|
|
if (tabId === receiverTabId && info.status === 'complete') {
|
|
chrome.tabs.onUpdated.removeListener(listener);
|
|
resolve();
|
|
}
|
|
});
|
|
});
|
|
|
|
// Send a message to the receiver tab
|
|
chrome.tabs.sendMessage(receiverTabId, {
|
|
targetTabId: currentTabId,
|
|
consumerTabId: receiverTabId
|
|
});
|
|
});
|