-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@lexical/devtools): Correct handling of the restricted tabs
- Loading branch information
Showing
10 changed files
with
153 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/lexical-devtools/src/entrypoints/background/ActionIconWatchdog.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
import type {Tabs} from 'wxt/browser'; | ||
import type {StoreApi} from 'zustand'; | ||
|
||
import {IS_FIREFOX} from 'shared/environment'; | ||
|
||
import {ExtensionState} from '../../store'; | ||
|
||
export default class ActionIconWatchdog { | ||
private constructor( | ||
private readonly extensionStore: StoreApi<ExtensionState>, | ||
) {} | ||
|
||
static async start(store: StoreApi<ExtensionState>) { | ||
return new ActionIconWatchdog(store).init(); | ||
} | ||
|
||
async init() { | ||
const tabs = await browser.tabs.query({}); | ||
await Promise.all( | ||
tabs.map(this.checkAndHandleRestrictedPageIfSo.bind(this)), | ||
); | ||
|
||
browser.tabs.onCreated.addListener((tab) => { | ||
this.checkAndHandleRestrictedPageIfSo(tab); | ||
}); | ||
|
||
// Listen to URL changes on the active tab and update the DevTools icon. | ||
browser.tabs.onUpdated.addListener(this.handleTabsUpdatedEvent.bind(this)); | ||
} | ||
|
||
private async setIcon( | ||
lexicalBuildType: 'restricted' | 'enabled', | ||
tabId: number, | ||
) { | ||
const action = IS_FIREFOX ? browser.browserAction : browser.action; | ||
|
||
await action.setIcon({ | ||
path: { | ||
'128': browser.runtime.getURL( | ||
lexicalBuildType === 'enabled' | ||
? '/icon/128.png' | ||
: '/icon/128-restricted.png', | ||
), | ||
'16': browser.runtime.getURL( | ||
lexicalBuildType === 'enabled' | ||
? '/icon/16.png' | ||
: '/icon/16-restricted.png', | ||
), | ||
'32': browser.runtime.getURL( | ||
lexicalBuildType === 'enabled' | ||
? '/icon/32.png' | ||
: '/icon/32-restricted.png', | ||
), | ||
'48': browser.runtime.getURL( | ||
lexicalBuildType === 'enabled' | ||
? '/icon/48.png' | ||
: '/icon/48-restricted.png', | ||
), | ||
}, | ||
tabId: tabId, | ||
}); | ||
|
||
if (lexicalBuildType === 'restricted') { | ||
this.extensionStore.getState().markTabAsRestricted(tabId); | ||
} | ||
} | ||
|
||
private handleTabsUpdatedEvent( | ||
tabId: number, | ||
_changeInfo: unknown, | ||
tab: Tabs.Tab, | ||
): void { | ||
this.checkAndHandleRestrictedPageIfSo(tab); | ||
} | ||
|
||
private isRestrictedBrowserPage(url: string | undefined) { | ||
return ( | ||
!url || ['chrome:', 'about:', 'file:'].includes(new URL(url).protocol) | ||
); | ||
} | ||
|
||
private async checkAndHandleRestrictedPageIfSo(tab: Tabs.Tab) { | ||
if (tab.id == null) { | ||
return; | ||
} | ||
|
||
if (tab.id == null || this.isRestrictedBrowserPage(tab.url)) { | ||
return this.setIcon('restricted', tab.id); | ||
} | ||
|
||
return this.setIcon('enabled', tab.id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters