Skip to content

Commit e34a442

Browse files
author
qiubowen
committed
fix: enable running in incognito mode for recorder
1 parent 445c28c commit e34a442

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

examples/recorder-crx/src/background.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ async function changeAction(tabId: number, mode?: CrxMode | 'detached') {
7373
// https://bugs.chromium.org/p/chromium/issues/detail?id=1450904
7474
chrome.tabs.onUpdated.addListener(tabId => changeAction(tabId));
7575

76-
async function getCrxApp() {
76+
async function getCrxApp(isIncognito: boolean) {
7777
if (!crxAppPromise) {
7878
await settingsInitializing;
7979

80-
crxAppPromise = crx.start().then(crxApp => {
80+
crxAppPromise = crx.start( { incognito: isIncognito } ).then(crxApp => {
8181
crxApp.recorder.addListener('hide', async () => {
8282
await crxApp.detachAll();
8383
});
@@ -104,6 +104,13 @@ async function getCrxApp() {
104104
async function attach(tab: chrome.tabs.Tab, mode?: Mode) {
105105
if (!tab?.id || (attachedTabIds.has(tab.id) && !mode))
106106
return;
107+
// if the tab is incognito, chek if can be started in incognito mode.
108+
if (tab.incognito) {
109+
if (!await chrome.extension.isAllowedIncognitoAccess() || !settings.playInIncognito) {
110+
console.error('Not authorized to launch in Incognito mode.');
111+
return;
112+
}
113+
}
107114
const tabId = tab.id;
108115

109116
const sidepanel = !isUnderTest() && settings.sidepanel;
@@ -114,8 +121,7 @@ async function attach(tab: chrome.tabs.Tab, mode?: Mode) {
114121

115122
// ensure one attachment at a time
116123
chrome.action.disable();
117-
118-
const crxApp = await getCrxApp();
124+
const crxApp = await getCrxApp(tab.incognito);
119125

120126
try {
121127
if (crxApp.recorder.isHidden()) {

src/server/crx.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,17 @@ export class Crx extends SdkObject {
138138
private async _startIncognitoCrxApplication(browser: CRBrowser, transport: CrxTransport, options?: channels.BrowserNewContextParams) {
139139
const windows = await chrome.windows.getAll().catch(() => {}) ?? [];
140140
const windowId = windows.find(window => window.incognito)?.id;
141-
const incognitoTabIdPromise = new Promise<number>(resolve => {
142-
const tabCreated = (tab: chrome.tabs.Tab) => {
143-
if (!tab.incognito || !tab.id)
144-
return;
145-
chrome.tabs.onCreated.removeListener(tabCreated);
146-
resolve(tab.id);
147-
};
148-
chrome.tabs.onCreated.addListener(tabCreated);
149-
});
150141
if (!windowId)
151142
chrome.windows.create({ incognito: true, url: 'about:blank' });
152143
else
153-
chrome.tabs.create({ url: 'about:blank', windowId });
154-
155-
const incognitoTabId = await incognitoTabIdPromise;
144+
// do not create but find exists one
145+
chrome.tabs.query({windowId: windowId}, (tabIds) => {
146+
if (!tabIds) {
147+
chrome.tabs.create({ url: 'about:blank', windowId });
148+
}
149+
})
150+
151+
const incognitoTabId = (await chrome.tabs.query({windowId: windowId, active: true}))[0].id!;
156152
let context!: CRBrowserContext;
157153
await transport.attach(incognitoTabId, async ({ browserContextId }) => {
158154
// ensure we create and initialize the new context before the Target.attachedToTarget event is emitted

0 commit comments

Comments
 (0)