From 9a5979e0f8708db82f9f5d4c1892f9d914e0d8c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Farr=C3=A9s?= <14farresa@gmail.com> Date: Fri, 12 Sep 2025 12:45:53 +0200 Subject: [PATCH] Adapt to Manifest V3 --- background.js | 58 ++++++++++++++++++++++++++++----------------------- manifest.json | 16 +++++++------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/background.js b/background.js index 632ddec..4b83374 100644 --- a/background.js +++ b/background.js @@ -2,42 +2,48 @@ let setState = state => { if (state === null) { - sessionStorage.removeItem('state'); - chrome.browserAction.setBadgeText({ text: '' }); + chrome.storage.session.remove('state'); + chrome.action.setBadgeText({ text: '' }); } else { - sessionStorage.setItem('state', JSON.stringify(state)); - chrome.browserAction.setBadgeText({ text: '?' }); + chrome.storage.session.set({ state: state }); + chrome.action.setBadgeText({ text: '?' }); } }; -let getState = () => JSON.parse(sessionStorage.getItem('state')); +let getState = callback => { + chrome.storage.session.get(['state'], result => { + callback(result.state); + }); +}; -chrome.browserAction.onClicked.addListener(currentTab => { - let state = getState(); - if (state) { - setState(null); - } else { - chrome.tabs.query({ currentWindow: true, highlighted: true }, - highlightedTabs => { - setState({ - tabs: highlightedTabs.map(tab => tab.id), - currentTab: currentTab.id, - window: currentTab.windowId +chrome.action.onClicked.addListener(currentTab => { + getState(state => { + if (state) { + setState(null); + } else { + chrome.tabs.query({ currentWindow: true, highlighted: true }, + highlightedTabs => { + setState({ + tabs: highlightedTabs.map(tab => tab.id), + currentTab: currentTab.id, + window: currentTab.windowId + }); }); - }); - } + } + }); }); chrome.windows.onFocusChanged.addListener(windowId => { if (windowId != chrome.windows.WINDOW_ID_NONE) { - let state = getState(); - if (state) { - if (windowId != state.window) { - setState(null); - chrome.tabs.move(state.tabs, { windowId: windowId, index: -1 }, () => { - chrome.tabs.update(state.currentTab, { active: true }); - }); + getState(state => { + if (state) { + if (windowId != state.window) { + setState(null); + chrome.tabs.move(state.tabs, { windowId: windowId, index: -1 }, () => { + chrome.tabs.update(state.currentTab, { active: true }); + }); + } } - } + }); } }, { windowTypes: ['normal'] }); diff --git a/manifest.json b/manifest.json index 5d7ab67..33a7695 100644 --- a/manifest.json +++ b/manifest.json @@ -2,15 +2,17 @@ "name": "Move tabs between windows", "version": "1.0", "description": "Click the extension's toolbar button, then switch to another Chrome window — selected tab(s) will move to that window.", - "manifest_version": 2, + "manifest_version": 3, "background": { - "persistent": true, - "scripts": [ - "background.js" - ] + "service_worker": "background.js" }, "offline_enabled": true, - "browser_action": { + "action": { "default_title": "Move tabs to another window" - } + }, + "permissions": [ + "tabs", + "windows", + "storage" + ] }