Skip to content

Commit fa09004

Browse files
committed
feat: use GM.openInTab and add background param
1 parent 5c676ad commit fa09004

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

.changeset/wet-bikes-scream.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@sv443-network/userutils": minor
3+
---
4+
5+
Made `openInNewTab()` use `GM.openInTab` by default and fall back to the old behavior.
6+
Also added `background` param to specify if the tab should get focus when opened.

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,22 +582,23 @@ preloadImages([
582582
### openInNewTab()
583583
Usage:
584584
```ts
585-
openInNewTab(url: string): void
585+
openInNewTab(url: string, background?: boolean): void
586586
```
587587

588-
Creates an invisible anchor with a `_blank` target and clicks it.
589-
Contrary to `window.open()`, this has a lesser chance to get blocked by the browser's popup blocker and doesn't open the URL as a new window.
590-
This function has to be run in response to a user interaction event, else the browser might reject it.
588+
Tries to use `GM.openInTab` to open the given URL in a new tab, or as a fallback if the grant is not given, creates an invisible anchor element and clicks it.
589+
If `background` is set to true, the tab will be opened in the background. Leave `undefined` to use the browser's default behavior.
591590

592-
⚠️ This function needs to be run after the DOM has loaded (when using `@run-at document-end` or after `DOMContentLoaded` has fired).
591+
⚠️ Needs the `@grant GM.openInTab` directive, otherwise only the fallback behavior will be used and the warning below is extra important:
592+
⚠️ For the fallback to work, this function needs to be run in response to a user interaction event, else the browser might reject it.
593593

594594
<details><summary><b>Example - click to view</b></summary>
595595

596596
```ts
597597
import { openInNewTab } from "@sv443-network/userutils";
598598

599599
document.querySelector("#my-button").addEventListener("click", () => {
600-
openInNewTab("https://example.org/");
600+
// open in background:
601+
openInNewTab("https://example.org/", true);
601602
});
602603
```
603604

lib/dom.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,30 @@ export function preloadImages(srcUrls: string[], rejects = false) {
6666
}
6767

6868
/**
69-
* Creates an invisible anchor with a `_blank` target and clicks it.
70-
* Contrary to `window.open()`, this has a lesser chance to get blocked by the browser's popup blocker and doesn't open the URL as a new window.
71-
*
72-
* This function has to be run in response to a user interaction event, else the browser might reject it.
69+
* Tries to use `GM.openInTab` to open the given URL in a new tab, otherwise if the grant is not given, creates an invisible anchor element and clicks it.
70+
* For the fallback to work, this function needs to be run in response to a user interaction event, else the browser might reject it.
71+
* @param href The URL to open in a new tab
72+
* @param background If set to `true`, the tab will be opened in the background - set to `undefined` (default) to use the browser's default behavior
7373
*/
74-
export function openInNewTab(href: string) {
75-
const openElem = document.createElement("a");
76-
Object.assign(openElem, {
77-
className: "userutils-open-in-new-tab",
78-
target: "_blank",
79-
rel: "noopener noreferrer",
80-
href,
81-
});
82-
openElem.style.display = "none";
83-
84-
document.body.appendChild(openElem);
85-
openElem.click();
86-
// timeout just to be safe
87-
setTimeout(openElem.remove, 50);
74+
export function openInNewTab(href: string, background?: boolean) {
75+
try {
76+
GM.openInTab(href, background);
77+
}
78+
catch(e) {
79+
const openElem = document.createElement("a");
80+
Object.assign(openElem, {
81+
className: "userutils-open-in-new-tab",
82+
target: "_blank",
83+
rel: "noopener noreferrer",
84+
href,
85+
});
86+
openElem.style.display = "none";
87+
88+
document.body.appendChild(openElem);
89+
openElem.click();
90+
// timeout just to be safe
91+
setTimeout(openElem.remove, 50);
92+
}
8893
}
8994

9095
/**

0 commit comments

Comments
 (0)