blob: 0ed0d8fa2c23f21b017ff12da6150fae484828bd (
plain)
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
|
var textarea = document.getElementById('copy-area');
var generate_backup_text;
var create_download_link;
var generate_filename;
generate_backup_text = function(callback) {
var backup_text = '';
chrome.windows.getAll({populate:true}, function(windows){
var w_index = 0;
chrome.storage.sync.get(function(items) {
var format = items.file_format;
if (format === 'yaml') {
var chrome_tabs = [];
windows.forEach(function(window){
backup_text += "- Window " + w_index + ":\n";
window.tabs.forEach(function(tab){
backup_text += " - page_title: '" + tab.title.replace('\'', '\'\'') + "'\n";
backup_text += " url: '" + tab.url + "'\n";
});
backup_text += "\n";
w_index++;
});
}
else if (format === 'html') {
backup_text += '<!doctype html>\n\
<html lang="en">\n\
<head>\n\
<meta charset="utf-8">\n';
backup_text += ' <title>Chrome Copy URLs From All Tabs</title>\n';
backup_text += '</head>\n\
<body>\n\
<div role="main">\n';
windows.forEach(function(window){
backup_text += " <h1>Window " + w_index + ":</h1>\n\n";
backup_text += " <ul>\n";
window.tabs.forEach(function(tab){
backup_text += " <li>\n"
backup_text += " <a href=\"" + tab.url + "\">" + tab.title + "</a>\n";
backup_text += " </li>\n"
});
backup_text += " </ul>\n";
w_index++;
});
backup_text += ' </div>\n\
</body>\n\
</html>';
}
else { // format === 'text'
windows.forEach(function(window){
backup_text += "Window " + w_index + ":";
window.tabs.forEach(function(tab){
backup_text += "\n";
backup_text += "\t* " + tab.title + "\n";
backup_text += "\t " + tab.url + "\n";
});
backup_text += "\n\n";
w_index++;
});
}
callback(backup_text);
});
});
};
generate_backup_text(function(backup_text) {
textarea.value = backup_text;
create_download_link(textarea.value, function(download_link) {
document.getElementById('download-link').appendChild(download_link);
});
});
// Adapted from:
// http://stackoverflow.com/a/18197511
create_download_link = function(text) {
generate_filename(function(filename) {
var download_link = document.createElement('a');
download_link.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
download_link.setAttribute('download', filename);
download_link.innerHTML = 'Download file';
document.getElementById('download-link').appendChild(download_link);
});
};
generate_filename = function(callback) {
chrome.storage.sync.get(function(items) {
var format = items.file_format;
var d = new Date();
var date_string =
d.getFullYear()
+ ''
+ ('0' + (d.getMonth() + 1)).slice(-2)
+ ''
+ ('0' + d.getDate()).slice(-2)
+ '-'
+ ('0' + d.getHours()).slice(-2)
+ 'h'
+ ('0' + d.getMinutes()).slice(-2);
var file_extension = '';
if (format === 'yaml') {
file_extension = 'yml';
}
else if (format === 'html') {
file_extension = 'html';
}
else {
file_extension = 'txt';
}
callback('chrome-tabs-' + date_string + '.' + file_extension);
});
};
|