user3063045 user3063045 - 29 days ago 9
Javascript Question

Chrome Extension Stop Loading Pages after Number of Seconds

This is similar to this post but I don't know how to get it working without the click action:

chrome extension : Stop loading the page on launch

I want to create a Chrome extension that doesn't require clicking to work. So I want it it stop loading ALL pages after a set number of seconds after they've been launched.

So if the extension is installed and a new tab opens up the page will load for only 10 seconds and then stop loading.

How can I stop pages from loading after X seconds on all pages via chrome extension?

Xan Xan
Answer

Well, the question you link to uses Programmatic Injection to add a content script to stop loading. That works great if you want it only sometimes.

If you want something with a content script to happen always, you define it in the manifest:

If your content script's code should always be injected, register it in the extension manifest using the content_scripts field

"content_scripts" : [{
  "matches": ["<all_urls>"],
  "js": ["stop.js"],
  "run_at": "document_start"
}],

With that, you know that stop.js is loaded alongside every page at the beginning of page load. Then you can implement your logic. Dead-simple solution:

// stop.js
setTimeout(() => {window.stop()}, 10000);

Note that this will trigger an install-time permission warning "Read and modify all your data on all websites you visit", since you can no longer rely on "activeTab" but do something without user interaction.