|
| 1 | +import { PlatformAdapter, ItemChangeCallback } from '../../core/interfaces/platform-adapter'; |
| 2 | +import { PlatformId } from '../../types'; |
| 3 | +import { isInstagramUrl, getVideoId, getPlatformType } from './parser'; |
| 4 | +import { InstagramDomEnforcer } from './enforcer'; |
| 5 | + |
| 6 | +export class InstagramAdapter implements PlatformAdapter { |
| 7 | + public readonly id: PlatformId; |
| 8 | + public name: string; |
| 9 | + |
| 10 | + private readonly WATCH_THRESHOLD = 1500; |
| 11 | + |
| 12 | + private callback: ItemChangeCallback | null = null; |
| 13 | + private lastVideoId: string | null = null; |
| 14 | + private pendingVideoId: string | null = null; |
| 15 | + |
| 16 | + private popstateHandler: (() => void) | null = null; |
| 17 | + private visibilityHandler: (() => void) | null = null; |
| 18 | + private urlCheckInterval: number | null = null; |
| 19 | + |
| 20 | + private feedObserver: IntersectionObserver | null = null; |
| 21 | + private domObserver: MutationObserver | null = null; |
| 22 | + |
| 23 | + private watchTimer: number | null = null; |
| 24 | + private isWatching: boolean = false; |
| 25 | + private countedItems = new Set<string>(); |
| 26 | + |
| 27 | + private enforcer = new InstagramDomEnforcer(); |
| 28 | + |
| 29 | + constructor( |
| 30 | + currentUrl: string, |
| 31 | + private onModeChange: () => void, |
| 32 | + ) { |
| 33 | + this.id = getPlatformType(currentUrl); |
| 34 | + this.name = this.id === 'instagram_reels' ? 'Instagram Reels' : 'Instagram Feed'; |
| 35 | + } |
| 36 | + |
| 37 | + private startOrResumeWatchTimer(id: string) { |
| 38 | + if (this.isWatching) return; |
| 39 | + if (this.countedItems.has(id)) return; |
| 40 | + |
| 41 | + this.isWatching = true; |
| 42 | + this.watchTimer = window.setTimeout(() => { |
| 43 | + const isReels = this.id === 'instagram_reels'; |
| 44 | + const isValid = isReels |
| 45 | + ? getVideoId(window.location.href) === id |
| 46 | + : this.pendingVideoId === id; |
| 47 | + |
| 48 | + if (!document.hidden && isValid) { |
| 49 | + if (!this.countedItems.has(id)) { |
| 50 | + this.countedItems.add(id); |
| 51 | + if (this.callback) this.callback(id); |
| 52 | + } |
| 53 | + } |
| 54 | + this.isWatching = false; |
| 55 | + this.watchTimer = null; |
| 56 | + }, this.WATCH_THRESHOLD); |
| 57 | + } |
| 58 | + |
| 59 | + private pauseWatchTimer() { |
| 60 | + if (this.watchTimer) { |
| 61 | + window.clearTimeout(this.watchTimer); |
| 62 | + this.watchTimer = null; |
| 63 | + } |
| 64 | + this.isWatching = false; |
| 65 | + } |
| 66 | + |
| 67 | + private handleItemChange(id: string) { |
| 68 | + this.pendingVideoId = id; |
| 69 | + this.pauseWatchTimer(); |
| 70 | + if (!document.hidden) { |
| 71 | + this.startOrResumeWatchTimer(id); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private setupFeedObservation() { |
| 76 | + if (this.feedObserver) return; |
| 77 | + |
| 78 | + this.feedObserver = new IntersectionObserver( |
| 79 | + (entries) => { |
| 80 | + entries.forEach((entry) => { |
| 81 | + const link = entry.target.querySelector('a[href*="/p/"], a[href*="/reel/"]'); |
| 82 | + const postId = link ? link.getAttribute('href') : null; |
| 83 | + |
| 84 | + if (entry.isIntersecting && postId) { |
| 85 | + this.pendingVideoId = postId; |
| 86 | + this.startOrResumeWatchTimer(postId); |
| 87 | + } else if (!entry.isIntersecting && postId === this.pendingVideoId) { |
| 88 | + this.pauseWatchTimer(); |
| 89 | + this.pendingVideoId = null; |
| 90 | + } |
| 91 | + }); |
| 92 | + }, |
| 93 | + { threshold: 0.6 }, |
| 94 | + ); |
| 95 | + |
| 96 | + this.domObserver = new MutationObserver(() => { |
| 97 | + const articles = document.querySelectorAll('article'); |
| 98 | + articles.forEach((article) => { |
| 99 | + if (!article.hasAttribute('data-limitra-observed')) { |
| 100 | + article.setAttribute('data-limitra-observed', 'true'); |
| 101 | + this.feedObserver?.observe(article); |
| 102 | + } |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + this.domObserver.observe(document.body, { childList: true, subtree: true }); |
| 107 | + } |
| 108 | + |
| 109 | + private stopFeedObservation() { |
| 110 | + if (this.feedObserver) { |
| 111 | + this.feedObserver.disconnect(); |
| 112 | + this.feedObserver = null; |
| 113 | + } |
| 114 | + if (this.domObserver) { |
| 115 | + this.domObserver.disconnect(); |
| 116 | + this.domObserver = null; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public isCurrentPlatform(url: string): boolean { |
| 121 | + return isInstagramUrl(url); |
| 122 | + } |
| 123 | + |
| 124 | + public observe(onItemChange: ItemChangeCallback): void { |
| 125 | + this.callback = onItemChange; |
| 126 | + this.lastVideoId = null; |
| 127 | + |
| 128 | + this.visibilityHandler = () => { |
| 129 | + if (document.hidden) { |
| 130 | + this.pauseWatchTimer(); |
| 131 | + } else { |
| 132 | + if (this.pendingVideoId) { |
| 133 | + this.startOrResumeWatchTimer(this.pendingVideoId); |
| 134 | + } |
| 135 | + } |
| 136 | + }; |
| 137 | + document.addEventListener('visibilitychange', this.visibilityHandler); |
| 138 | + |
| 139 | + this.observeUrlChanges(); |
| 140 | + |
| 141 | + if (this.id === 'instagram_feed') { |
| 142 | + this.setupFeedObservation(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + public disconnect(): void { |
| 147 | + this.pauseWatchTimer(); |
| 148 | + this.stopFeedObservation(); |
| 149 | + this.countedItems.clear(); |
| 150 | + |
| 151 | + if (this.urlCheckInterval !== null) { |
| 152 | + window.clearInterval(this.urlCheckInterval); |
| 153 | + this.urlCheckInterval = null; |
| 154 | + } |
| 155 | + if (this.popstateHandler) { |
| 156 | + window.removeEventListener('popstate', this.popstateHandler); |
| 157 | + this.popstateHandler = null; |
| 158 | + } |
| 159 | + if (this.visibilityHandler) { |
| 160 | + document.removeEventListener('visibilitychange', this.visibilityHandler); |
| 161 | + this.visibilityHandler = null; |
| 162 | + } |
| 163 | + |
| 164 | + this.enforcer.stop(); |
| 165 | + |
| 166 | + this.callback = null; |
| 167 | + this.pendingVideoId = null; |
| 168 | + this.lastVideoId = null; |
| 169 | + } |
| 170 | + |
| 171 | + public executePunishment(): void { |
| 172 | + this.enforcer.enforce(); |
| 173 | + } |
| 174 | + |
| 175 | + private checkUrl() { |
| 176 | + const currentUrl = window.location.href; |
| 177 | + const evaluatedMode = getPlatformType(currentUrl); |
| 178 | + |
| 179 | + if (evaluatedMode !== this.id) { |
| 180 | + this.onModeChange(); |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + if (this.id === 'instagram_reels') { |
| 185 | + const currentId = getVideoId(currentUrl); |
| 186 | + if (currentId !== this.lastVideoId) { |
| 187 | + this.lastVideoId = currentId; |
| 188 | + if (currentId) { |
| 189 | + void this.handleItemChange(currentId); |
| 190 | + } else { |
| 191 | + this.pauseWatchTimer(); |
| 192 | + this.pendingVideoId = null; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + private observeUrlChanges() { |
| 199 | + this.popstateHandler = () => this.checkUrl(); |
| 200 | + window.addEventListener('popstate', this.popstateHandler); |
| 201 | + |
| 202 | + this.urlCheckInterval = window.setInterval(() => { |
| 203 | + this.checkUrl(); |
| 204 | + if ( |
| 205 | + this.id === 'instagram_reels' && |
| 206 | + !document.hidden && |
| 207 | + !this.isWatching && |
| 208 | + this.pendingVideoId |
| 209 | + ) { |
| 210 | + if (getVideoId(window.location.href) === this.pendingVideoId) { |
| 211 | + this.startOrResumeWatchTimer(this.pendingVideoId); |
| 212 | + } |
| 213 | + } |
| 214 | + }, 500); |
| 215 | + |
| 216 | + this.checkUrl(); |
| 217 | + } |
| 218 | + |
| 219 | + public isVideoPlaying(): boolean { |
| 220 | + const video = document.querySelector('video') as HTMLVideoElement | null; |
| 221 | + return !!video && !video.paused; |
| 222 | + } |
| 223 | +} |
0 commit comments