Files
UT-Registration-Plus/src/views/lib/openNewTabFromContentScript.ts
2024-03-06 15:11:30 -06:00

26 lines
831 B
TypeScript

import { createMessenger } from 'chrome-extension-toolkit';
type MyMessages = {
openNewTab: (data: { url: string }) => void;
};
const messenger = createMessenger<MyMessages>('background');
/**
* Content scripts and background scripts are isolated environments.
* Content scripts are where our code interacting with the webpage lives,
* whereas the background script is where we can open a tab from.
* This function allows us to open a new tab from the content script by communicating
* with the background script.
*/
export async function openTabFromContentScript(url: string) {
messenger
.openNewTab({ url })
.then(() => {
console.log('New tab opened with URL:', url);
})
.catch(error => {
console.error('Error opening new tab:', error);
});
}