diff --git a/extension/background.js b/extension/background.js index a22a67c..fc9c5d4 100644 --- a/extension/background.js +++ b/extension/background.js @@ -103,6 +103,8 @@ function initialize() { setInterruptDownload(!interruptDownloads, true); } }); + // Set internal popup.js message listener + current_browser.runtime.onMessage.addListener(handleInternalMessage); chromeVersion = parseInt(chromeVersion); sendMessageToHost(message); createContextMenus(); @@ -638,7 +640,7 @@ function parseCookies(cookies_arr) { /** * Update the exclude keywords. - * Is called from the popup.js. + * Is triggered by the popup.js message handler. */ function updateExcludeKeywords(exclude) { if (exclude === "") { @@ -651,7 +653,7 @@ function updateExcludeKeywords(exclude) { /** * Update the include keywords. - * Is called from the popup.js. + * Is triggered by the popup.js message handler. */ function updateIncludeKeywords(include) { if (include === "") { @@ -664,7 +666,7 @@ function updateIncludeKeywords(include) { /** * Update the exclude MIMEs. - * Is called from the popup.js. + * Is triggered by the popup.js message handler. */ function updateExcludeMIMEs(exclude) { if (exclude === "") { @@ -677,7 +679,7 @@ function updateExcludeMIMEs(exclude) { /** * Update the include MIMEs. - * Is called from the popup.js. + * Is triggered by the popup.js message handler. */ function updateIncludeMIMEs(include) { if (include === "") { @@ -690,13 +692,48 @@ function updateIncludeMIMEs(include) { /** * Update the minimum file size to interrupt. - * Is called from the popup.js. + * Is triggered by the popup.js message handler. */ function updateMinFileSize(size) { minFileSizeToInterrupt = size; current_browser.storage.sync.set({ "uget-min-file-size": size }); } +/** + * Handle messages within the extension. + * Is triggered by a popup.js message. + */ +function handleInternalMessage(message, sender, sendResponse) { + if (message.hasOwnProperty("get")) { + switch (message.get) { + case "state": + sendResponse({data: getState()}); + break; + } + } else if (message.hasOwnProperty("update")) { + switch (message.update) { + case "interruptDownload": + setInterruptDownload(message.data, true); + break; + case "minFileSize": + updateMinFileSize(message.data); + break; + case "excludeKeywords": + updateExcludeKeywords(message.data); + break; + case "includeKeywords": + updateIncludeKeywords(message.data); + break; + case "excludeMIMEs": + updateExcludeMIMEs(message.data); + break; + case "includeMIMEs": + updateIncludeMIMEs(message.data); + break; + } + } +} + /** * Check whether not to interrupt the given url. */ diff --git a/extension/popup.js b/extension/popup.js index f54266c..abe0228 100644 --- a/extension/popup.js +++ b/extension/popup.js @@ -20,6 +20,10 @@ var current_browser; +function sendMessageCallbabackToPromise(message, responseCallback) { + browser.runtime.sendMessage(message).then(responseCallback); +} + try { current_browser = browser; current_browser.runtime.getBrowserInfo().then( @@ -29,15 +33,17 @@ try { } } ); + compatSendMessage = sendMessageCallbabackToPromise; } catch (ex) { // Not Firefox current_browser = chrome; + compatSendMessage = current_browser.runtime.sendMessage } $(document).ready(function() { // Show the system status - current_browser.runtime.getBackgroundPage(function(backgroundPage) { - var state = backgroundPage.getState(); + compatSendMessage({get: "state"}, function(response) { + var state = response.data; if (state == 0) { $('#info').css('display', 'block'); $('#warn').css('display', 'none'); @@ -65,9 +71,9 @@ $(document).ready(function() { // Set event listeners $('#chk_enable').change(function() { var enabled = this.checked; - current_browser.runtime.getBackgroundPage(function(backgroundPage) { - backgroundPage.setInterruptDownload(enabled, true); - }); + compatSendMessage( + {update: "interruptDownload", data: enabled} + ); }); $("#fileSize").on("change paste", function() { var minFileSize = parseInt($(this).val()); @@ -77,32 +83,32 @@ $(document).ready(function() { minFileSize = -1; } $('#fileSize').val(minFileSize); - current_browser.runtime.getBackgroundPage(function(backgroundPage) { - backgroundPage.updateMinFileSize(minFileSize * 1024); - }); + compatSendMessage( + {update: "minFileSize", data: minFileSize * 1024} + ); }); $("#urlsToExclude").on("change paste", function() { var keywords = $(this).val().trim(); - current_browser.runtime.getBackgroundPage(function(backgroundPage) { - backgroundPage.updateExcludeKeywords(keywords); - }); + compatSendMessage( + {update: "excludeKeywords", data: keywords} + ); }); $("#urlsToInclude").on("change paste", function() { var keywords = $(this).val().trim(); - current_browser.runtime.getBackgroundPage(function(backgroundPage) { - backgroundPage.updateIncludeKeywords(keywords); - }); + compatSendMessage( + {update: "includeKeywords", data: keywords} + ); }); $("#mimeToExclude").on("change paste", function() { var keywords = $(this).val().trim(); - current_browser.runtime.getBackgroundPage(function(backgroundPage) { - backgroundPage.updateExcludeMIMEs(keywords); - }); + compatSendMessage( + {update: "excludeMIMEs", data: keywords} + ); }); $("#mimeToInclude").on("change paste", function() { var keywords = $(this).val().trim(); - current_browser.runtime.getBackgroundPage(function(backgroundPage) { - backgroundPage.updateIncludeMIMEs(keywords); - }); + compatSendMessage( + {update: "includeMIMEs", data: keywords} + ); }); }); \ No newline at end of file