在chrome扩展中引入加载时间延迟
对于一个研究项目,我正在编写一个 chrome 扩展,当我访问某个 url 时,它应该引入 2 秒的加载延迟。所以基本上我想要用户输入“example.com”的效果,他们必须在页面开始加载之前等待 2 秒。有什么办法可以用 chrome 扩展来做到这一点吗?我已经搜索了文档,但似乎找不到类似的内容。
For a research project, I am coding a chrome extension where when I go to a certain url, it should introduce a load delay of 2 seconds. So basically I'd like the effect where the user types in "example.com", they have to wait 2 extra seconds before the page begins to load. Is there any way to do this with chrome extensions? I've scoured the documentation but can't seem to find anything like this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Chris 的解决方案可以工作,但它会锁定 UI。您也可以尝试这个简单的内容脚本:
content_script.js:
manifest.json:
我必须说它非常烦人。此外,精明的用户会知道发生了什么事,因为即使没有“正在等待 www.example.com...”状态行,它也会显示空白页面。
如果你想完美地模仿慢速网络,我认为最好的方法是创建一个引入延迟的 HTTP/HTTPS(或者可能是 SOCKS5)代理。您可以通过创建 proxy.pac 文件使浏览器仅在某些 URL 上使用代理或者使用 Chrome 实验性代理 API。
那么你想模拟什么呢?如果你想模拟高CPU JS或渲染,那么使用Chris的解决方案。如果你想模拟慢速网络,请使用代理。我的解决方案有点模拟慢速网络,但它并不是完美的伪装。
Chris's solution would work but it locks up the UI. You might also try this simple content script:
content_script.js:
manifest.json:
I must say though that it's very annoying. Furthermore, an astute user will know that something's up because it will show a blank page even when there is no "Waiting for www.example.com..." status line.
If you want to perfectly mimic a slow network, I think the best way would be to create a HTTP/HTTPS (or maybe SOCKS5) proxy that introduces the delay. You can make the browser use the proxy only on certain URLs by creating a proxy.pac file or by using the Chrome experimental proxy API.
So what do you want to simulate? If you want to simulate high-CPU JS or rendering, then use Chris's solution. If you want to simulate slow network, use a proxy. My solution kind of simulates a slow network but it's not a perfect disguise.
在您的
manifest.json
文件中,添加:并创建一个
sleep.js
文件,其中包含:(可从 phpjs)
因此,如果有人访问 www.example.com,页面将在加载前等待 2 秒。
但是,如果页面重定向到另一个页面(example.com 会这样做,因此最好尝试不同的网站:)),则这将不起作用。
请检查 清单文档 了解更多信息。
In your
manifest.json
file, add in:And create a
sleep.js
file containing:(found from phpjs)
So if someone goes to www.example.com, the page will wait 2 seconds before loading.
However, this won't work if the page redirects to another page (which example.com does, so a different site would be better to try on :))
Check the manifest docs for more info.
克里斯和永然的解决方案对我来说都不起作用。对我有用的:
sleep() 的 JavaScript 版本是什么?
Both Chris's and Yonran's solution didn't work for me. What worked for me:
What is the JavaScript version of sleep()?