简体   繁体   English

在内容脚本中从background.js调用事件侦听器

[英]Call event listener from background.js in content script

I have an event listener in background.js as follows: 我在background.js有一个事件侦听器,如下所示:

chrome.webRequest.onHeadersReceived.addListener(function(details){
 .....
})

How can I call this event listener from content script? 如何从内容脚本调用此事件侦听器?

You cannot call or basically say trigger any webrequest api event listener from content script. 您不能从内容脚本调用或基本上不能说触发任何webrequest api事件侦听器。 Basically the definition of onHeadersReceived is as below on the offical doumentation. 基本上, onHeadersReceived的定义如下文所述。

Fires each time that an HTTP(S) response header is received. 每次收到HTTP(S)响应标头时触发。 Due to redirects and authentication requests this can happen multiple times per request. 由于重定向和身份验证请求,每个请求可能会多次发生。 This event is intended to allow extensions to add, modify, and delete response headers, such as incoming Set-Cookie headers. 此事件旨在允许扩展添加,修改和删除响应头,例如传入的Set-Cookie头。 The caching directives are processed before this event is triggered, so modifying headers such as Cache-Control has no influence on the browser's cache. 在触发此事件之前会处理缓存指令,因此,修改标头(如Cache-Control)不会影响浏览器的缓存。 It also allows you to redirect the request. 它还允许您重定向请求。

Please checkout the official documentation of the event you are listening to for better clarity and options. 请查看您正在听的活动的官方文档 ,以获取更好的清晰度和选项。

But you can always add custom eventListners on background.js scripts which you are trigger from content scripts using the messaging feature. 但是,您始终可以在background.js脚本上添加自定义eventListners,这些脚本是使用消息传递功能从内容脚本触发的。 A simple example is depicted in the documentation . 文档中描述了一个简单的示例。 Where you send a custom message from content script and receive the message on background script. 从内容脚本发送自定义消息并在后台脚本接收消息的位置。 This way you can communicate with background script. 这样,您可以与后台脚本进行通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自background.js的内容脚本中的Chrome调用功能 - Chrome call function in content scripts from background.js 从内容脚本调用一个注入按钮到background.js - Calling an injected button from the content script to the background.js 将消息从background.js发送到内容脚本 - Sending message from background.js to content script 从background.js向内容脚本发送消息 - Sending a message from background.js to a content script 如何在 Chrome 扩展程序的 background.js 中调试事件侦听器 - How to debug event listener in background.js in Chrome extension Background.js找不到使用内容脚本注入的内容 - Background.js Doesn't Find Content Injected with Content Script 如何将数组从 background.js 传递给 inject.js(内容)脚本(Chrome 扩展程序) - How to pass an array from background.js to inject.js (content) script (Chrome Extension) 将内容脚本中的大型JSON / JS对象压缩为background.js-Chrome扩展 - Compress large JSON/JS object from content script to background.js - chrome extension 从内容脚本将File对象传递到background.js或传递createObjectURL(并在刷新后保持活动状态) - Pass File object to background.js from content script or pass createObjectURL (and keep alive after refresh) 准备好Chrome扩展功能从background.js到内容脚本的结果 - Chrome extension function result from background.js to content script when ready
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM