Skip to content

Commit

Permalink
feat(recently colsed tab): add recently closed tab search
Browse files Browse the repository at this point in the history
  • Loading branch information
crown committed Jun 3, 2018
1 parent e3f72ac commit f8e256d
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 21 deletions.
39 changes: 39 additions & 0 deletions src/api/chrome-api.js
Expand Up @@ -153,6 +153,43 @@ function listenMsg(callback) {
chrome.runtime.onMessage.addListener(callback)
}

function queryRecentLyClosed(arr) {
return new Promise(resolve => {
if (arr.length === 0) arr.push('')
chrome.sessions.getRecentlyClosed({
maxResults: 5
}, (rep) => {
const tmp = []
for (let index = 0; index < rep.length; index += 1) {
const item = rep[index].tab
if (item) {
/**
* length === 0: 默认出现所有所有 最近关闭的标签页
* length === 1 : 不需要进行后面的多个单词匹配
*/
const isRight = arr.length < 2 || arr.every(item1 => new RegExp(`${item1}`, 'gi').test(`${item.url} ${item.title}`))
if (isRight) {
tmp.push({
type: 'recentlyClosed',
title: item.title,
subtitle: item.url,

id: item.sessionId,
})
}
}
}
resolve(tmp)
})

})

}

function restoreRecentTab(sessionId) {
chrome.sessions.restore(sessionId)
}

export default {
findActiveTab,
getConfig,
Expand All @@ -165,4 +202,6 @@ export default {
setConfig,
updateTabStatus,
openNewTab,
queryRecentLyClosed,
restoreRecentTab
}
1 change: 1 addition & 0 deletions src/assets/close.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions src/background/default-setting.js
Expand Up @@ -6,20 +6,20 @@ export default {
/**
* @param
* isDefault: 在不指定 keyword 的情况下是否进入搜索队列
* isSearch: 是否搜索该栏目下的内容
* keyword: 搜索的关键字
*/
{
type: 'bookmark',
isSearch: true,
isDefault: true,
keyword: 'bm'
}, {
type: 'tab',
isSearch: true,
isDefault: true,
keyword: 't'
}
}, {
type: 'recentlyClosed',
keyword: 'rc'
},
]

}
30 changes: 16 additions & 14 deletions src/background/filter-search-data.js
Expand Up @@ -42,21 +42,20 @@ function filterKeyword(strArr) {
let tmp = []
for (let idx = 0; idx < setting.itemSetting.length; idx += 1) {
const item = setting.itemSetting[idx]
if (item.isSearch) {
if (item.keyword === strArr[0]) {
// 搜索单个子列
tmp = [{
type: item.type,
strArr: strArr.slice(1)
}]
break
} else if (item.isDefault)
tmp.push({
type: item.type,
strArr
})
}
if (item.keyword === strArr[0]) {
// 搜索单个子列
tmp = [{
type: item.type,
strArr: strArr.slice(1)
}]
break
} else if (item.isDefault)
tmp.push({
type: item.type,
strArr
})
}
console.log(tmp)
// 不管是默认搜索还是单项的搜索, 都要出现 keyowrd 类别的提示(如果有对应的话)
return [{
type: 'keyword',
Expand All @@ -76,6 +75,9 @@ function searchFromType(item, callback) {
case 'tab':
chromeAPI.queryTab(item.strArr).then(tmp => callback(null, tmp))
break
case 'recentlyClosed':
chromeAPI.queryRecentLyClosed(item.strArr).then(tmp => callback(null, tmp))
break

default:
break
Expand Down
3 changes: 3 additions & 0 deletions src/background/handle-selected-item.js
Expand Up @@ -8,6 +8,9 @@ function handleSelectedItem(item) {
case 'tab':
chromeAPI.updateTabStatus(item.id)
break
case 'recentlyClosed':
chromeAPI.restoreRecentTab(item.id)
break

default:
break
Expand Down
2 changes: 0 additions & 2 deletions src/background/index.js
Expand Up @@ -9,8 +9,6 @@ import handleSelectedItem from './handle-selected-item'

import util from '../util'

chromeAPI.getConfig()

// init extension setting if
chromeAPI
.getConfig()
Expand Down
3 changes: 3 additions & 0 deletions src/components/SearchList/search-list.styl
Expand Up @@ -38,6 +38,9 @@
&.keyword
background: url('../../assets/keyword.svg') center / 34px no-repeat

&.recentlyClosed
background: url('../../assets/close.svg') center / 34px no-repeat

.key-enter
display: none
flex-shrink: 0
Expand Down
2 changes: 1 addition & 1 deletion static/manifest.json
Expand Up @@ -46,7 +46,7 @@
"omnibox": {
"keyword": "c"
},
"permissions": ["bookmarks", "chrome://favicon/", "tabs", "storage", "<all_urls>"],
"permissions": ["bookmarks", "sessions", "tabs", "storage", "<all_urls>"],
"version": "2.0.0",
"web_accessible_resources": [
"img/*"
Expand Down

0 comments on commit f8e256d

Please sign in to comment.