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

Add setting to choose between all tabs and current window only #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 45 additions & 19 deletions go-to-random-tab.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
/* global chrome */
'use strict';

// Filtering options for tabs.query().
const tabFilter = {
// Avoid switching to the current tab.
active: false,
currentWindow: true,
};
const LIMIT_CURRENT_WINDOW = 'limit-to-current-window'

function goToRandomTab() {
chrome.tabs.query(tabFilter, function (tabs) {
// If we need at least one other tabs to switch anywhere.
if (tabs.length < 1) {
return;
}
chrome.storage.sync.get(LIMIT_CURRENT_WINDOW, data => {
chrome.tabs.query({ active: false, currentWindow: data[LIMIT_CURRENT_WINDOW] }, tabs => {
console.log(data, tabs.length)
artemisart marked this conversation as resolved.
Show resolved Hide resolved
// If we need at least one other tabs to switch anywhere.
if (tabs.length < 1) {
return;
}

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

chrome.browserAction.onClicked.addListener(function (tab) {
Expand All @@ -36,3 +35,30 @@ chrome.commands.onCommand.addListener(function (command) {
goToRandomTab();
}
});

function init() {
chrome.storage.sync.get(LIMIT_CURRENT_WINDOW, data => {
let currentWindow = data[LIMIT_CURRENT_WINDOW]
if (!(currentWindow == true || currentWindow == false)) { // init
chrome.storage.sync.set({ LIMIT_CURRENT_WINDOW: false })
currentWindow = false
}

chrome.contextMenus.create({
id: LIMIT_CURRENT_WINDOW,
title: "Limit to active window",
type: 'checkbox',
checked: currentWindow,
contexts: ['browser_action'],
})

chrome.contextMenus.onClicked.addListener(info => {
if (info.menuItemId == LIMIT_CURRENT_WINDOW) {
chrome.storage.sync.set({ [LIMIT_CURRENT_WINDOW]: info.checked })
chrome.storage.sync.get(null, console.log)
artemisart marked this conversation as resolved.
Show resolved Hide resolved
}
})
})
}

init()
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Go to random tab",
"version": "2.1.1",
"version": "2.2.0",
"description": "A simple button to go to a random open tab.",
"homepage_url": "https://github.com/mikl/browser-go-to-random-tab",
"icons": {
Expand All @@ -13,7 +13,7 @@
"id": "jid1-3vuPf16zAtrlFA@jetpack"
}
},
"permissions": ["tabs"],
"permissions": ["tabs", "contextMenus", "storage"],
"browser_action": {
"browser_style": true,
"default_area": "tabstrip",
Expand Down