简体   繁体   中英

How can I delay a new tab from loading with a userscript?

I use a userscript to modify the client-side code of a website. This code is adding an anchor tag to the page. Its target is _blank . The thing is that if I click this link too frequently, the site errors. A simple refresh on the new tab fixes the problem.

When I click on the link and it instantly opens a new tab. But I don't want that new tab to render until I visit it, or with some sort of time delay. Is there a way of achieving this?

I am using Firefox, so Firefox-only solutions are fine. I found this , but I don't see a way of using it to prevent the tab from rendering in the first place. When I Google for this, I see results about add-ons that can solve the problem. But, the links to them always 404. Ideally, the solution would only affect the tabs created by this script instead of the way all tabs work, but if the only way to do it is to affect the way all tabs work, I'd accept that as a solution.

The Tampermonkey documentation says there is a GM_openInTab function. It has a parameter called loadInBackgroun d, but it only decides if the new tab is focused when you click the link.

If there is a way of making this new tab render some HTML of my choosing, I think that would be a neat solution. ie, I'd write some HTML that, on focus, goes to the actual website's page. If this is an option, I'd need to know how to open a tab to HTML of my choosing in grease monkey.

(Just realization of idea you told in your question yourself)

You can place simple page that waits for focus and then redirects to what you pass in URL parameter somewhere and open in background tabs. Like:

load-url-from-search-on-focus.html?http://example.com :

<!doctype html>
<body
 onload="document.title=u=location.search.slice(1)"
 onfocus="u?document.location.replace(u):document.write('?search missing')">

Try it.


(data:uri could have been used instead of hosted page, if there weren't those pesky security precautions blocking rendering of top-level datauri navigations:|)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM