Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

content_scripts "world": "MAIN" 报错 #26

Open
724493602 opened this issue Jun 9, 2023 · 7 comments
Open

content_scripts "world": "MAIN" 报错 #26

724493602 opened this issue Jun 9, 2023 · 7 comments

Comments

@724493602
Copy link

manifest 中 content_scripts 在"world" = "MAIN" 时 没有chrome.runtime对象,引发报错

@Jervis2049
Copy link
Owner

不知道"world" = "MAIN"是什么,可否描述更清楚一些,比如给出报错的截图,或贴出相关的代码逻辑。

@724493602
Copy link
Author

{
"js": ["dist/main.js"],
"world": "MAIN"
},
"world"= "MAIN"时,会在沙盒环境中运行,这是的脚本没有windows

@724493602
Copy link
Author

是没有chrome.runtime,说错了

@Jervis2049
Copy link
Owner

感谢您提出的问题,我知道原因了,我提供的插件暂时实现不了这个,短期内还改不了。

@Jervis2049
Copy link
Owner

还没用过这个"world"= "MAIN" , 想请教一下是用于什么场景的。我或许可以做个区分,如果有"world"= "MAIN" ,打包时对这个content_scripts做不同的处理。

@724493602
Copy link
Author

获取当前的window 环境,我们有个场景是操作某个网站的编辑器,这个编辑器是挂载在window上,必须要 word=main 才能获取到。这时候这个content_scripts是不是就 IIFE 加载就好了

@Jervis2049
Copy link
Owner

获取当前的window 环境,我们有个场景是操作某个网站的编辑器,这个编辑器是挂载在window上,必须要 word=main 才能获取到。这时候这个content_scripts是不是就 IIFE 加载就好了

这个场景不声明"word": "main", 在content_scripts里面注入一个injected.js应该也能达到目的吧? 如这个例子所示examples/crx-webAccessibleResources/content.js , 先看看是否可行

const script = document.createElement('script')
script.src = chrome.runtime.getURL('injected.ts')
;(document.head || document.documentElement).appendChild(script)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants