workspace-api-for-chrome
Provides workspace management capabilities for Chrome.
How to use
Install the package:
npm add workspace-api-for-chrome
# or
yarn add workspace-api-for-chrome
Remember to declare the following permissions in manifest.json
:
"permissions": [
"tabs",
"webNavigation"
]
API
Import and initialize an instance (note that the creation of the window should be done separately and beforehand):
import Workspace from 'workspace-api-for-chrome';
// suppose a window is created,
// and its ID is stored in `windowId`
const workspace = new Workspace(windowId);
// if you want to control more advanced options
const workspace = new Workspace(windowId, { webNavEndDelay: 1000, tabUrlCacheDepth: 1 });
// also, if you want to enable logging
const workspace = new Workspace(windowId, {}, true);
The second parameter is an object with the following scheme:
type WorkspaceOptions = {
webNavEndDelay?: number;
tabUrlCacheDepth?: number;
}
-
webNavEndDelay
is the milliseconds to wait before a web navigation is considered completed. -
tabUrlCacheDepth
means how many URLs that are previously written to a tab should be recorded for duplication check.
read()
Read the workspace and return an object (compatible with write()).
const data = await workspace.read();
Return Value Type
{
activeTabIndex: number;
tabs: {
url: string;
}[];
}
readRaw()
Read the workspace and return tabs in Chrome native format.
const rawData = await workspace.readRaw();
Return Value Type
{
tabs: chrome.tabs.Tab[];
}
write()
Write the workspace and use minimal operations to make it align with the target state.
Note: if activeTabIndex
is not provided, the active tab will remain the same; if tabs
is not provided, the tabs
will not be synchronized.
await workspace.write(data);
Parameter Type: data
{
activeTabIndex?: number;
tabs?: {
url: string;
}[];
}
addEventListener()
Add callback function to handle events within the workspace.
const callbackFn = (params) => {
const { event, rawParams } = params;
console.log(event, rawParams);
};
workspace.addEventHandler(callbackFn);
Parameter Type: handlerToAdd
(params: EventHandlerParams) => void
Note: see Type Definitions for detailed type definitions.
removeEventHandler()
Remove callback function that handles events within the workspace.
workspace.removeEventHandler(callbackFn);
Parameter Type: handlerToRemove
(params: IEventHandlerParams) => void
Note: see Type Definitions for detailed type definitions.
destroy()
Remove all internal listeners. Use it when you will no longer use the workspace instance.
workspace.destroy();
Type Definitions
enum ITabEvent {
OnActivated,
OnAttached,
OnCreated,
OnDetached,
OnHighlighted,
OnMoved,
OnRemoved,
OnUpdated,
}
type IEventHandlerParams =
| {
event: ITabEvent.OnActivated;
rawParams: { activeInfo: chrome.tabs.TabActiveInfo };
}
| {
event: ITabEvent.OnAttached;
rawParams: { tabId: number; attachInfo: chrome.tabs.TabAttachInfo };
}
| {
event: ITabEvent.OnCreated;
rawParams: { tab: chrome.tabs.Tab };
}
| {
event: ITabEvent.OnDetached;
rawParams: { tabId: number; detachInfo: chrome.tabs.TabDetachInfo };
}
| {
event: ITabEvent.OnHighlighted;
rawParams: { highlightInfo: chrome.tabs.TabHighlightInfo };
}
| {
event: ITabEvent.OnMoved;
rawParams: { tabId: number; moveInfo: chrome.tabs.TabMoveInfo };
}
| {
event: ITabEvent.OnRemoved;
rawParams: { tabId: number; removeInfo: chrome.tabs.TabRemoveInfo };
}
| {
event: ITabEvent.OnUpdated;
rawParams: { tabId: number; changeInfo: chrome.tabs.TabChangeInfo; tab: chrome.tabs.Tab };
};