在chrome扩展中引入加载时间延迟

发布于 2024-11-03 06:04:24 字数 158 浏览 0 评论 0原文

对于一个研究项目,我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

作妖 2024-11-10 06:04:24

Chris 的解决方案可以工作,但它会锁定 UI。您也可以尝试这个简单的内容脚本:

content_script.js:

document.documentElement.style.display = 'none';
setTimeout(function() {document.documentElement.style.display = '';}, 1000);

manifest.json:

{
  "name": "Delay webpage displays",
  "version": "1.0",
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["content_script.js"],
      "run_at": "document_start"
    }
  ]  
}

我必须说它非常烦人。此外,精明的用户会知道发生了什么事,因为即使没有“正在等待 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:

document.documentElement.style.display = 'none';
setTimeout(function() {document.documentElement.style.display = '';}, 1000);

manifest.json:

{
  "name": "Delay webpage displays",
  "version": "1.0",
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["content_script.js"],
      "run_at": "document_start"
    }
  ]  
}

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.

天赋异禀 2024-11-10 06:04:24

在您的 manifest.json 文件中,添加:

"content_scripts": [{
    "matches": ["http://*.example.com/*"], // add in different URLs here
    "js": ["sleep.js"],
    "run_at": "document_start"
}]

并创建一个 sleep.js 文件,其中包含:(

function sleep (seconds) {
    var start = new Date().getTime();
    while (new Date() < start + seconds*1000) {}
    return 0;
}
sleep(2);

可从 phpjs)

因此,如果有人访问 www.example.com,页面将在加载前等待 2 秒。
但是,如果页面重定向到另一个页面(example.com 会这样做,因此最好尝试不同的网站:)),则这将不起作用。

请检查 清单文档 了解更多信息。

In your manifest.json file, add in:

"content_scripts": [{
    "matches": ["http://*.example.com/*"], // add in different URLs here
    "js": ["sleep.js"],
    "run_at": "document_start"
}]

And create a sleep.js file containing:

function sleep (seconds) {
    var start = new Date().getTime();
    while (new Date() < start + seconds*1000) {}
    return 0;
}
sleep(2);

(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.

无声无音无过去 2024-11-10 06:04:24

克里斯和永然的解决方案对我来说都不起作用。对我有用的:
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()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文