Skip to content

Commit

Permalink
add ability to choose current/all windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ipanasenko committed Mar 6, 2023
1 parent 125f349 commit 8a70f18
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "random-tabber",
"version": "1.1.0",
"version": "1.2.0",
"description": "My Chrome Extension",
"private": true,
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Random Tabber",
"version": "1.1.0",
"version": "1.2.0",
"description": "Jump to a random Chrome/Edge tab",
"icons": {
"16": "icons/icon-16.png",
Expand Down
29 changes: 16 additions & 13 deletions public/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
<title>Random Tabber Options</title>
</head>
<body>
<label>
COMING SOON! Switch to tab in:
<select id="switch-in" disabled>
<option value="current">Current window</option>
<option value="all">All windows</option>
</select>
</label>
<br />
<label>
<input type="checkbox" id="close-current" /> Close current tab</label
>
<div id="loading">Loading settings...</div>
<div id="loaded" style="display: none">
<label>
Switch to tab in:
<select id="switch-in">
<option value="current">Current window</option>
<option value="all">All windows</option>
</select>
</label>
<br />
<label>
<input type="checkbox" id="close-current" /> Close current tab</label
>

<br /><br />
<button id="save">Save</button>
<br /><br />
<button id="save">Save</button>
</div>

<script src="options.js"></script>
</body>
Expand Down
15 changes: 12 additions & 3 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { getSettings } from './options';
import { getSettings } from './common';

chrome.action.onClicked.addListener(async ({ windowId, id }) => {
const settings = await getSettings();

const allTabs = await chrome.tabs.query({ windowId });
const shouldCloseCurrentTab = settings['close-current'];
const shouldSearchInCurrentWindowOnly = settings['switch-in'] === 'current';

const allTabs = await chrome.tabs.query({
windowId: shouldSearchInCurrentWindowOnly ? windowId : undefined,
});
const allOtherTabs = allTabs.filter((tab) => tab.id !== id);
const randomTab =
allOtherTabs[Math.floor(Math.random() * allOtherTabs.length)];
Expand All @@ -13,7 +18,11 @@ chrome.action.onClicked.addListener(async ({ windowId, id }) => {
tabs: randomTab.index,
});

if (settings['close-current']) {
if (!shouldSearchInCurrentWindowOnly) {
await chrome.windows.update(randomTab.windowId, { focused: true });
}

if (shouldCloseCurrentTab) {
await chrome.tabs.remove(id);
}
});
11 changes: 11 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
interface Settings {
'switch-in': 'current' | 'all';
'close-current': boolean;
}

const defaultSettings: Settings = {
'switch-in': 'current',
'close-current': false,
};
export const getSettings = async (): Promise<Settings> =>
(await chrome.storage.sync.get(defaultSettings)) as Settings;
16 changes: 4 additions & 12 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
interface Settings {
'switch-in': 'current' | 'all';
'close-current': boolean;
}

const defaultSettings: Settings = {
'switch-in': 'current',
'close-current': false,
};

export const getSettings = async (): Promise<Settings> =>
(await chrome.storage.sync.get(defaultSettings)) as Settings;
import { getSettings } from './common';

async function saveOptions() {
(document.getElementById('save') as HTMLButtonElement).innerText =
Expand All @@ -36,6 +25,9 @@ async function restoreOptions() {
settings['switch-in'];
(document.getElementById('close-current') as HTMLInputElement).checked =
settings['close-current'];

(document.getElementById('loaded') as HTMLDivElement).style.display = 'block';
(document.getElementById('loading') as HTMLDivElement).style.display = 'none';
}

document.addEventListener('DOMContentLoaded', restoreOptions);
Expand Down

0 comments on commit 8a70f18

Please sign in to comment.