diff options
| author | Teddy Wing | 2014-03-30 19:13:47 -0400 |
|---|---|---|
| committer | Teddy Wing | 2014-03-30 19:13:47 -0400 |
| commit | 3cdd7ca2de7f9f4eb0fa134310b524d6c96debca (patch) | |
| tree | 57e73f86a1469764627ae21f1220b32b40170227 /chrome-get-urls-from-tabs-in-windows.js | |
| parent | 6f988f83e450c22ab6c1d5fee7dab237181a3dbd (diff) | |
| download | chrome-copy-urls-from-all-tabs-3cdd7ca2de7f9f4eb0fa134310b524d6c96debca.tar.bz2 | |
Refactor backup text
Instead of updating the textarea directly, add the backup text to a
string and assign that string to the textarea later.
This allows us to add the text to a file for download on click of the
extension button and outside the context of the extension's main page.
Diffstat (limited to 'chrome-get-urls-from-tabs-in-windows.js')
| -rw-r--r-- | chrome-get-urls-from-tabs-in-windows.js | 136 |
1 files changed, 75 insertions, 61 deletions
diff --git a/chrome-get-urls-from-tabs-in-windows.js b/chrome-get-urls-from-tabs-in-windows.js index 46a9e8a..0ed0d8f 100644 --- a/chrome-get-urls-from-tabs-in-windows.js +++ b/chrome-get-urls-from-tabs-in-windows.js @@ -1,79 +1,93 @@ var textarea = document.getElementById('copy-area'); +var generate_backup_text; var create_download_link; var generate_filename; -chrome.windows.getAll({populate:true}, function(windows){ - var w_index = 0; +generate_backup_text = function(callback) { + var backup_text = ''; - chrome.storage.sync.get(function(items) { - var format = items.file_format; + chrome.windows.getAll({populate:true}, function(windows){ + var w_index = 0; - if (format === 'yaml') { - var chrome_tabs = []; + chrome.storage.sync.get(function(items) { + var format = items.file_format; - windows.forEach(function(window){ - textarea.value += "- Window " + w_index + ":\n"; + if (format === 'yaml') { + var chrome_tabs = []; - window.tabs.forEach(function(tab){ - textarea.value += " - page_title: '" + tab.title.replace('\'', '\'\'') + "'\n"; - textarea.value += " url: '" + tab.url + "'\n"; - }); + 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"; + }); - textarea.value += "\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'; - w_index++; - }); - } - else if (format === 'html') { - textarea.value += '<!doctype html>\n\ -<html lang="en">\n\ -<head>\n\ - <meta charset="utf-8">\n'; - - textarea.value += ' <title>Chrome Copy URLs From All Tabs</title>\n'; - - textarea.value += '</head>\n\ -<body>\n\ - <div role="main">\n'; - - windows.forEach(function(window){ - textarea.value += " <h1>Window " + w_index + ":</h1>\n\n"; - textarea.value += " <ul>\n"; + backup_text += '</head>\n\ + <body>\n\ + <div role="main">\n'; - window.tabs.forEach(function(tab){ - textarea.value += " <li>\n" - textarea.value += " <a href=\"" + tab.url + "\">" + tab.title + "</a>\n"; - textarea.value += " </li>\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++; }); - - textarea.value += " </ul>\n"; - - w_index++; - }); - - textarea.value += ' </div>\n\ -</body>\n\ -</html>'; - } - else { // format === 'text' - windows.forEach(function(window){ - textarea.value += "Window " + w_index + ":"; - window.tabs.forEach(function(tab){ - textarea.value += "\n"; - textarea.value += "\t* " + tab.title + "\n"; - textarea.value += "\t " + tab.url + "\n"; + 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++; }); - - textarea.value += "\n\n"; - - w_index++; - }); - } - - - create_download_link(textarea.value); + } + + + 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); }); }); |
