admin管理员组文章数量:1356595
For my web extension I would like to have a "debug mode" which makes troubleshooting issues with the extension easier. By default this debug mode should be disabled, but it should be possible to enable it at runtime.
I don't have a settings page for the extension yet (and am also not planning on adding one soon), so I don't want to create one just for enabling this debug mode. Also the debug mode should hopefully rarely be needed.
Therefore one approach I was thinking of is using the Console of the background script when debugging the extension in the browser (Firefox, Chrome), to somehow inform the background script or modify its state to enable the "debug mode" of my extension.
For Firefox and Chrome what seems to work is:
- In the debug Console set a custom
window
property / global variable, for examplemyExtDebug = true
- In the background script check
window.myExtDebug === true
However, my questions are:
- Are there any downsides of this
window
approach, for example security-wise? How reliable is it / to what extent does it rely on implementation details?
(Note that I am using a property name which includes the name of my extension, so chances of an accidental collision with another property should be low.) - What other approaches exist?
For my web extension I would like to have a "debug mode" which makes troubleshooting issues with the extension easier. By default this debug mode should be disabled, but it should be possible to enable it at runtime.
I don't have a settings page for the extension yet (and am also not planning on adding one soon), so I don't want to create one just for enabling this debug mode. Also the debug mode should hopefully rarely be needed.
Therefore one approach I was thinking of is using the Console of the background script when debugging the extension in the browser (Firefox, Chrome), to somehow inform the background script or modify its state to enable the "debug mode" of my extension.
For Firefox and Chrome what seems to work is:
- In the debug Console set a custom
window
property / global variable, for examplemyExtDebug = true
- In the background script check
window.myExtDebug === true
However, my questions are:
- Are there any downsides of this
window
approach, for example security-wise? How reliable is it / to what extent does it rely on implementation details?
(Note that I am using a property name which includes the name of my extension, so chances of an accidental collision with another property should be low.) - What other approaches exist?
1 Answer
Reset to default 0The Mozilla documentation says:
Background scripts run in the context of a special page called a background page. This gives them a
window
global
Which suggests the approach of using window
might be officially supported.
For Chrome since Manifest v3 there are no background scripts and pages anymore, instead service workers have to be used. They don't seem to have access to window
but there is the concept of an offscreen document (not sure if this can be used for the purpose of the question).
(Note: This just describes my observations. Feel free to add your own answer if you can provide more information.)
本文标签: google chrome extensionModify background script state from browser debug consoleStack Overflow
版权声明:本文标题:google chrome extension - Modify background script state from browser debug console - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744066270a2585055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
window
approach works for Chrome as well. Must have done something wrong during initial testing. Have updated the question now. – Marcono1234 Commented Mar 30 at 12:06