-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
|
||
module.exports = function (steward) { | ||
const version = 1; | ||
const author = 'solobat'; | ||
const name = 'Tabs plus'; | ||
const type = 'keyword'; | ||
const icon = 'https://i.imgur.com/QcoukjA.png'; | ||
const title = 'Manage tabs in all the window'; | ||
const subtitle = 'Manage tabs in all the window.'; | ||
const commands = [{ | ||
key: 'atab', | ||
type, | ||
title, | ||
subtitle, | ||
icon | ||
}]; | ||
|
||
function getAllTabs(query) { | ||
return new Promise((resolve) => { | ||
chrome.windows.getAll({ populate: true }, function (wins) { | ||
if (wins.length) { | ||
const tabs = wins.reduce((memo, win) => { | ||
memo.push(...win.tabs) | ||
return memo | ||
}, []) | ||
|
||
const results = tabs.filter(function (tab) { | ||
return steward.util.matchText(query, `${tab.title}${tab.url}`); | ||
}); | ||
|
||
resolve(results) | ||
} else { | ||
resolve([]); | ||
} | ||
}); | ||
}) | ||
} | ||
|
||
function dataFormat(list, command) { | ||
return list.map(function (item, index) { | ||
let desc = command.subtitle; | ||
|
||
return { | ||
key: command.key, | ||
id: item.id, | ||
icon: item.favIconUrl || chrome.extension.getURL('img/icon.png'), | ||
title: `[Win: ${item.windowId}]${item.title}`, | ||
desc, | ||
isWarn: item.active, | ||
raw: item | ||
}; | ||
}); | ||
} | ||
|
||
function onInput(query, command) { | ||
return getAllTabs(query).then(tabs => { | ||
return dataFormat(tabs, command) | ||
}) | ||
} | ||
|
||
function updateWindow(winId, updateProperties) { | ||
return chrome.windows.update(winId, updateProperties); | ||
} | ||
|
||
function updateTab(id, updateProperties, winId) { | ||
if (updateProperties.active) { | ||
updateWindow(winId, { | ||
focused: true | ||
}) | ||
} | ||
return chrome.tabs.update(id, updateProperties); | ||
} | ||
|
||
function activeOneTab(item) { | ||
updateTab(item.id, { | ||
active: true | ||
}, item.raw.windowId); | ||
} | ||
|
||
function onEnter(item, command, query, shiftKey, list) { | ||
if (command.key === 'atab') { | ||
activeOneTab(item); | ||
} | ||
} | ||
|
||
return { | ||
author, | ||
version, | ||
name, | ||
category: 'browser', | ||
icon, | ||
title, | ||
commands, | ||
onInput, | ||
onEnter | ||
}; | ||
} |