-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathgetAutoMedia.ts
More file actions
140 lines (113 loc) · 4.87 KB
/
getAutoMedia.ts
File metadata and controls
140 lines (113 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { FlatMediaInfo, flattenMediaInfos, MediaData, MediaDataWithScopes, MediaPath, MediaScope } from "@/contentScript/isolated/utils/genMediaInfo"
import { checkContentScript, compareFrame, TabInfo } from "@/utils/browserUtils"
import { fetchView } from "@/utils/state"
const WEIGHTS = {
SAME_FRAME: 0b10_000_000_000_000,
IS_VISIBLE: 0b1_000_000_000_000,
LARGE_VIDEO: 0b100_000_000_000,
PEAK_INTERSECT: 0b100_000_000_000,
INTERSECT_90: 0b10_000_000_000,
INTERSECT_50: 0b1_000_000_000,
INTERSECT_10: 0b100_000_000,
ACTIVE: 0b100_000_000,
DURATION_10M: 0b1000,
DURATION_3M: 0b100,
DURATION_1M: 0b10,
DISQUALIFIED: -0b1,
}
export async function getMediaDataWithScopes() {
const raw = await chrome.storage.session.get<RecordAny>()
let pinned: MediaPath
let scopes: MediaScope[] = []
for (let key in raw) {
if (!key.startsWith("m:")) continue
if (key.startsWith("m:pin")) {
pinned = raw[key]
} else if (key.startsWith("m:scope:")) {
scopes.push(raw[key])
}
}
return { pinned, scopes } satisfies MediaDataWithScopes
}
export async function clearClosed() {
const [tabs, data] = await Promise.all([chrome.tabs.query({}), chrome.storage.session.get<RecordAny>()])
const tabIds = new Set(tabs.map((t) => t.id))
const clearKeys: string[] = []
for (let key in data) {
if (!key.startsWith("m:scope:")) continue
if (tabIds.has(data[key]?.tabInfo.tabId)) continue
clearKeys.push(key)
}
chrome.storage.session.remove(clearKeys)
}
function pickHighest(infos: FlatMediaInfo[], tabInfo: TabInfo, now: number, peakIntersect: number) {
let highest: { info: FlatMediaInfo; score: number }
infos.forEach((info) => {
let score = 0
if (compareFrame(info.tabInfo, tabInfo) && tabInfo.frameId !== 0) score += WEIGHTS.SAME_FRAME
if (info.isVisible) score += WEIGHTS.IS_VISIBLE
if (!info.paused || (info.lastPlayed && now - info.lastPlayed < 60_000)) score += WEIGHTS.ACTIVE
if (info.intersectionRatio > 0.1 && info.intersectionRatio === peakIntersect) {
score += WEIGHTS.PEAK_INTERSECT
} else if (info.intersectionRatio > 0.9) {
score += WEIGHTS.INTERSECT_90
} else if (info.intersectionRatio > 0.5) {
score += WEIGHTS.INTERSECT_50
} else if (info.intersectionRatio > 0.1) {
score += WEIGHTS.INTERSECT_10
}
if (info.elementSize && info.elementSize.w > 200 && info.elementSize.h > 200) {
score += WEIGHTS.LARGE_VIDEO
}
if (info.infinity || info.duration >= 10 * 60) {
score += WEIGHTS.DURATION_10M
} else if (info.duration >= 3 * 60) {
score += WEIGHTS.DURATION_3M
} else if (info.duration >= 1 * 60) {
score += WEIGHTS.DURATION_1M
}
if (!info.isConnected && info.hasAudioTrack && info.domain === "open.spotify.com") score += WEIGHTS.SAME_FRAME
if (!highest || score > highest.score || (score === highest.score && (info.infinity ? 60 : info.duration) > highest.info.duration)) {
highest = { info, score }
}
})
return highest
}
export async function getMediaData() {
const d = await getMediaDataWithScopes()
return { pinned: d.pinned, infos: flattenMediaInfos(d.scopes) } satisfies MediaData
}
export async function getAutoMedia(tabInfo: TabInfo, videoOnly?: boolean) {
let [{ ignorePiP }, { infos, pinned }] = await Promise.all([fetchView({ ignorePiP: true }), getMediaData()])
infos = infos.filter((info) => info.readyState)
infos = videoOnly ? infos.filter((info) => info.videoSize) : infos
const pinnedInfo = infos.find((info) => info.key === pinned?.key)
if (pinnedInfo && (await checkContentScript(pinnedInfo.tabInfo.tabId, pinnedInfo.tabInfo.frameId))) return pinnedInfo
infos.sort((a, b) => b.creationTime - a.creationTime)
let pippedInfo = infos.find((info) => info.pipMode) || infos.find((info) => info.isDip)
if (pippedInfo && !(await checkContentScript(pippedInfo.tabInfo.tabId, pippedInfo.tabInfo.frameId))) {
pippedInfo = null
}
if (!ignorePiP && pippedInfo) return pippedInfo
if (tabInfo) {
infos = infos.filter((info) => info.tabInfo.tabId === tabInfo.tabId)
}
if (!infos.length) return pippedInfo || undefined
const now = Date.now()
let peakIntersect = infos
.filter((v) => v.intersectionRatio != null)
.sort((a, b) => b.intersectionRatio - a.intersectionRatio)[0]?.intersectionRatio
let highest = pickHighest(infos, tabInfo, now, peakIntersect)
if (!highest) return undefined
const isAlive = await checkContentScript(highest.info.tabInfo.tabId, highest.info.tabInfo.frameId)
if (isAlive) return highest.info
const staleKey = `m:scope:${highest.info.tabInfo.tabId}:${highest.info.tabInfo.frameId}`
await chrome.storage.session.remove(staleKey)
infos = infos.filter((info) => info.key !== highest.info.key)
if (!infos.length) return pippedInfo || undefined
peakIntersect = infos
.filter((v) => v.intersectionRatio != null)
.sort((a, b) => b.intersectionRatio - a.intersectionRatio)[0]?.intersectionRatio
highest = pickHighest(infos, tabInfo, Date.now(), peakIntersect)
return highest?.info || pippedInfo || undefined
}