Skip to content

Commit 5bf974d

Browse files
committed
feat: add argument additionalProps to openInNewTab()
1 parent 007aa9d commit 5bf974d

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

.changeset/great-donuts-roll.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sv443-network/userutils": minor
3+
---
4+
5+
Added `additionalProps` parameter to `openInNewTab()` to add or overwrite anchor element props (only if `GM.openInTab()` is unavailable)

docs.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,12 @@ preloadImages([
528528
### openInNewTab()
529529
Signature:
530530
```ts
531-
openInNewTab(url: string, background?: boolean): void
531+
openInNewTab(url: string, background?: boolean, additionalProps?: Partial<HTMLAnchorElement>): void
532532
```
533533

534534
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.
535535
If `background` is set to true, the tab will be opened in the background. Leave `undefined` to use the browser's default behavior.
536+
If `additionalProps` is set and `GM.openInTab` is not available, the given properties will be added or overwritten on the created anchor element.
536537

537538
⚠️ Needs the `@grant GM.openInTab` directive, otherwise only the fallback behavior will be used and the warning below is extra important:
538539
⚠️ For the fallback to work, this function needs to be run in response to a user interaction event, else the browser might reject it.
@@ -954,8 +955,8 @@ digitCount(num: number | Stringifiable): number
954955
```
955956

956957
Calculates and returns the amount of digits in the given number.
957-
The given value will be converted by being passed to `String()` and then `Number()` before the calculation.
958-
Returns `NaN` if the number is invalid.
958+
If it isn't a number already, the value will be converted by being passed to `String()` and then `Number()` before the calculation.
959+
Returns `NaN` if the number is invalid and `Infinity` if the number is too large to be represented as a regular number.
959960

960961
<details><summary><b>Example - click to view</b></summary>
961962

lib/dom.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,33 @@ export function preloadImages(srcUrls: string[], rejects = false): Promise<Promi
6161
* For the fallback to work, this function needs to be run in response to a user interaction event, else the browser might reject it.
6262
* @param href The URL to open in a new tab
6363
* @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
64+
* @param additionalProps Additional properties to set on the anchor element (only applies when `GM.openInTab` is not available)
6465
*/
65-
export function openInNewTab(href: string, background?: boolean) {
66+
export function openInNewTab(href: string, background?: boolean, additionalProps?: Partial<HTMLAnchorElement>): void {
6667
try {
67-
GM.openInTab(href, background);
68+
GM.openInTab?.(href, background);
6869
}
6970
catch(e) {
7071
const openElem = document.createElement("a");
7172
Object.assign(openElem, {
7273
className: "userutils-open-in-new-tab",
7374
target: "_blank",
7475
rel: "noopener noreferrer",
76+
tabIndex: -1,
77+
ariaHidden: "true",
7578
href,
79+
...additionalProps,
80+
});
81+
Object.assign(openElem.style, {
82+
display: "none",
83+
pointerEvents: "none",
7684
});
77-
openElem.style.display = "none";
7885

7986
document.body.appendChild(openElem);
8087
openElem.click();
81-
// timeout just to be safe
82-
setTimeout(openElem.remove, 50);
88+
89+
// schedule removal after the click event has been processed
90+
setTimeout(openElem.remove, 0);
8391
}
8492
}
8593

0 commit comments

Comments
 (0)