From 08781bc28dfa2bd517a0806eab65e6fa2f30974a Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 30 Mar 2014 19:19:12 -0400 Subject: Add functionality for downloading backup on icon click If the download option is set, a backup file is now downloaded when the extension button is clicked. --- background.js | 21 ++++++++++++++++++++- chrome-get-urls-from-tabs-in-windows.js | 4 ++-- manifest.json | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/background.js b/background.js index df2c829..b775258 100644 --- a/background.js +++ b/background.js @@ -30,7 +30,26 @@ chrome.extension.onConnect.addListener(function(port) { }); }); + +function download_backup_file () { + generate_backup_text(function(backup_text) { + create_download_link(backup_text, function(download_link) { + download_link.click() + }); + }); +} + + // Called when the user clicks on the browser action icon. chrome.browserAction.onClicked.addListener(function(tab) { - openOrFocusOptionsPage(); + chrome.storage.sync.get(function(items) { + var behaviour = items.button_click_behaviour; + + if (behaviour === 'download') { + download_backup_file(); + } + else { // behaviour === 'window' + openOrFocusOptionsPage(); + } + }); }); \ No newline at end of file diff --git a/chrome-get-urls-from-tabs-in-windows.js b/chrome-get-urls-from-tabs-in-windows.js index 0ed0d8f..fc27d1f 100644 --- a/chrome-get-urls-from-tabs-in-windows.js +++ b/chrome-get-urls-from-tabs-in-windows.js @@ -94,14 +94,14 @@ generate_backup_text(function(backup_text) { // Adapted from: // http://stackoverflow.com/a/18197511 -create_download_link = function(text) { +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'; - document.getElementById('download-link').appendChild(download_link); + callback(download_link); }); }; diff --git a/manifest.json b/manifest.json index 72f46bf..c08a1e6 100644 --- a/manifest.json +++ b/manifest.json @@ -12,7 +12,7 @@ "options_page": "options.html", "background": { - "scripts": ["background.js"], + "scripts": ["chrome-get-urls-from-tabs-in-windows.js", "background.js"], "persistent": false }, -- cgit v1.2.3