Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
feat: make window focused
Browse files Browse the repository at this point in the history
- use tabs.query instead of global tabId
- focus the window of view-tab page when clicking the action btn in anther window
  • Loading branch information
GeekaholicLin committed Aug 2, 2019
1 parent 8d2143d commit 85f46e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
1 change: 0 additions & 1 deletion global.d.ts
@@ -1,5 +1,4 @@
interface Window {
tabId?: number;
timeoutId?: number;
REMU_SYNC_DELAY?: number;
REMU_TOKEN?: string;
Expand Down
25 changes: 8 additions & 17 deletions src/background/index.ts
Expand Up @@ -27,30 +27,21 @@ if (process.env.NODE_ENV !== 'development') {
dsn: 'https://238e73db89cb46929d35b7f1b7c6b181@sentry.io/1510135',
});
}
// record tab id
window.tabId = null;

window.REMU_GIST_ID = '';
window.REMU_TOKEN = '';
window.REMU_GIST_UPDATE_AT = '';

chrome.browserAction.onClicked.addListener(function() {
const index = chrome.extension.getURL('view-tab.html');

if (window.tabId) {
chrome.tabs.update(window.tabId, { selected: true });
} else {
chrome.tabs.create({ url: index }, function(tab) {
window.tabId = tab.id;
});
}
});

// remove tab
chrome.tabs.onRemoved.addListener(function(tabId) {
if (tabId === window.tabId) {
window.tabId = null;
}
chrome.tabs.query({ url: index }, function(tabs) {
if (tabs.length) {
chrome.tabs.update(tabs[0].id, { active: true });
chrome.windows.update(tabs[0].windowId, { focused: true });
} else {
chrome.tabs.create({ url: index });
}
});
});

chrome.storage.onChanged.addListener(function(changes, areaName) {
Expand Down

0 comments on commit 85f46e7

Please sign in to comment.