Skip to content

Commit

Permalink
修复新打开浏览器时最近关闭列表无法获取图标的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean-214 committed Sep 10, 2020
1 parent 92db55d commit c91875d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "last-session",
"version": "1.1.1",
"version": "1.1.2",
"private": true,
"scripts": {
"lint": "eslint --fix --ignore-pattern \"src/assets/iconfont/**\" \"src/**/*.js\"",
Expand Down
34 changes: 32 additions & 2 deletions src/lib/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import storage from './storage';
import util from './util';

const LAST_SESSION = { lastSession: { lastModified: 0, tabs: [] } };
const CACHED_RECENTS = { cachedRecents: {} };
const EXPIRES = 7 * 24 * 60 * 60 * 1000;

/**
* 获取最近关闭的标签页和/或窗口的列表
Expand Down Expand Up @@ -48,18 +50,46 @@ async function setRemoved(url, removed) {

async function getRecents(filter = null) {
const tabs = [];
const sessionList = await getRecentlyClosed();
for (const session of sessionList) {
const sessions = await getRecentlyClosed();
const cachedRecents = await getCachedRecents();
const now = Date.now();
for (const session of sessions) {
if (session && session.tab && session.tab.sessionId != null && !util.isInnerUrl(session.tab.url)) {
tabs.push(session.tab);
// 从缓存中获取favIconUrl
const key = session.tab.url;
const favIconUrl = session.tab.favIconUrl;
if (favIconUrl) {
cachedRecents[key] = { favIconUrl, expires: now };
} else if (cachedRecents[key]) {
session.tab.favIconUrl = cachedRecents[key].favIconUrl;
// 更新缓存过期时间
cachedRecents[key].expires = now;
}
}
}
// 更新缓存
for (const [key, value] of Object.entries(cachedRecents)) {
if (now - value.expires > EXPIRES) {
delete cachedRecents[key];
}
}
await setCachedRecents(cachedRecents);
if (filter && filter.maxResults) {
return tabs.slice(0, filter.maxResults);
}
return tabs;
}

async function getCachedRecents() {
const items = await storage.get(CACHED_RECENTS);
return items.cachedRecents;
}

function setCachedRecents(cachedRecents = {}) {
return storage.set({ cachedRecents });
}

export default {
getRecentlyClosed,
restore,
Expand Down

0 comments on commit c91875d

Please sign in to comment.