[英]How to make a content script run before a page is fully loaded for Google Chrome Extensions
As a fun project, I am working on creating a "Dark Mode" content script for a Google Chrome Extension.作为一个有趣的项目,我正在为 Google Chrome 扩展程序创建一个“黑暗模式”内容脚本。 I know the project might sound silly because of all the dark mode readers already available for all apps, but I am just trying to start small with the extensions and because I use the dark-mode reader so often, it seemed obvious to start with this.我知道这个项目可能听起来很傻,因为所有应用程序都已经提供了所有暗模式阅读器,但我只是试图从扩展开始,而且因为我经常使用暗模式阅读器,所以从这个开始似乎很明显.
The content-script itself hasn't caused me much issue, but I am running into trouble between the time it takes for the page to load and the content-script to run.内容脚本本身并没有给我带来太多问题,但是我在页面加载和内容脚本运行之间遇到了麻烦。 Although there is a very small difference in time, it is annoying to briefly see a bright-white page when I'm in the dark before the content-script activates to make everything dark again.虽然时间上的差异很小,但在内容脚本激活以使所有内容再次变暗之前,当我处于黑暗中时,短暂地看到一个亮白色的页面是很烦人的。 I understand that there is this command that I can add to the manifest:我知道有这个命令可以添加到清单中:
"run_at": "document_start",
but this ends up causing no changes to the page.但这最终导致页面没有变化。 It seems that running at "document_start"
might not give time for all the HTML to load, thereby preventing the script from actually changing anything.似乎在"document_start"
处运行可能不会给所有 HTML 加载时间,从而阻止脚本实际更改任何内容。 Does anyone have any advice to at least give an illusion of the content-script running immediately so I don't see the original page and then the content-script modified page?有没有人有任何建议至少给人一种内容脚本立即运行的错觉,所以我看不到原始页面,然后是内容脚本修改页面? I want the changes to be more seamless and I don't notice the page actively changing if that makes sense.我希望更改更加无缝,如果有意义的话,我不会注意到页面会主动更改。
Why don't you try by keeping the script tag at the bottom of the body tag.为什么不尝试将脚本标记保留在正文标记的底部。
<body>
.
.
<script>
</script>
<body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.