Files
chrome-extensions-samples/api-samples/tabCapture/service-worker.js
Xuezhou Dai 961028b1bf Add tabCapture sample (#933)
* 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>
2023-06-01 10:51:54 +01:00

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
});
});