Skip to content

Commit

Permalink
feat: add options for closing and reloading tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed May 16, 2019
1 parent ca31096 commit 62b780d
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 11 deletions.
67 changes: 66 additions & 1 deletion src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,71 @@
"description": "Title of the option."
},

"optionSectionTitle_tabs": {
"message": "Tabs",
"description": "Title of the option section."
},

"optionTitle_closeTabs": {
"message": "Close tabs",
"description": "Title of the option."
},

"optionValue_closeTabs_all": {
"message": "All tabs",
"description": "Value of the option."
},

"optionValue_closeTabs_active": {
"message": "Active tab",
"description": "Value of the option."
},

"optionValue_closeTabs_allButActive": {
"message": "All tabs except active",
"description": "Value of the option."
},

"optionValue_closeTabs_exit": {
"message": "All tabs and exit",
"description": "Value of the option."
},

"optionValue_closeTabs_false": {
"message": "Disable",
"description": "Value of the option."
},

"optionTitle_closePinnedTabs": {
"message": "Close pinned tabs",
"description": "Title of the option."
},

"optionTitle_reloadTabs": {
"message": "Reload tabs",
"description": "Title of the option."
},

"optionValue_reloadTabs_all": {
"message": "All tabs",
"description": "Value of the option."
},

"optionValue_reloadTabs_active": {
"message": "Active tab",
"description": "Value of the option."
},

"optionValue_reloadTabs_allButActive": {
"message": "All tabs except active",
"description": "Value of the option."
},

"optionValue_reloadTabs_false": {
"message": "Disable",
"description": "Value of the option."
},

"optionSectionTitle_misc": {
"message": "Miscellaneous",
"description": "Title of the option section."
Expand Down Expand Up @@ -355,7 +420,7 @@
},

"error_internalError": {
"message": "Something went wrong.",
"message": "Something went wrong. Open the browser console for more details.",
"description": "Error message."
}
}
117 changes: 109 additions & 8 deletions src/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import browser from 'webextension-polyfill';

