Skip to content

Commit 624dd9d

Browse files
committed
fix: new window tab creation
1 parent 583fb02 commit 624dd9d

5 files changed

Lines changed: 65 additions & 67 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ All notable changes to this project will be documented in this file. See [standa
44

55
### [1.11.3](https://github.com/drodil/video_auto_pause/compare/v1.11.2...v1.11.3) (2026-01-21)
66

7-
87
### Bug Fixes
98

10-
* cursor listeners ([3675887](https://github.com/drodil/video_auto_pause/commit/3675887b0c491644445386167ee9f73e4ec2d984))
11-
9+
- cursor listeners ([3675887](https://github.com/drodil/video_auto_pause/commit/3675887b0c491644445386167ee9f73e4ec2d984))
1210

1311
### Maintenance
1412

15-
* error handling + formatting ([7d33f8d](https://github.com/drodil/video_auto_pause/commit/7d33f8d146c083c322458de40dfe1b2c8114a69c))
16-
* fix prettier deps ([eb8763b](https://github.com/drodil/video_auto_pause/commit/eb8763bf25b77450b3b02d5c4e2f8fe42cc62985))
13+
- error handling + formatting ([7d33f8d](https://github.com/drodil/video_auto_pause/commit/7d33f8d146c083c322458de40dfe1b2c8114a69c))
14+
- fix prettier deps ([eb8763b](https://github.com/drodil/video_auto_pause/commit/eb8763bf25b77450b3b02d5c4e2f8fe42cc62985))
1715

1816
### [1.11.2](https://github.com/drodil/video_auto_pause/compare/v1.11.1...v1.11.2) (2025-06-05)
1917

manifests/chrome.json

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,8 @@
22
"name": "Video Auto Pause",
33
"version": "1.11.3",
44
"description": "Stops multiple video services on tab unfocus and continues on focus",
5-
"permissions": [
6-
"tabs",
7-
"storage",
8-
"activeTab",
9-
"scripting",
10-
"idle"
11-
],
12-
"host_permissions": [
13-
"<all_urls>"
14-
],
5+
"permissions": ["tabs", "storage", "activeTab", "scripting", "idle"],
6+
"host_permissions": ["<all_urls>"],
157
"homepage_url": "https://github.com/drodil/video_auto_pause",
168
"options_ui": {
179
"page": "options.html",

manifests/firefox.json

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@
22
"name": "Video Auto Pause",
33
"version": "1.11.3",
44
"description": "Stops various video services on tab unfocus and continues on focus",
5-
"permissions": [
6-
"tabs",
7-
"storage",
8-
"activeTab",
9-
"scripting",
10-
"idle"
11-
],
5+
"permissions": ["tabs", "storage", "activeTab", "scripting", "idle"],
126
"browser_specific_settings": {
137
"gecko": {
148
"id": "drodil@youtube_auto_pause",
159
"strict_min_version": "127.0"
1610
}
1711
},
18-
"host_permissions": [
19-
"<all_urls>"
20-
],
12+
"host_permissions": ["<all_urls>"],
2113
"homepage_url": "https://github.com/drodil/video_auto_pause",
2214
"options_ui": {
2315
"page": "options.html",
@@ -39,9 +31,7 @@
3931
}
4032
},
4133
"background": {
42-
"scripts": [
43-
"vap_bs.js"
44-
]
34+
"scripts": ["vap_bs.js"]
4535
},
4636
"commands": {
4737
"toggle-extension": {

src/vap_bs.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ env.storage.onChanged.addListener(async function () {
176176
changeIcon(true);
177177
}
178178

179-
for (let i = 0; i < tabs.length; i++) {
180-
if (isEnabledForTab(tabs[i]) && tabs[i].active) {
181-
resume(tabs[i]);
179+
for (const element of tabs) {
180+
if (isEnabledForTab(element) && element.active) {
181+
resume(element);
182182
}
183183
}
184184
});
@@ -203,10 +203,10 @@ env.tabs.onActivated.addListener(async function (info) {
203203
}
204204

205205
if (options.autopause && previous_tab !== -1) {
206-
debugLog(`Tab changed, stopping video from tab ${previous_tab}`);
207206
try {
208207
const prev = await env.tabs.get(previous_tab);
209-
if (prev) {
208+
if (prev && prev.windowId === tab.windowId) {
209+
debugLog(`Tab changed, stopping video from tab ${previous_tab}`);
210210
stop(prev);
211211
}
212212
} catch (e) {
@@ -257,14 +257,18 @@ env.tabs.onRemoved.addListener(function (tabId) {
257257
disabledTabs = disabledTabs.filter((tab) => tab !== tabId);
258258
save_settings();
259259
}
260+
261+
if (previous_tab === tabId) {
262+
previous_tab = -1;
263+
}
260264
});
261265

262266
// Window focus listener
263267
env.windows.onFocusChanged.addListener(async function (windowId) {
264268
if (windowId !== previous_window) {
265269
if (options.focuspause && state !== "locked") {
266270
const tabsStop = await env.tabs.query({ windowId: previous_window });
267-
debugLog(`Window changed, stopping videos in window ${windowId}`);
271+
debugLog(`Window changed, stopping videos in window ${previous_window}`);
268272
for (let i = 0; i < tabsStop.length; i++) {
269273
if (!isEnabledForTab(tabsStop[i])) {
270274
continue;
@@ -291,6 +295,12 @@ env.windows.onFocusChanged.addListener(async function (windowId) {
291295
}
292296
});
293297

298+
env.windows.onRemoved.addListener(function (windowId) {
299+
if (previous_window === windowId) {
300+
previous_window = env.windows.WINDOW_ID_NONE;
301+
}
302+
});
303+
294304
// Message listener for messages from tabs
295305
env.runtime.onMessage.addListener(
296306
async function (request, sender, sendResponse) {

src/video_auto_pause.js

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
if (window.autoPauseInjected !== true) {
22
const env = chrome.runtime ? chrome : browser;
33
window.autoPauseInjected = true;
4-
let manuallyPaused = false;
4+
const manuallyPausedVideos = new WeakMap();
55
let automaticallyPaused = false;
66

77
let options = {
@@ -55,7 +55,10 @@ if (window.autoPauseInjected !== true) {
5555
}
5656

5757
if (!options.manualPause) {
58-
manuallyPaused = false;
58+
const videoElements = document.getElementsByTagName("video");
59+
for (const element of videoElements) {
60+
manuallyPausedVideos.delete(element);
61+
}
5962
automaticallyPaused = true;
6063
}
6164

@@ -188,8 +191,8 @@ if (window.autoPauseInjected !== true) {
188191

189192
const iframeElements = document.getElementsByTagName("iframe");
190193

191-
for (let i = 0; i < iframeElements.length; i++) {
192-
const iframe = iframeElements[i];
194+
for (const element of iframeElements) {
195+
const iframe = element;
193196
try {
194197
if (request.action === "stop") {
195198
iframe.contentWindow.postMessage(
@@ -207,41 +210,44 @@ if (window.autoPauseInjected !== true) {
207210
}
208211
}
209212

210-
for (let i = 0; i < videoElements.length; i++) {
213+
for (const element of videoElements) {
211214
try {
212-
if (request.action === "stop" && !manuallyPaused) {
215+
const video = element;
216+
const isManuallyPaused = manuallyPausedVideos.get(video);
217+
218+
if (request.action === "stop" && !isManuallyPaused) {
213219
automaticallyPaused = true;
214-
videoElements[i].pause();
220+
video.pause();
215221
} else if (
216222
request.action === "resume" &&
217-
videoElements[i].paused &&
218-
!manuallyPaused
223+
video.paused &&
224+
!isManuallyPaused
219225
) {
220226
debugLog(
221-
`Attempting to resume video: paused=${videoElements[i].paused}, manuallyPaused=${manuallyPaused}`,
227+
`Attempting to resume video: paused=${video.paused}, manuallyPaused=${isManuallyPaused}`,
222228
);
223229
automaticallyPaused = false;
224-
if (!videoElements[i].ended) {
225-
await videoElements[i].play();
230+
if (!video.ended) {
231+
await video.play();
226232
}
227233
} else if (request.action === "resume") {
228234
debugLog(
229-
`Resume blocked: paused=${videoElements[i].paused}, manuallyPaused=${manuallyPaused}`,
235+
`Resume blocked: paused=${video.paused}, manuallyPaused=${isManuallyPaused}`,
230236
);
231237
} else if (request.action === "toggle_mute") {
232-
videoElements[i].muted = !videoElements[i].muted;
238+
video.muted = !video.muted;
233239
} else if (request.action === "mute") {
234-
videoElements[i].muted = true;
240+
video.muted = true;
235241
} else if (request.action === "unmute") {
236-
videoElements[i].muted = false;
242+
video.muted = false;
237243
} else if (request.action === "toggle") {
238-
if (videoElements[i].paused && !manuallyPaused) {
239-
if (!videoElements[i].ended) {
240-
await videoElements[i].play();
244+
if (video.paused && !isManuallyPaused) {
245+
if (!video.ended) {
246+
await video.play();
241247
}
242248
automaticallyPaused = false;
243-
} else if (!manuallyPaused) {
244-
videoElements[i].pause();
249+
} else if (!isManuallyPaused) {
250+
video.pause();
245251
automaticallyPaused = true;
246252
}
247253
}
@@ -268,15 +274,15 @@ if (window.autoPauseInjected !== true) {
268274
);
269275
if (!automaticallyPaused && options.manualPause) {
270276
debugLog(`Manually paused video`);
271-
manuallyPaused = true;
277+
manuallyPausedVideos.set(videoElement, true);
272278
automaticallyPaused = false;
273279
}
274280
});
275281

276282
videoElement.addEventListener("play", (_e) => {
277283
if (options.manualPause) {
278284
debugLog(`Manually resumed video`);
279-
manuallyPaused = false;
285+
manuallyPausedVideos.delete(videoElement);
280286
}
281287
automaticallyPaused = false;
282288
});
@@ -289,12 +295,14 @@ if (window.autoPauseInjected !== true) {
289295
if (!options.scrollpause) {
290296
return;
291297
}
292-
if (entries[0].isIntersecting === true) {
293-
debugLog(`Video not anymore in viewport`);
294-
sendMessage({ visible: true });
295-
} else {
296-
debugLog(`Video in viewport`);
297-
sendMessage({ visible: false });
298+
for (const entry of entries) {
299+
if (entry.isIntersecting === true) {
300+
debugLog(`Video in viewport`);
301+
sendMessage({ visible: true });
302+
} else {
303+
debugLog(`Video scrolled out of viewport`);
304+
sendMessage({ visible: false });
305+
}
298306
}
299307
},
300308
{ threshold: [0] },
@@ -305,8 +313,8 @@ if (window.autoPauseInjected !== true) {
305313
let videoElements = document.getElementsByTagName("video");
306314
sendMessage({ hasVideos: hasVideos() });
307315

308-
for (let i = 0; i < videoElements.length; i++) {
309-
attachVideoListeners(videoElements[i], intersection_observer);
316+
for (const element of videoElements) {
317+
attachVideoListeners(element, intersection_observer);
310318
}
311319
}
312320

@@ -329,8 +337,8 @@ if (window.autoPauseInjected !== true) {
329337
const videos = node.getElementsByTagName("video");
330338
if (videos.length > 0) {
331339
debugLog(`New video elements detected in added node`);
332-
for (let i = 0; i < videos.length; i++) {
333-
attachVideoListeners(videos[i], intersection_observer);
340+
for (const element of videos) {
341+
attachVideoListeners(element, intersection_observer);
334342
}
335343
sendMessage({ hasVideos: hasVideos() });
336344
}

0 commit comments

Comments
 (0)