Skip to content

Commit 48e7b18

Browse files
committed
Fix for issue #33: Change the name of the space from the quick-switch-space mode of popup
1 parent ae1b7bf commit 48e7b18

File tree

4 files changed

+145
-5
lines changed

4 files changed

+145
-5
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [1.1.9] - 2025-12-??
6+
7+
### Changes
8+
9+
- Fixed [issue #33](https://github.com/codedread/spaces/issues/33): Allow setting the Active space name from quick-switch mode.
10+
- Increased unit test coverage from 23.62% to 23.91%.
11+
512
## [1.1.8] - 2025-12-03
613

714
### Changes

js/popup.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const NO_HOTKEY = 'no hotkey set';
1414
* @param {string} action The popup action ('switch' or 'move')
1515
*/
1616
export async function handlePopupMenuClick(action) {
17-
const params = await chrome.runtime.sendMessage({'action': 'generatePopupParams', 'popupAction': action});
17+
const params = await chrome.runtime.sendMessage({ 'action': 'generatePopupParams', 'popupAction': action });
1818
if (!params) return;
1919
window.location.hash = params;
2020
window.location.reload();
@@ -32,14 +32,44 @@ export function setGlobalCurrentSpace(space) {
3232
globalCurrentSpace = space;
3333
}
3434

35+
/**
36+
* Determines the window ID to use based on URL hash and current window.
37+
* This is the core logic for the bug fix that ensures correct window ID selection.
38+
* @param {string} urlString - The full URL string including hash
39+
* @param {number|null} currentWindowId - The ID of the current window, or null if not available yet
40+
* @returns {number|false} The window ID to use, or false if invalid
41+
*/
42+
export function getWindowIdFromContext(urlString, currentWindowId) {
43+
// First check if windowId is in the URL hash (e.g., when opened in quick-switch mode)
44+
// This ensures we use the original browser window, not the popup window itself
45+
const windowIdFromHash = getHashVariable('windowId', urlString);
46+
let windowId;
47+
48+
if (windowIdFromHash && windowIdFromHash !== 'false') {
49+
windowId = parseInt(windowIdFromHash, 10);
50+
} else if (currentWindowId !== null) {
51+
// Fallback to current window if not in hash (e.g., when opened from extension icon)
52+
windowId = currentWindowId;
53+
} else {
54+
// No window ID available
55+
return false;
56+
}
57+
58+
// Validate the window ID (must be a positive integer)
59+
return !isNaN(windowId) && windowId > 0 ? windowId : false;
60+
}
61+
3562
/** Initialize the popup window. */
36-
function initializePopup() {
63+
export function initializePopup() {
3764
document.addEventListener('DOMContentLoaded', async () => {
3865
const url = getHashVariable('url', window.location.href);
3966
globalUrl = url !== '' ? decodeURIComponent(url) : false;
67+
68+
// Get the current window ID if needed (for fallback when no hash parameter exists)
4069
const currentWindow = await chrome.windows.getCurrent({ populate: true });
41-
const windowId = currentWindow.id;
42-
globalWindowId = windowId !== '' ? windowId : false;
70+
71+
// Determine which window ID to use (from hash or current window)
72+
globalWindowId = getWindowIdFromContext(window.location.href, currentWindow.id);
4373
globalTabId = getHashVariable('tabId', window.location.href);
4474
const sessionName = getHashVariable(
4575
'sessionName',

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Spaces",
33
"description": "Intuitive tab management",
4-
"version": "1.1.8",
4+
"version": "1.1.9.0",
55
"permissions": [
66
"contextMenus",
77
"downloads",
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Unit tests for getWindowIdFromContext function in popup.js
3+
* Tests the window ID bug fix that ensures the correct window ID is selected
4+
* based on URL hash parameters (quick-switch mode) vs current window (extension icon).
5+
*/
6+
7+
import { getWindowIdFromContext } from '../js/popup.js';
8+
9+
describe('getWindowIdFromContext', () => {
10+
describe('Window ID from URL hash (quick-switch mode)', () => {
11+
it('should use windowId from hash when present and valid', () => {
12+
const urlString = 'popup.html#windowId=456&action=switch';
13+
const currentWindowId = 100;
14+
15+
const result = getWindowIdFromContext(urlString, currentWindowId);
16+
17+
expect(result).toBe(456);
18+
});
19+
20+
it('should handle parameters in different order', () => {
21+
const urlString = 'popup.html#action=switch&tabId=10&windowId=777&sessionName=test';
22+
const currentWindowId = 100;
23+
24+
const result = getWindowIdFromContext(urlString, currentWindowId);
25+
26+
expect(result).toBe(777);
27+
});
28+
});
29+
30+
describe('Fallback to current window', () => {
31+
it('should use current window when windowId is not in hash', () => {
32+
const urlString = 'popup.html#action=switch';
33+
const currentWindowId = 100;
34+
35+
const result = getWindowIdFromContext(urlString, currentWindowId);
36+
37+
expect(result).toBe(100);
38+
});
39+
40+
it('should use current window when windowId is "false"', () => {
41+
const urlString = 'popup.html#windowId=false';
42+
const currentWindowId = 100;
43+
44+
const result = getWindowIdFromContext(urlString, currentWindowId);
45+
46+
expect(result).toBe(100);
47+
});
48+
49+
it('should use current window when no hash present', () => {
50+
const urlString = 'popup.html';
51+
const currentWindowId = 100;
52+
53+
const result = getWindowIdFromContext(urlString, currentWindowId);
54+
55+
expect(result).toBe(100);
56+
});
57+
});
58+
59+
describe('Invalid window ID values', () => {
60+
it('should return false when windowId is NaN', () => {
61+
const urlString = 'popup.html#windowId=invalid';
62+
const currentWindowId = 100;
63+
64+
const result = getWindowIdFromContext(urlString, currentWindowId);
65+
66+
expect(result).toBe(false);
67+
});
68+
69+
it('should return false when windowId is zero or negative', () => {
70+
expect(getWindowIdFromContext('popup.html#windowId=0', 100)).toBe(false);
71+
expect(getWindowIdFromContext('popup.html#windowId=-5', 100)).toBe(false);
72+
});
73+
74+
it('should return false when current window ID is invalid', () => {
75+
expect(getWindowIdFromContext('popup.html', 0)).toBe(false);
76+
expect(getWindowIdFromContext('popup.html', -1)).toBe(false);
77+
});
78+
79+
it('should return false when currentWindowId is null and no hash value', () => {
80+
const result = getWindowIdFromContext('popup.html', null);
81+
expect(result).toBe(false);
82+
});
83+
});
84+
85+
describe('Bug fix verification', () => {
86+
it('BUG FIX: should prioritize hash windowId over currentWindowId in quick-switch mode', () => {
87+
// This is the core bug that was fixed
88+
// The popup window itself has ID 100, but we want to use the original window ID 456
89+
const urlString = 'popup.html#windowId=456&action=switch';
90+
const currentWindowId = 100; // This would be the popup window's ID
91+
92+
const result = getWindowIdFromContext(urlString, currentWindowId);
93+
94+
// MUST use 456 from hash, NOT 100 from currentWindowId
95+
expect(result).toBe(456);
96+
});
97+
98+
it('should use currentWindowId when opened from extension icon (no hash)', () => {
99+
const result = getWindowIdFromContext('popup.html', 200);
100+
expect(result).toBe(200);
101+
});
102+
});
103+
});

0 commit comments

Comments
 (0)