import {initStorage} from 'storage/init';
import storage from 'storage/storage';
import {getText} from 'utils/common';
import {getText, getActiveTab} from 'utils/common';
import {
getEnabledDataTypes,
showNotification,
Expand All @@ -11,14 +11,18 @@ import {
import {optionKeys} from 'utils/data';
import {targetEnv} from 'utils/config';

async function clearDataType(dataType, options, enDataTypes = null) {
async function clearDataType(dataType, options = null, enDataTypes = null) {
let {useCount} = await storage.get('useCount', 'sync');
useCount += 1;
await storage.set({useCount}, 'sync');
if ([10, 50].includes(useCount)) {
await showContributePage('clear');
}

if (!options) {
options = await storage.get(optionKeys, 'sync');
}

let since;
if (options.clearSince === 'epoch') {
since = 0;
Expand Down Expand Up @@ -62,6 +66,75 @@ async function clearDataType(dataType, options, enDataTypes = null) {
dataTypes[dataType] = true;
}

let tempTabId;
const {id: activeTabId} = await getActiveTab();

if (options.closeTabs !== 'false') {
if (['all', 'allButActive', 'exit'].includes(options.closeTabs)) {
const windows = await browser.windows.getAll({populate: true});
for (const window of windows) {
if (!window.focused) {
const tabIds = window.tabs.reduce((results, tab) => {
if (!tab.pinned || options.closePinnedTabs) {
results.push(tab.id);
}
return results;
}, []);
await browser.tabs.remove(tabIds);
}
}
}

const activeWindow = await browser.windows.getLastFocused({populate: true});

let pinnedTabIds = [];
if (!options.closePinnedTabs) {
pinnedTabIds = activeWindow.tabs.reduce((results, tab) => {
if (tab.pinned) {
results.push(tab.id);
}
return results;
}, []);
}

if (options.closeTabs === 'all') {
if (!pinnedTabIds.length) {
({id: tempTabId} = await browser.tabs.create({active: false}));
}
const tabIds = activeWindow.tabs.reduce((results, tab) => {
if (!pinnedTabIds.includes(tab.id)) {
results.push(tab.id);
}
return results;
}, []);

await browser.tabs.remove(tabIds);
} else if (options.closeTabs === 'active') {
if (!pinnedTabIds.length && activeWindow.tabs.length === 1) {
({id: tempTabId} = await browser.tabs.create({active: false}));
}

if (!pinnedTabIds.includes(activeTabId)) {
await browser.tabs.remove(activeTabId);
}
} else if (options.closeTabs === 'allButActive') {
const tabIds = activeWindow.tabs.reduce((results, tab) => {
if (!pinnedTabIds.includes(tab.id) && tab.id !== activeTabId) {
results.push(tab.id);
}
return results;
}, []);

await browser.tabs.remove(tabIds);
} else if (options.closeTabs === 'exit') {
({id: tempTabId} = await browser.tabs.create({
url: 'about:blank',
active: false
}));
await browser.tabs.remove(activeWindow.tabs.map(tab => tab.id));
}
}

try {
if (dataTypes.localStorage && since && targetEnv === 'firefox') {
await browser.browsingData.removeLocalStorage({});
Expand All @@ -71,14 +144,46 @@ async function clearDataType(dataType, options, enDataTypes = null) {
await browser.browsingData.remove({since}, dataTypes);
}
} catch (err) {
console.log(e);
await showNotification('error_dataTypeNotCleared');
throw err;
}

if (options.closeTabs === 'exit') {
browser.tabs.remove(tempTabId);
return;
}

if (options.notifyOnSuccess) {
await showNotification('info_dataTypeCleared');
}

if (options.reloadTabs !== 'false') {
if (options.reloadTabs === 'all') {
const reloadingTabs = [];
const tabs = await browser.tabs.query({});
for (const tab of tabs) {
if (tab.id !== tempTabId) {
reloadingTabs.push(browser.tabs.reload(tab.id, {bypassCache: true}));
}
}

await Promise.all(reloadingTabs);
} else if (options.reloadTabs === 'active') {
if (['allButActive', 'false'].includes(options.closeTabs)) {
await browser.tabs.reload(activeTabId, {bypassCache: true});
}
} else if (options.reloadTabs === 'allButActive') {
const reloadingTabs = [];
const tabs = await browser.tabs.query({});
for (const tab of tabs) {
if (![activeTabId, tempTabId].includes(tab.id)) {
reloadingTabs.push(browser.tabs.reload(tab.id, {bypassCache: true}));
}
}

await Promise.all(reloadingTabs);
}
}
}

async function onActionClick() {
Expand All @@ -101,11 +206,7 @@ async function onActionClick() {
}

async function onActionPopupClick(dataType) {
const options = await storage.get(
['dataTypes', 'disabledDataTypes', 'clearSince', 'notifyOnSuccess'],
'sync'
);
await clearDataType(dataType, options);
await clearDataType(dataType);
}

function onMessage(request, sender, sendResponse) {
Expand Down
35 changes: 35 additions & 0 deletions src/options/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,36 @@
</v-draggable>
</div>

<div class="section">
<div class="section-title" v-once>
{{ getText('optionSectionTitle_tabs') }}
</div>
<div class="option-wrap">
<div class="option select">
<v-select :label="getText('optionTitle_closeTabs')"
v-model="options.closeTabs"
:options="selectOptions.closeTabs">
</v-select>
</div>
<div class="option"
v-show="!['exit', 'false'].includes(options.closeTabs)">
<v-form-field input-id="cpt"
:label="getText('optionTitle_closePinnedTabs')">
<v-switch id="cpt"
v-model="options.closePinnedTabs">
</v-switch>
</v-form-field>
</div>
<div class="option select"
v-show="options.closeTabs !== 'exit'">
<v-select :label="getText('optionTitle_reloadTabs')"
v-model="options.reloadTabs"
:options="selectOptions.reloadTabs">
</v-select>
</div>
</div>
</div>

<div class="section">
<div class="section-title" v-once>
{{ getText('optionSectionTitle_misc') }}
Expand Down Expand Up @@ -75,6 +105,8 @@ export default {
dataLoaded: false,

selectOptions: getOptionLabels({
closeTabs: ['all', 'active', 'allButActive', 'exit', 'false'],
reloadTabs: ['all', 'active', 'allButActive', 'false'],
clearAllDataTypesAction: ['main', 'sub', 'false'],
clearSince: [
'1hour',
Expand All @@ -93,6 +125,9 @@ export default {
disabledDataTypes: [],
clearAllDataTypesAction: '',
clearSince: '',
closeTabs: '',
closePinnedTabs: false,
reloadTabs: '',
notifyOnSuccess: false
}
};
Expand Down
29 changes: 29 additions & 0 deletions src/storage/versions/local/kuZ4zBZDX.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import browser from 'webextension-polyfill';

const message = 'Add closeTabs, closePinnedTabs and reloadTabs options';

const revision = 'kuZ4zBZDX';
const downRevision = 'ByCtvupNz';

const storage = browser.storage.local;

async function upgrade() {
const changes = {
closeTabs: 'false', // 'all', 'active', 'allButActive', 'exit', 'false'
closePinnedTabs: true,
reloadTabs: 'false' // 'all', 'active', 'allButActive', 'false'
};

changes.storageVersion = revision;
return storage.set(changes);
}

async function downgrade() {
const changes = {};
await storage.remove(['closeTabs', 'closePinnedTabs', 'reloadTabs']);

changes.storageVersion = downRevision;
return storage.set(changes);
}

export {message, revision, upgrade, downgrade};
2 changes: 1 addition & 1 deletion src/storage/versions/local/versions.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"versions": ["xO0Hh1Vm2", "BkFsUtt7f", "ByCtvupNz"]}
{"versions": ["xO0Hh1Vm2", "BkFsUtt7f", "ByCtvupNz", "kuZ4zBZDX"]}
29 changes: 29 additions & 0 deletions src/storage/versions/sync/kuZ4zBZDX.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import browser from 'webextension-polyfill';

const message = 'Add closeTabs, closePinnedTabs and reloadTabs options';

const revision = 'kuZ4zBZDX';
const downRevision = 'ByCtvupNz';

const storage = browser.storage.sync;

async function upgrade() {
const changes = {
closeTabs: 'false', // 'all', 'active', 'allButActive', 'exit', 'false'
closePinnedTabs: true,
reloadTabs: 'false' // 'all', 'active', 'allButActive', 'false'
};

changes.storageVersion = revision;
return storage.set(changes);
}

async function downgrade() {
const changes = {};
await storage.remove(['closeTabs', 'closePinnedTabs', 'reloadTabs']);

changes.storageVersion = downRevision;
return storage.set(changes);
}

export {message, revision, upgrade, downgrade};
2 changes: 1 addition & 1 deletion src/storage/versions/sync/versions.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"versions": ["xO0Hh1Vm2", "BkFsUtt7f", "ByCtvupNz"]}
{"versions": ["xO0Hh1Vm2", "BkFsUtt7f", "ByCtvupNz", "kuZ4zBZDX"]}
3 changes: 3 additions & 0 deletions src/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const optionKeys = [
'disabledDataTypes',
'clearAllDataTypesAction',
'clearSince',
'closeTabs',
'closePinnedTabs',
'reloadTabs',
'notifyOnSuccess'
];

Expand Down

0 comments on commit 62b780d

Please sign in to comment.