runtime 运行时
用于检测和响应运行时事件,例如应用程序安装、启动、关闭、卸载等 返回应用的运行时信息,例如应用的ID、版本、浏览器和操作系统的信息
属性
id 应用ID
chrome.runtime.id
lastError 错误信息
chrome.runtime.lastError
方法 - 获取应用信息
getManifest() 获取manifest.json
chrome.runtime.getManifest()
getURL() 获取资源路径
chrome.runtime.getURL(path: string)
getPlatformInfo() 获取平台信息
chrome.runtime.getPlatformInfo()
getPackageDirectoryEntry() 获取包目录
chrome.runtime.getPackageDirectoryEntry()
getBackgroundPage() 获取后台页面
返回后台页面的window对象
chrome.runtime.getBackgroundPage()
getContexts() 获取应用所有页面信息
type ContextType = "TAB" | "POPUP" | "BACKGROUND" | "OFFSCREEN_DOCUMENT" | "SIDE_PANEL";
type ContextFilter = {
contextIds?: string[];
contextTypes?: ContextType[];
documentIds?: string[];
documentOrigins?: string[];
documentUrls?: string[];
frameIds?: number[];
incognito?: boolean;
tabIds?: number[];
windowIds?: number[];
}
chrome.runtime.getContexts(
filter: ContextFilter,
callback?: function,
)
方法 - 操作应用
reload() 重新加载应用
chrome.runtime.reload()
restart() 重启应用
chrome.runtime.restart()
restartAfterDelay() 延迟重启应用
chrome.runtime.restartAfterDelay(
seconds: number,
callback?: function,
)
openOptionsPage() 打开设置页面
chrome.runtime.openOptionsPage()
requestUpdateCheck() 检查更新
chrome.runtime.requestUpdateCheck(
callback?: function,
)
setUninstallURL() 设置卸载URL
chrome.runtime.setUninstallURL(
url: string,
callback?: function,
)
方法 - 消息传递
sendMessage() 插件内发送消息
chrome.runtime.sendMessage(
extensionId?: string,
message: any,
options?: MessageOptions,
callback?: (response: any) => void,
)
sendNativeMessage() 插件外发送消息
chrome.runtime.sendNativeMessage(
application: string,
message: any,
callback?: (response: any) => void,
)
connect() 插件内长连接
chrome.runtime.connect(
extensionId?: string,
connectInfo?: ConnectInfo,
)
connectNative() 插件外长连接
chrome.runtime.connectNative(
application: string,
)
事件 - 应用生命周期
onStartup 应用启动事件
// background.js
chrome.runtime.onStartup.addListener(() => {
console.log('🍄 background: >>>>>>>>>>>>>>>>>> 应用启动事件', Date.now());
});
onInstalled 应用安装事件
type OnInstalledReason = 'install' | 'update' | 'chrome_update' | 'shared_module_update';
type OnInstalledDetails = {
reason: OnInstalledReason;
id?: string;
}
chrome.runtime.onInstalled.addListener(((details: OnInstalledDetails)) => {
console.log('🍄 background: >>>>>>>>>>>>>>>>>> 应用安装事件', Date.now());
});
onSuspend 应用挂起事件(卸载之前)
// background.js
chrome.runtime.onSuspend.addListener(() => {
console.log('🍄 background: >>>>>>>>>>>>>>>>>> 应用挂起事件', Date.now());
});
onSuspendCanceled 应用取消挂起事件
// background.js
chrome.runtime.onSuspendCanceled.addListener(() => {
console.log('🍄 background: >>>>>>>>>>>>>>>>>> 应用取消挂起事件', Date.now());
});
onRestartRequired 应用重启事件
// background.js
chrome.runtime.onRestartRequired.addListener(() => {
console.log('🍄 background: >>>>>>>>>>>>>>>>>> 应用重启事件', Date.now());
});
事件 - 消息监听
onMessage 应用内消息事件
type MessageSender = {
id?: string;
url?: string;
tlsChannelId?: string;
tab?: chrome.Tabs.Tab;
documentId?: string;
frameId?: number;
nativeApplication?: string;
origin?: string;
}
chrome.runtime.onMessage.addListener(
message: any,
sender: MessageSender,
sendResponse: function
) => boolean | undefined;
onMessageExternal 来自网页/其他插件的消息事件
// background.js
chrome.runtime.onMessageExternal.addListener(() => {
console.log('🍄 background: >>>>>>>>>>>>>>>>>> 应用外部消息事件', Date.now());
});
onConnect 应用内长连接事件
type Port = {
name: string;
onDisconnect: {
addListener: (callback: (port: Port) => void) => void;
};
}
chrome.runtime.onConnect.addListener((port: Port) => {
console.log('🍄 background: >>>>>>>>>>>>>>>>>> 应用连接事件', Date.now());
});
onConnectExternal 来自网页/其他插件的连接事件
// background.js
chrome.runtime.onConnectExternal.addListener(() => {
console.log('🍄 background: >>>>>>>>>>>>>>>>>> 应用外部连接事件', Date.now());
});