From 3cdd7ca2de7f9f4eb0fa134310b524d6c96debca Mon Sep 17 00:00:00 2001
From: Teddy Wing
Date: Sun, 30 Mar 2014 19:13:47 -0400
Subject: 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.
---
chrome-get-urls-from-tabs-in-windows.js | 136 ++++++++++++++++++--------------
1 file 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 += '\n\
+ \n\
+
\n\
+ \n';
+
+ backup_text += ' Chrome Copy URLs From All Tabs\n';
- w_index++;
- });
- }
- else if (format === 'html') {
- textarea.value += '\n\
-\n\
-\n\
- \n';
-
- textarea.value += ' Chrome Copy URLs From All Tabs\n';
-
- textarea.value += '\n\
-\n\
- \n';
-
- windows.forEach(function(window){
- textarea.value += "
Window " + w_index + ":
\n\n";
- textarea.value += "
\n";
+ backup_text += '\n\
+ \n\
+ \n';
- window.tabs.forEach(function(tab){
- textarea.value += "
- \n"
- textarea.value += " " + tab.title + "\n";
- textarea.value += "
\n"
+ windows.forEach(function(window){
+ backup_text += "
Window " + w_index + ":
\n\n";
+ backup_text += "
\n";
+
+ window.tabs.forEach(function(tab){
+ backup_text += " - \n"
+ backup_text += " " + tab.title + "\n";
+ backup_text += "
\n"
+ });
+
+ backup_text += "
\n";
+
+ w_index++;
});
-
- textarea.value += " \n";
-
- w_index++;
- });
-
- textarea.value += '
\n\
-\n\
-';
- }
- 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 += '
\n\
+ \n\
+ ';
+ }
+ 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);
});
});
--
cgit v1.2.3