Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 32 additions & 26 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'] });
16 changes: 9 additions & 7 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}