简体   繁体   English

如何在为 Google Chrome 扩展程序完全加载页面之前运行内容脚本

[英]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.

相关问题 有没有办法在加载DOM之后但在执行任何页面脚本之前运行Google Chrome内容脚本? - Is there a way to run a Google Chrome content script after the DOM is loaded but before any page scripts are executed? Chrome扩展程序,如何延迟脚本直到页面完全加载 - Chrome extension, how to delay script until page is fully loaded 每次使用Google扩展插件加载页面时运行内容脚本吗? - Run content script every time a page loads with Google Extensions? 有没有办法在页面加载之后但在脚本执行之前运行内容脚本? - Is there a way to run a content script after the page is loaded but before scripts are executed? 页面和所有扩展程序完全加载后,如何运行javascript? - How can I run javascript after the page and all extensions are fully loaded? Google Chrome 扩展:如何在以编程方式注入的内容脚本中包含 jQuery? - Google Chrome Extensions: How to include jQuery in programmatically injected content script? 页面完全加载前如何触发onclick - How to trigger onclick before page is fully loaded 在页面绘制之前运行Chrome扩展内容脚本 - Run a chrome extesions content script before page draw 如何在处理 DOM 之前使用谷歌浏览器内容脚本修改 web 页面? - How can I modify a web page using google chrome content script before the DOM is processed? 加载新内容时运行脚本 - Chrome 扩展 - Run script as new content is loaded - Chrome Extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM