extension.onRequest
警告: 因为该方法自 Chrome 33 起被弃用,该 API 在 Firefox 中并未被实现。请改用 runtime.onMessage。
当从扩展进程或内容脚本发送请求时触发。
语法
js
chrome.extension.onRequest.addListener(function(
request, // 可选的任意类型
sender, // runtime.MessageSender
() => {/* … */} // 函数
) {/* … */})
chrome.extension.onRequest.removeListener(listener)
chrome.extension.onRequest.hasListener(listener)
事件有三个函数:
addListener(listener)
-
添加一个该事件的监听器。
removeListener(listener)
-
停止监听该事件。
listener
参数是要移除的监听器。 hasListener(listener)
-
检查
listener
是否注册到该事件上。若该监听器正在监听该事件则返回true
,否则返回false
。
addListener 语法
参数
listener
-
当此事件发生时调用的函数。该函数传递以下参数:
request
-
any
。由调用脚本发送的请求。 sender
sendResponse
-
function
。当你有响应时调用的函数。参数应为任何可 JSON 化的对象(如果没有响应则为 undefined)。如果在同一文档中有多个onRequest
监听器,则只有一个可以发送响应。
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | ||||||
---|---|---|---|---|---|---|---|
onRequest |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
备注:
此 API 基于 Chromium 的 chrome.extension
API。该文档衍生自 Chromium 代码中的 extension.json
。