-
Notifications
You must be signed in to change notification settings - Fork 2
/
options.js
172 lines (159 loc) · 5.54 KB
/
options.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
new Vue({
el: '#app',
data: {
config: {
selected: "random",
},
folderName: "",
recursionConfig: true,
randomBookmark: {
id: "",
url: "",
title: "",
},
folders: [
// "Bookmarks bar",
"Bookmarks bar/博客",
// "other bookmark/picture",
// "书签栏/博客",
// "书签栏/博客/技术博客"
],
markdown: {
content: ""
},
errorInfo: ""
},
created() {
chrome.bookmarks.getTree(this.initConfig);
},
methods: {
init() {
chrome.bookmarks.getTree(this.analyseBookMark);
},
// 书签栏 移动设备书签 其它书签
analyseBookMark(data) {
let value = this.folderName.trim();
var words = this.splitBookMarkName(value);
data = this.getFirstLevelBookMarksFolder(data);
// 获取到目标文件夹
data = this.getTargetBookMarksFolder(data, words);
// 递归获取目标文件夹的所有书签
let list = this.getBookmarks(data);
if (this.config.selected == "random") {
this.getRandomBookmark(list);
} else if (this.config.selected == "markdown") {
this.getMarkdown(list);
}
},
// 选择该文件夹
chooseFolder(folder) {
this.folderName = folder;
},
removeBookmarkByID(id) {
let flag = confirm("confrim remove this bookmark?")
if (flag) {
chrome.bookmarks.remove(id);
alert("remove sucess.");
}
},
// 获取 markdown 格式链接
getMarkdown(list) {
console.log("get markdown ...");
let str = "";
if (list.length == 0) {
str = "请参考下面输入样例 :)";
} else {
str = '# List\n\n';
for (let i = 0; i < list.length; i++) {
str = str.concat('[').concat(list[i].title).concat(']').concat('(').concat(list[i].url).concat(')\n\n');
}
}
this.markdown.content = str;
},
// 随机获取一个书签
getRandomBookmark(list) {
if (list.length === 0) {
this.errorInfo = `
bookmark folder do not exists,
try to check the root folder is '书签栏' or 'Bookmarks bar'.
`;
} else {
let index = Math.floor(Math.random() * list.length);
let bookmark = list[index];
this.randomBookmark.id = bookmark.id;
this.randomBookmark.url = bookmark.url;
this.randomBookmark.title = bookmark.title;
// window.open(this.url, 'target');
}
},
splitBookMarkName(value) {
// value = "书签栏::博客"
var words = value.split('/')
return words;
},
getBookmarks(data) {
var list = [];
this.getBookmarksByFolder(data, list);
return list;
},
getBookmarksByFolder(data, list) {
var recursionConfig = this.recursionConfig;
for (var children in data) {
if (typeof (data[children]) == 'object') {
let subData = data[children];
for (let index = 0; index < subData.length; index++) {
if (subData[index].url != undefined) {
list.push({
'id': subData[index].id,
'title': subData[index].title,
'url': subData[index].url
})
} else {
if (recursionConfig) {
this.getBookmarksByFolder(subData[index], list);
}
}
}
}
}
},
getTargetBookMarksFolder(data, words) {
if (words.length === 0) {
return data;
} else {
data = this.getTargetBookMarksFolderByWord(data, words[0]);
words.splice(0, 1);
return this.getTargetBookMarksFolder(data, words);
}
},
getTargetBookMarksFolderByWord(data, word) {
for (let children in data) {
if (typeof (data[children]) == 'object') {
data = data[children];
for (let index in data) {
if (data[index].title === word) {
return data[index];
}
}
}
}
},
// util function 先获取根书签文件夹
getFirstLevelBookMarksFolder(data) {
for (var children in data) {
if (typeof (data[children]) == 'object' && data[children].title == '') {
return data[children];
} else {
return getFirstLevelBookMarksFolder(data[children]);
}
}
},
initConfig(data) {
data = this.getFirstLevelBookMarksFolder(data);
data.children.forEach(element => {
this.folders.push(element.title);
});
this.folderName = this.folders[0];
}
},
})