Skip to content

Commit

Permalink
fix(safari): fix GM.openInTab in safari always opened in background i…
Browse files Browse the repository at this point in the history
…ssue
  • Loading branch information
magicdawn committed Oct 8, 2024
1 parent de1554a commit 1262506
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/components/VideoCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useCurrentUsingTab, videoSourceTabState } from '$components/RecHeader/t
import { ETab } from '$components/RecHeader/tab-enum'
import { type AppRecItemExtend, type PvideoJson, type RecItemType, isRanking } from '$define'
import { EApiType } from '$define/index.shared'
import { openNewTab } from '$modules/gm'
import { DislikeIcon, OpenExternalLinkIcon, WatchLaterIcon } from '$modules/icon'
import { IconPark } from '$modules/icon/icon-park'
import { dynamicFeedFilterSelectUp } from '$modules/rec-services/dynamic-feed'
Expand Down Expand Up @@ -410,7 +411,7 @@ const VideoCardInner = memo(function VideoCardInner({

function openInNewWindow() {
const u = `/?dyn-mid=${authorMid}`
GM.openInTab(u, { insert: true, active: true, setParent: true })
openNewTab(u)
}

// newWindow ??= tab !== ETab.DynamicFeed
Expand Down
7 changes: 2 additions & 5 deletions src/components/VideoCard/top-marks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { flexCenterStyle, flexVerticalCenterStyle } from '$common/emotion-css'
import { colorPrimaryValue } from '$components/ModalSettings/theme.shared'
import type { RankingItemExtend, RecItemType } from '$define'
import { EApiType } from '$define/index.shared'
import { openNewTab } from '$modules/gm'
import type { NormalRankingItem } from '$modules/rec-services/hot/ranking/api.normal-category'
import {
RANKING_CATEGORIES_MAP,
Expand Down Expand Up @@ -123,11 +124,7 @@ export function RankingNumMark({ item }: { item: RankingItemExtend }) {
key: x.bvid,
label: x.title,
onClick() {
GM.openInTab(new URL(`/video/${x.bvid}`, location.href).href, {
active: true,
insert: true,
setParent: true,
})
openNewTab(new URL(`/video/${x.bvid}`, location.href).href)
},
}
}),
Expand Down
3 changes: 2 additions & 1 deletion src/components/VideoCard/use/_pip-window.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { APP_CLS_ROOT } from '$common'
import { useLessFrequentFn } from '$common/hooks/useLessFrequentFn'
import { AntdApp } from '$components/AntdApp'
import { colorPrimaryValue } from '$components/ModalSettings/theme.shared'
import { openNewTab } from '$modules/gm'
import { isEdge } from '$ua'
import createEmotion from '@emotion/css/create-instance'
import { Global } from '@emotion/react'
Expand Down Expand Up @@ -156,7 +157,7 @@ function CloseThenOpenButton({ newHref, pipWindow }: { pipWindow: Window; newHre
const u = new URL(newHref)
u.searchParams.delete(QueryKey.PlayerScreenMode)
u.searchParams.delete(QueryKey.ForceAutoPlay)
GM.openInTab(u.href, { active: true, insert: true, setParent: true })
openNewTab(u.href)
}

return (
Expand Down
3 changes: 2 additions & 1 deletion src/components/VideoCard/use/useOpenRelated.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { baseDebug } from '$common'
import type { RecItemType } from '$define'
import { EApiType } from '$define/index.shared'
import { openNewTab } from '$modules/gm'
import { isNormalRankingItem } from '$modules/rec-services/hot/ranking/category'
import { settings, useSettingsSnapshot } from '$modules/settings'
import { getVideoDetail } from '$modules/video/video-detail'
Expand Down Expand Up @@ -75,7 +76,7 @@ export function useOpenRelated({

const handleCommon = () => {
const active = mode !== Mode.Background
GM.openInTab(newHref, { insert: true, active, setParent: true })
openNewTab(newHref, active)
}

const handlers: Record<Mode, () => void> = {
Expand Down
14 changes: 14 additions & 0 deletions src/modules/gm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { isSafari } from '$ua'

export function openNewTab(url: string, active = true) {
if (isSafari) {
// https://github.com/quoid/userscripts?tab=readme-ov-file#api
GM.openInTab(url, !active)
} else {
GM.openInTab(url, {
active,
insert: true,
setParent: true,
})
}
}
4 changes: 1 addition & 3 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ declare global {
}
| undefined
}
}

declare global {
interface VMScriptGMTabOptions {
/** tempermonkey only, https://www.tampermonkey.net/documentation.php?locale=en#api:GM_openInTab */
/** tampermonkey only, https://www.tampermonkey.net/documentation.php?locale=en#api:GM_openInTab */
setParent?: boolean
}
}

0 comments on commit 1262506

Please sign in to comment.