Skip to content

Commit

Permalink
add options
Browse files Browse the repository at this point in the history
  • Loading branch information
ipanasenko committed Mar 3, 2023
1 parent 7f58ab9 commit b03299a
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .idea/prettier.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion config/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const PATHS = require('./paths');

// used in the module rules and in the stats exlude list
// used in the module rules and in the stats exclude list
const IMAGE_TYPES = /\.(png|jpe?g|gif|svg)$/i;

// To re-use webpack configuration across templates,
Expand All @@ -28,6 +28,10 @@ const common = {
},
module: {
rules: [
{
test: /\.ts?$/,
use: 'ts-loader',
},
// Help webpack in understanding CSS files imported in .js files
{
test: /\.css$/,
Expand Down
1 change: 1 addition & 0 deletions config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const config = (env, argv) =>
entry: {
// popup: PATHS.src + '/popup.ts',
// contentScript: PATHS.src + '/contentScript.ts',
options: PATHS.src + '/options.ts',
background: PATHS.src + '/background.ts',
},
devtool: argv.mode === 'production' ? false : 'source-map',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "random-tabber",
"version": "0.1.0",
"version": "1.0.0",
"description": "My Chrome Extension",
"private": true,
"scripts": {
Expand All @@ -15,6 +15,7 @@
"file-loader": "^6.2.0",
"mini-css-extract-plugin": "^2.7.2",
"prettier": "^2.8.4",
"ts-loader": "^9.4.2",
"typescript": "^4.9.5",
"webpack": "^5.75.0",
"webpack-cli": "^4.10.0",
Expand Down
6 changes: 5 additions & 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": "0.1.0",
"version": "1.0.0",
"description": "Jump to a random Chrome/Edge tab",
"icons": {
"16": "icons/icon-16.png",
Expand All @@ -14,5 +14,9 @@
"action": {
"default_title": "Random Tabber"
},
"options_ui": {
"page": "options.html",
"open_in_tab": false
},
"permissions": ["storage"]
}
24 changes: 24 additions & 0 deletions public/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<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
>

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

<script src="options.js"></script>
</body>
</html>
24 changes: 23 additions & 1 deletion src/background.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
interface Settings {
'switch-in': 'current' | 'all';
'close-current': boolean;
}

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

export const getSettings = async (): Promise<Settings> =>
(await chrome.storage.sync.get(defaultSettings)) as Settings;

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

const allTabs = await chrome.tabs.query({ windowId });
const allOtherTabs = allTabs.filter((tab) => tab.id !== id);
const randomTab =
allOtherTabs[Math.floor(Math.random() * allOtherTabs.length)];

await chrome.tabs.highlight({ tabs: randomTab.index });
await chrome.tabs.highlight({
windowId: randomTab.windowId,
tabs: randomTab.index,
});

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

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

export const getSettings = async (): Promise<Settings> =>
(await chrome.storage.sync.get(defaultSettings)) as Settings;

async function saveOptions() {
(document.getElementById('save') as HTMLButtonElement).innerText =
'Saving...';

const switchIn = (document.getElementById('switch-in') as HTMLSelectElement)
.value;
const closeCurrent = (
document.getElementById('close-current') as HTMLInputElement
).checked;

await chrome.storage.sync.set({
'switch-in': switchIn,
'close-current': closeCurrent,
});

(document.getElementById('save') as HTMLButtonElement).innerText = 'Saved!';
}

async function restoreOptions() {
const settings = await getSettings();

(document.getElementById('switch-in') as HTMLSelectElement).value =
settings['switch-in'];
(document.getElementById('close-current') as HTMLInputElement).checked =
settings['close-current'];
}

document.addEventListener('DOMContentLoaded', restoreOptions);
document.getElementById('save').addEventListener('click', saveOptions);
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"compilerOptions": {
"module": "ES2022",
"moduleResolution": "node",
"target": "ESNext",
"resolveJsonModule": true,
"sourceMap": true
},
"exclude": [
"node_modules"
]
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
66 changes: 63 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,15 @@ __metadata:
languageName: node
linkType: hard

"ansi-styles@npm:^4.1.0":
version: 4.3.0
resolution: "ansi-styles@npm:4.3.0"
dependencies:
color-convert: ^2.0.1
checksum: 513b44c3b2105dd14cc42a19271e80f386466c4be574bccf60b627432f9198571ebf4ab1e4c3ba17347658f4ee1711c163d574248c0c1cdc2d5917a0ad582ec4
languageName: node
linkType: hard

"array-union@npm:^3.0.1":
version: 3.0.1
resolution: "array-union@npm:3.0.1"
Expand Down Expand Up @@ -497,6 +506,16 @@ __metadata:
languageName: node
linkType: hard

"chalk@npm:^4.1.0":
version: 4.1.2
resolution: "chalk@npm:4.1.2"
dependencies:
ansi-styles: ^4.1.0
supports-color: ^7.1.0
checksum: fe75c9d5c76a7a98d45495b91b2172fa3b7a09e0cc9370e5c8feb1c567b85c4288e2b3fded7cfdd7359ac28d6b3844feb8b82b8686842e93d23c827c417e83fc
languageName: node
linkType: hard

"chrome-trace-event@npm:^1.0.2":
version: 1.0.3
resolution: "chrome-trace-event@npm:1.0.3"
Expand All @@ -515,6 +534,22 @@ __metadata:
languageName: node
linkType: hard

"color-convert@npm:^2.0.1":
version: 2.0.1
resolution: "color-convert@npm:2.0.1"
dependencies:
color-name: ~1.1.4
checksum: 79e6bdb9fd479a205c71d89574fccfb22bd9053bd98c6c4d870d65c132e5e904e6034978e55b43d69fcaa7433af2016ee203ce76eeba9cfa554b373e7f7db336
languageName: node
linkType: hard

"color-name@npm:~1.1.4":
version: 1.1.4
resolution: "color-name@npm:1.1.4"
checksum: b0445859521eb4021cd0fb0cc1a75cecf67fceecae89b63f62b201cca8d345baf8b952c966862a9d9a2632987d4f6581f0ec8d957dfacece86f0a7919316f610
languageName: node
linkType: hard

"colorette@npm:^2.0.14":
version: 2.0.19
resolution: "colorette@npm:2.0.19"
Expand Down Expand Up @@ -613,7 +648,7 @@ __metadata:
languageName: node
linkType: hard

"enhanced-resolve@npm:^5.10.0":
"enhanced-resolve@npm:^5.0.0, enhanced-resolve@npm:^5.10.0":
version: 5.12.0
resolution: "enhanced-resolve@npm:5.12.0"
dependencies:
Expand Down Expand Up @@ -1017,7 +1052,7 @@ __metadata:
languageName: node
linkType: hard

"micromatch@npm:^4.0.4":
"micromatch@npm:^4.0.0, micromatch@npm:^4.0.4":
version: 4.0.5
resolution: "micromatch@npm:4.0.5"
dependencies:
Expand Down Expand Up @@ -1265,6 +1300,7 @@ __metadata:
file-loader: ^6.2.0
mini-css-extract-plugin: ^2.7.2
prettier: ^2.8.4
ts-loader: ^9.4.2
typescript: ^4.9.5
webpack: ^5.75.0
webpack-cli: ^4.10.0
Expand Down Expand Up @@ -1385,7 +1421,7 @@ __metadata:
languageName: node
linkType: hard

"semver@npm:^7.3.8":
"semver@npm:^7.3.4, semver@npm:^7.3.8":
version: 7.3.8
resolution: "semver@npm:7.3.8"
dependencies:
Expand Down Expand Up @@ -1461,6 +1497,15 @@ __metadata:
languageName: node
linkType: hard

"supports-color@npm:^7.1.0":
version: 7.2.0
resolution: "supports-color@npm:7.2.0"
dependencies:
has-flag: ^4.0.0
checksum: 3dda818de06ebbe5b9653e07842d9479f3555ebc77e9a0280caf5a14fb877ffee9ed57007c3b78f5a6324b8dbeec648d9e97a24e2ed9fdb81ddc69ea07100f4a
languageName: node
linkType: hard

"supports-color@npm:^8.0.0":
version: 8.1.1
resolution: "supports-color@npm:8.1.1"
Expand Down Expand Up @@ -1529,6 +1574,21 @@ __metadata:
languageName: node
linkType: hard

"ts-loader@npm:^9.4.2":
version: 9.4.2
resolution: "ts-loader@npm:9.4.2"
dependencies:
chalk: ^4.1.0
enhanced-resolve: ^5.0.0
micromatch: ^4.0.0
semver: ^7.3.4
peerDependencies:
typescript: "*"
webpack: ^5.0.0
checksum: 6f306ee4c615c2a159fb177561e3fb86ca2cbd6c641e710d408a64b4978e1ff3f2c9733df07bff27d3f82efbfa7c287523d4306049510c7485ac2669a9c37eb0
languageName: node
linkType: hard

"typescript@npm:^4.9.5":
version: 4.9.5
resolution: "typescript@npm:4.9.5"
Expand Down

0 comments on commit b03299a

Please sign in to comment.