Skip to content

Fix popup in private Firefox windows by replacing getBackgroundPage #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 === "") {
Expand All @@ -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 === "") {
Expand All @@ -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 === "") {
Expand All @@ -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 === "") {
Expand All @@ -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.
*/
Expand Down
46 changes: 26 additions & 20 deletions extension/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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');
Expand Down Expand Up @@ -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());
Expand All @@ -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}
);
});
});