diff options
| author | Teddy Wing | 2014-03-30 22:26:27 -0400 |
|---|---|---|
| committer | Teddy Wing | 2014-03-30 22:26:27 -0400 |
| commit | a0ea6d20f25fe55bdfd8abb3f4d20d0ceb657a90 (patch) | |
| tree | e9f74275b7a6c5e068e329942c442faa05f63b60 /chrome-get-urls-from-tabs-in-windows.js | |
| parent | b5f69d0b3426e86507a88e6f3bda5e758a8fd497 (diff) | |
| parent | c9f0e75dd6ad1d64ce63710216899cf8bb8155ae (diff) | |
| download | chrome-copy-urls-from-all-tabs-a0ea6d20f25fe55bdfd8abb3f4d20d0ceb657a90.tar.bz2 | |
Merge branch 'add-download-functionality'
Diffstat (limited to 'chrome-get-urls-from-tabs-in-windows.js')
| -rw-r--r-- | chrome-get-urls-from-tabs-in-windows.js | 149 |
1 files changed, 136 insertions, 13 deletions
diff --git a/chrome-get-urls-from-tabs-in-windows.js b/chrome-get-urls-from-tabs-in-windows.js index 4fdcefb..c46fbf5 100644 --- a/chrome-get-urls-from-tabs-in-windows.js +++ b/chrome-get-urls-from-tabs-in-windows.js @@ -1,21 +1,144 @@ var textarea = document.getElementById('copy-area'); +var generate_backup_text; +var create_download_link; +var generate_file_string; +var generate_filename; -chrome.windows.getAll({populate:true},function(windows){ - var w_index = 0; + +generate_backup_text = function(callback) { + var backup_text = ''; - windows.forEach(function(window){ - textarea.value += "Window " + w_index + ":"; + 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'; - window.tabs.forEach(function(tab){ - //collect all of the urls here, I will just log them instead - //console.log(tab.url); - textarea.value += "\n\t"; - textarea.value += '* ' + tab.url + "\n"; - textarea.value += "\t\t" + '<a href="' + tab.url + '">' + tab.title + '</a>'; + 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, callback) { + 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'; + + callback(download_link); + }); +}; + + +generate_file_string = function() { + 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); + + return 'chrome-tabs-' + date_string; +}; + + +generate_filename = function(callback) { + chrome.storage.sync.get(function(items) { + var format = items.file_format; - textarea.value += "\n\n"; + var file_extension = ''; + if (format === 'yaml') { + file_extension = 'yml'; + } + else if (format === 'html') { + file_extension = 'html'; + } + else { + file_extension = 'txt'; + } - w_index++; + callback(generate_file_string() + '.' + file_extension); }); -});
\ No newline at end of file +};
\ No newline at end of file |
