chrome 插件通信包
Summary ’🎉
这个是一个用于chrome 插件内的通信包, 封装了三个简单的类,用于插件内以及插件与页面的通信
Install 🔌
cnpm i --save chrome-pipe-plugin
Usage ✨
// page { const p = }</script> // content.js const c = ELE: 'pip' c // 初始化c // 向background注册当前tab // 添加页面监听c // 添加background监听c // background.js const bg = bg // 添加监听bg
具体例子可以查看 /example
Skill 📎
自定义配置通信元素或响应事件
配置详情参看 Config
const c = ELE: 'pip' // 通信元素id DATA_SAVE_KEY: 'PIP_MSG' // 页面通信时,数据sessionStorage缓存key
链式调用
const c = c
Config 📚
-
Default
/*** 默认配置*/const DEFAULT_CONF =ELE: 'page-pip' // content 通信元素IDPG_SEND_KEY: 'PG_TO_CONTENT' // 页面发送事件名CONTENT_SEND_KEY: 'CONTENT_TO_PG' // content发送事件名,既页面接受事件DATA_SAVE_KEY: 'CHROME_PLUGIN_PIP' // sessionStorage 数据缓存字段 -
PGClient
const defaultOptions =ELE: CONFIGELESEND_KEY: CONFIGPG_SEND_KEYLISTENER_KEY: CONFIGCONTENT_SEND_KEYDATA_SAVE_KEY: CONFIGDATA_SAVE_KEY -
CTClient
const defaultOptions =ELE: CONFIGELESEND_TO_PG_KEY: CONFIGCONTENT_SEND_KEYLISTENER_PG_KEY: CONFIGPG_SEND_KEYDATA_SAVE_KEY: CONFIGDATA_SAVE_KEY
API 📝
-
PGClient
-
init 初始化
() => PGClient
主要用来获取通道元素, 清空监听函数, 元素绑定监听事件
-
updateEle 更新绑定元素
(ele_id) => PGClient
-
bindListener 元素绑定监听事件
() => PGClient
-
send 向content发送消息
(type: string, data: Object) => PGClient
-
addListener 添加消息监听回调
(fn: (data: Object) => {}) => PGClient
-
removeListener 移除消息监听
(fn : (data: Object) => {}) => PGClient
-
clearListener 清空监听 () => PGClient
该函数也将解绑元素通信监听事件
-
-
CTClient
-
init 初始化
() => CTClient
-
sendObjToPg 向页面发送对象类型数据
( type: string, data: Object ) => CTClient
-
sendStrToPg 向页面发送字符类型数据
( type: string, data: string ) => CTClient
这个函数为基础的发送函数, sendObjToPg 只是 sendStrToPg 的包装函数, 数据格式都必须满足json要求
-
listenePgMsg 通过页面通信元素绑定通信监听
() => CTClient
-
addPGListener 添加页面消息回调
(fn: (data: Object) => {}) => CTClient
-
registerPort 项background注册通信 port
() => {}
监听background时,需要保证该函数先与 background 发送消息前执行
-
sendToBgShortMsg 使用短链向background发送消息
(data: Object) => CTClient
-
sendToBgLongMsg 使用长链向background发送消息
(data: Object) => CTClient
-
addBgListener 添加background消息回调
-
-
BGClient
- init 初始化
() => BGClient
- bindListener 绑定监听
() => BGClient
- addListener 添加消息回调
(data: Object) => BGClient
- send 向content发送消息
( type: string, data: string ) => BGClient
- init 初始化