在Chrome扩展内容脚本中共享内存中的对象? [英] Share in-memory objects in Chrome extension content scripts?

查看:131
本文介绍了在Chrome扩展内容脚本中共享内存中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript和Chrome开发都很陌生,并且正在尝试创建一个在某些网页中注入内容/ CSS的扩展。很简单,但要做到这一点,它需要查看本地存储中的大量数据。从我目前阅读的内容来看,正确的做法是:


  1. 读取所需的数据( JSON序列化)每次访问页面时直接从内容脚本存储,或
  2. 维护扩展后台页面中的状态并传输所需数据() JSON序列化)使用消息传递到内容脚本环境。

然而,其中任何一种方法都是非常低效的由于大量的数据被不必要的序列化和反序列化每个页面加载。

所以,我想知道:


  • 在Chrome中,是否有可能维护共享内存缓存,以便所有标签中注入的内容脚本都可以访问?


  • 如果不是,那么在后台页面侦听 chrome.tabs.onUpdated 事件并以某种方式修改目标DOM本身?

    解决方案


    1 - 每次访问页面时直接从内容脚本中读取所需数据(JSON序列化)。


    但是您必须这样做,每次你的网页加载,你想避免(我猜)


    2-维护扩展后台页面中的状态和使用消息传递将所需数据(也包括JSON序列化)传输到内容脚本环境。

    制作内容脚本和背景的唯一方法脚本交互是通过消息传递。您实际上并没有寻找替代解决方案,但您希望改进流程并避免每次加载页面时传递消息。



    为此,您可以开发一个规范。该规范说明了哪些网址或哪些网域或基于您希望从后台获取数据的某些条件。如果您当前的URL / Tab与规范相符,则只需将消息传递给后台即可。



    或者,Background也可以执行相同操作并仅在遵循规范时才发送消息。此外,当您的扩展加载时,您也可以将存储缓存到本地变量中。



    使用 Chrome Storage API 来监听存储设备的更改并相应地更新您的本地数据副本。



    您也可以查看我写的代码,使用相同的方法。


    I am new to both JavaScript and Chrome development, and am trying to create an extension that injects content/CSS in certain web pages. Simple enough, but the catch is that to do so it requires looking through a significant amount of data in local storage. From what I've read so far, the correct way to do this would be either:

    1. Reading the required data (JSON serialized) from storage directly from the content script every time the page is visited, or
    2. Maintaining the state in the extension background page and transferring the required data (also JSON serialized) to the content script environment using message passing.

    Either of these approaches, however, would be extremely inefficient due to large amounts of data being needlessly serialized and deserialized on every page load.

    So, I want to know:

    • Is it in any way possible to maintain a shared memory cache in Chrome that content scripts injected in all tabs can access?

    • If not, is an alternate approach possible where the background page listens for the chrome.tabs.onUpdated event and somehow modifies the target DOM itself?

    解决方案

    1- Reading the required data (JSON serialized) from storage directly from the content script every time the page is visited.

    But you have to do that every time your page is loaded which you want to avoid (I guess)

    2- Maintaining the state in the extension background page and transferring the required data (also JSON serialized) to the content script environment using message passing.

    The only way to make Content Scripts and Background scripts interact is via Message Passing. You are not actually looking to an alternative solution but you want to improve the process and avoid message passing each time a page is loaded.

    For this, you can develop a spec. The spec states for which URLs or which Domains or based on some condition you want to get the data from Background. If your current URL/Tab agrees with spec only then pass a message to background.

    Optionally, Background can also do the same and send message only if your spec is followed. Moreover, when your extension is loaded, you can also cache the storage into local variable.

    Use Chrome Storage API to listen for changes in storage and update your local data copy accordingly.

    You can also look at this code written by me using the same approach.

    这篇关于在Chrome扩展内容脚本中共享内存中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