Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for discarded and hidden tabs #5

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 80 additions & 19 deletions go-to-random-tab.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,94 @@
/* global chrome */
'use strict';

// Filtering options for tabs.query().
const tabFilter = {
// Avoid switching to the current tab.
active: false,
currentWindow: true
};
const DISCARDED = "discarded"
const HIDDEN = "hidden"
const STORAGE_KEY = "options"

const capitalize = ([firstChar, ...otherChars]) => `${firstChar.toUpperCase()}${(otherChars.join(''))}`

function goToRandomTab() {
chrome.tabs.query(tabFilter, function (tabs) {
// If we need at least one other tabs to switch anywhere.
if (tabs.length < 1) {
return;

//check options
chrome.storage.local.get([STORAGE_KEY], (storage) => {

// Filtering options for tabs.query().
let query = {
// Avoid switching to the current tab.
active: false,
currentWindow: true,
discarded: storage[STORAGE_KEY][DISCARDED],
hidden: storage[STORAGE_KEY][HIDDEN]
}

// Try switching tabs up to three times.
for (let i = 0; i < 3; i += 1) {
let newTabIndex = Math.round(Math.random() * (tabs.length - 1));
chrome.tabs.query(query, (tabs) => {
// If we need at least one other tabs to switch anywhere.
if (tabs.length < 1) {
return;
}

if (tabs[newTabIndex]) {
chrome.tabs.update(tabs[newTabIndex].id, { active: true });
break;
// Try switching tabs up to three times.
for (let i = 0; i < 3; i += 1) {
let newTabIndex = Math.floor(Math.random() * tabs.length);

if (tabs[newTabIndex]) {
chrome.tabs.update(tabs[newTabIndex].id, { active: true });
break;
}
}
}
});

})

})
}


chrome.browserAction.onClicked.addListener(function(tab) {
goToRandomTab();
});
})

chrome.commands.onCommand.addListener(function(command) {
if (command == "go-to-random-tab") {
goToRandomTab();
}
})

chrome.contextMenus.removeAll();

chrome.contextMenus.create({
id: DISCARDED,
title: capitalize(DISCARDED),
type: "checkbox",
contexts: ["browser_action"],
})

chrome.contextMenus.create({
id: HIDDEN,
title: capitalize(HIDDEN),
type: "checkbox",
contexts: ["browser_action"],
})

// initialize store
chrome.storage.local.clear()
chrome.storage.local.set({
options: {
discarded: false,
hidden: false
}
})


chrome.contextMenus.onClicked.addListener((info) => {
if(info.menuItemId === DISCARDED || info.menuItemId === HIDDEN) {
if(info.checked != info.wasChecked) {

chrome.storage.local.get([STORAGE_KEY], (storage) => {

storage[STORAGE_KEY][info.menuItemId] = info.checked
chrome.storage.local.set({options: storage[STORAGE_KEY]})

})
}
}
})
17 changes: 16 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"96": "icons/random-96.png"
},
"permissions": [
"tabs"
"tabs",
"contextMenus",
"storage"
],
"browser_action": {
"browser_style": true,
Expand All @@ -22,5 +24,18 @@
"background": {
"scripts": ["go-to-random-tab.js"],
"persistent": false
},
"commands": {
"go-to-random-tab": {
"suggested_key": {
"default": "Ctrl + Alt + R"
},
"description": "Sends go-to-random event to the extension"
}
},
"applications": {
"gecko": {
"id": "[email protected]"
}
}
}