-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
177 lines (175 loc) · 4.75 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// var ctx = $("#canvas")[0].getContext("2d");
// var ctx = document.createElement('canvas').getContext("2d")
var getYear = function () {
var date = new Date();
return date.getFullYear().toString();
};
var getMonth = function () {
var date = new Date();
return [date.getFullYear(), date.getMonth() + 1].join("-");
};
var canFav = 1;
var currentTabId = null;
// var favImg = new Image();
// favImg.src = "icon-1.png";
// favImg.width = 19;
// favImg.height = 19;
// document.body.appendChild(favImg);
// var favedImg = new Image();
// favedImg.width = 19;
// favedImg.height = 19;
// favedImg.src = "icon-1-faved.png";
// document.body.appendChild(favedImg);
// favImg.onload = function () {};
var setNormalIcon = function () {
// ctx.clearRect(0,0, 64, 64)
// ctx.drawImage(favImg,0,0,19,19)
chrome.action.setIcon({
path: {
19: "icon.png",
38: "icon.png",
},
});
chrome.action.setTitle({ title: "点击收藏到本月文件夹" });
};
var setLikedIcon = function () {
// ctx.clearRect(0,0, 64, 64)
// ctx.drawImage(favedImg,0,0,19,19)
chrome.action.setIcon({
path: {
19: "icon-faved.png",
38: "icon-faved.png",
},
});
chrome.action.setTitle({ title: "已经收藏过此页面" });
// canFav=0
};
function handleBookmarkChange(url) {
chrome.bookmarks.search(url, function (list) {
changeIcon(list && list.length)
})
}
function changeIcon(flag) {
if (flag) {
setLikedIcon()
canFav = 0
} else {
setNormalIcon()
canFav = 1
}
}
chrome.bookmarks.onRemoved.addListener(function (id, removeInfo) {
// console.log('removeinfo', removeInfo)
// currentTabId
chrome.tabs.get(currentTabId, function (tab) {
// console.log('after remove currentTab', tab)
handleBookmarkChange(tab.url)
})
})
chrome.bookmarks.onChanged.addListener(function (id, changeInfo) {
// console.log('change', id, changeInfo)
handleBookmarkChange(changeInfo.url)
});
chrome.tabs.onActivated.addListener(function (activeInfo) {
currentTabId = activeInfo.tabId;
// console.log('active current', currentTab)
chrome.tabs.get(activeInfo.tabId, function (tab) {
handleBookmarkChange(tab.url)
});
});
chrome.tabs.onCreated.addListener(function (tab) {
currentTabId = tab.id
// console.log('create current', currentTab)
handleBookmarkChange(tab.url)
});
/**
* 创建 年份-月份 文件夹
* @param {string} yearId 年份文件夹id
* @param {string} nowMonth 年份-月份 信息
* @param {object} tab 需要收藏的当前标签页
*/
function createYearMonthFolder(yearId, nowMonth, tab) {
// console.log(yearId);
chrome.bookmarks.create(
{
index: 0,
title: nowMonth,
parentId: yearId,
},
function (monthNode) {
createBookmark(monthNode.id, tab);
}
);
}
/**
* 创建书签
* @param {string} monthId 年份-月份 文件夹id
* @param {object} tab 需要收藏的当前标签页
*/
function createBookmark(monthId, tab) {
chrome.bookmarks.create(
{
index: 0,
title: tab.title,
url: tab.url,
parentId: monthId,
},
function () {
setLikedIcon();
}
);
}
chrome.action.onClicked.addListener(function (tab) {
if (!canFav) return;
var nowYear = getYear();
var nowMonth = getMonth();
chrome.bookmarks.get("1", function (root) {
if (root.length) {
chrome.bookmarks.search(nowMonth, function (list) {
var monthId = null;
var yearId = null;
// 如果 year-month 日期文件夹存在,就将该 文件夹id 赋值给 id 变量
if (list) {
list.forEach(function (item) {
if (item.title == nowMonth) {
monthId = item.id;
}
});
}
// 年份-月份 文件夹不存在
if (!list || monthId === null) {
// 判断 年份 文件夹是否存在
chrome.bookmarks.search(nowYear, function (marks) {
if (marks) {
marks.forEach(function (mark) {
if (mark.title == nowYear) {
yearId = mark.id;
}
});
}
// 年份 文件夹不存在
if (!marks || yearId == null) {
// 创建年份文件夹
chrome.bookmarks.create(
{
index: 0,
title: nowYear,
parentId: root[0].id,
},
function (yearNode) {
createYearMonthFolder(yearNode.id, nowMonth, tab);
}
);
} else {
// 年份 文件夹存在
createYearMonthFolder(yearId, nowMonth, tab);
}
});
} else {
// 年份-月份 文件夹存在
createBookmark(monthId, tab);
}
});
}
});
});