Skip to content

Commit 6b05b33

Browse files
committed
Added ability to turn extension on and off, added counter badge
1 parent cc22b10 commit 6b05b33

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
7+
[*.json]
8+
indent_size = 2
9+
10+
[*.md]
11+
trim_trailing_whitespace = false

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# "Prevent Duplicate Tabs" Chrome extension
22

3-
Chrome extension that detects when a duplicate tab is opened and activates already existing tab.
3+
Chrome extension that detects when a duplicate tab is opened and activates already existing tab. You can temporary turn it off by clicking extension's icon. Icon badge is showing the number of prevented duplicates.
44

55
## How to install
66

7-
I have not published this extension into Chrome Web Store, so your will need to install extension locally:
8-
97
1. Clone extension to your machine - e.g. `git clone https://github.com/Litee/prevent-duplicate-tabs-chrome-extension.git`
10-
1. Look at `background.js` and into `manifest.json`. It is always a good idea to check that code does not do anything suspicious and does not have too many permissions ;)
8+
1. Look into `background.js` and `manifest.json`. It is always a good idea to check that code does not do anything suspicious and does not have too many permissions ;)
119
1. Open chrome://extensions tab in your Chrome browser
1210
1. Activate developer mode (required for next step)
1311
1. Install extension as unpacked

background.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
let preventedDuplicatesCount = 0;
2+
let active = true;
3+
4+
chrome.browserAction.setBadgeBackgroundColor({
5+
color: '#933EC5'
6+
});
7+
8+
chrome.browserAction.onClicked.addListener(tab => {
9+
active = !active;
10+
updateBadge();
11+
});
12+
113
chrome.tabs.onCreated.addListener(newTab => {
2-
if (newTab.url) {
14+
if (active && newTab.url) {
315
verifyAndDeduplicate(newTab.id, newTab.url);
416
}
517
});
618

719
chrome.tabs.onUpdated.addListener((updatedTabId, updateInfo, updatedTab) => {
8-
if (updateInfo.url) {
20+
if (active && updateInfo.url) {
921
verifyAndDeduplicate(updatedTabId, updateInfo.url);
1022
}
1123
});
@@ -26,6 +38,14 @@ function verifyAndDeduplicate(currentTabId, currentTabUrl) {
2638
focused: true
2739
})
2840
chrome.tabs.remove(currentTabId);
41+
preventedDuplicatesCount++;
42+
updateBadge();
2943
}
3044
});
45+
}
46+
47+
function updateBadge() {
48+
chrome.browserAction.setBadgeText({
49+
text: active ? (preventedDuplicatesCount > 0 ? `${preventedDuplicatesCount}` : '') : 'OFF'
50+
});
3151
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Prevent Duplicate Tabs",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"manifest_version": 2,
55
"description": "Chrome extension that detects when a duplicate tab is opened and activates already existing tab",
66
"icons": {

0 commit comments

Comments
 (0)