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

Refactoring, support pinning multiple tabs #10

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 5 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
"manifest_version": 2,
"name": "Tab Pinner (Keyboard Shortcuts)",
"description": "Pin or Unpin a tab easily from the keyboard.",
"version": "1.2"
"version": "1.2",
"background": {
"persistent": false,
"scripts": [ "tab_pinner.js" ]
"scripts": [
"tab_pinner.js"
]
},
"commands": {
"tab_pinner_pin_all_tabs": {
Expand All @@ -29,5 +31,5 @@
"16": "pin_16.png",
"48": "pin_48.png"
},
"permissions": [],
"permissions": []
}
95 changes: 69 additions & 26 deletions tab_pinner.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,75 @@
// Copyright (c) 2015 Brandon Buck, All Rights Reserved

var PIN_UNPIN_TAB = "tab_pinner_pin_unpin_tab",
UNPIN_ALL = "tab_pinner_unpin_all_tabs",
PIN_ALL = "tab_pinner_pin_all_tabs";

var forEachTab = function(fn) {
chrome.windows.getCurrent({populate: true}, function(curWindow) {
curWindow.tabs.forEach(fn);
const PIN_UNPIN_TAB = "tab_pinner_pin_unpin_tab",
UNPIN_ALL = "tab_pinner_unpin_all_tabs",
PIN_ALL = "tab_pinner_pin_all_tabs";

/**
* Sets all given tabs to the given pinned state.
*
* @param {Array} tabs The tabs to set the pinned state of.
* @param {boolean} pinned The pinned state to set the tabs to.
*/
function setTabsPinnedState(tabs, pinned) {
if (!pinned) {
// Reverse the order so that the first tab is the last tab to be unpinned.
// This keeps the tab order the same when unpinning.
tabs.reverse();
}
tabs.forEach((tab) => chrome.tabs.update(tab.id, { pinned: pinned }));
}

/**
* Toggles the pinned state of the given tabs. If any of the tabs are unpinned, pin them. Otherwise, unpin them.
*
* @param {Array} tabs The tabs to toggle the pinned state of.
*/
function togglePins(tabs) {
// If any tab is unpinned, pin all tabs
const hasUnpinnedTabs = tabs.some((tab) => !tab.pinned);
setTabsPinnedState(tabs, hasUnpinnedTabs);
}

/**
* Runs the given function with all tabs in the current window.
*
* @param {Function} fn The function to run with all tabs.
*/
function withAllTabs(fn) {
chrome.windows.getCurrent({ populate: true }, function (curWindow) {
fn(curWindow.tabs);
});
};
}

chrome.commands.onCommand.addListener(function(command) {
if (command === PIN_UNPIN_TAB) {
forEachTab(function(tab) {
if (tab.active) {
chrome.tabs.update(tab.id, {pinned: !tab.pinned});
}
});
} else if (command === UNPIN_ALL) {
forEachTab(function(tab) {
chrome.tabs.update(tab.id, {pinned: false});
});
} else if (command === PIN_ALL) {
forEachTab(function(tab) {
chrome.tabs.update(tab.id, {pinned: true});
});
} else {
return;
/**
* Runs the given function with all user selected tabs in the current window.
*
* @param {Function} fn The function to run with all user selected tabs.
*/
function withSelectedTabs(fn) {
chrome.tabs.query({ currentWindow: true, highlighted: true }, (tabs) => {
fn(tabs);
});
}

chrome.commands.onCommand.addListener((command) => {
switch (command) {
case PIN_UNPIN_TAB:
withSelectedTabs((tabs) => {
togglePins(tabs);
});
break;
case UNPIN_ALL:
withAllTabs((tabs) => {
setTabsPinnedState(tabs, false);
});
break;
case PIN_ALL:
withAllTabs((tabs) => {
setTabsPinnedState(tabs, true);
});
break;
default:
break;
}
});