Share in-memory objects in Chrome extension content scripts?

霸气de小男生 提交于 2019-12-06 03:13:24

1) I don't think this is possible. You seem to have exhausted the possibilities.

2) Content scripts are the only way to access/modify a normal tab's DOM.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!