From eda98b5f493e95f8679e85f966396be6b7a2174e Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Sat, 8 Oct 2022 00:49:09 +0545 Subject: [PATCH] message: convert from getBackgroundPage calls to messages Since MV3 doesn't support getBackgroundPage, switch to runtime message passing instead. It should largely be straight forward. --- advanced.js | 11 ++++++++--- background.js | 43 ++++++++++++++++++++++++++++++++++++++----- options.js | 2 +- popup.js | 23 ++++++++++++++++------- 4 files changed, 63 insertions(+), 16 deletions(-) diff --git a/advanced.js b/advanced.js index 22faca0..62f332c 100644 --- a/advanced.js +++ b/advanced.js @@ -675,7 +675,9 @@ function menu_backupNow() { lastTimeBackupNowClicked = new Date().getTime(); - chrome.runtime.getBackgroundPage((bg) => bg.backupNowManual(function(success, backupName, backupObj) { + chrome.runtime.sendMessage({ + action: 'backupNowManual', + }, function({success, backupName, backupObj}) { if (success) { //updateBackupsList(); insertBackupItem (backupName, backupObj, true /*insertAtBeginning*/, true /*doAnimation*/); @@ -685,12 +687,15 @@ function menu_backupNow() { } else { alert('An error occured while creating the backup..'); } - })); + }); } function menu_restoreNow() { - chrome.runtime.getBackgroundPage((bg) => bg.restoreNow('full_backup')); + chrome.runtime.sendMessage({ + action: 'restoreNow', + args: ['full_backup'], + }); } //document.onload(function () { diff --git a/background.js b/background.js index 1771059..1bae018 100644 --- a/background.js +++ b/background.js @@ -38,7 +38,7 @@ chrome.storage.local.get(function(items) { var d = new Date(); var formattedDate = date_format (d); - backupNow(true, formattedDate, function(success, backupName, backupObj) { + backupNow(true, formattedDate, function({success, backupName, backupObj}) { // backup completed }); }); @@ -67,7 +67,7 @@ function onAlarm (alarm) { // if last backup time != lastTabsEdit // perform automatic backup - backupNow(true, formattedDate, function(success, backupName, backupObj) { + backupNow(true, formattedDate, function({success, backupName, backupObj}) { // automatic backup completed var popupViews = chrome.extension.getViews({type: "popup"}); if (popupViews.length > 0) { @@ -246,7 +246,7 @@ function backupNow(isAutomatic, backupName, callbackDone) { //alert ("Error: " + chrome.runtime.lastError.message); updateBrowserActionIcon (1); - callbackDone(false); + callbackDone({success: false}); } else { console.log("backup saved"); //alert("Backup saved successfully!"); @@ -267,12 +267,16 @@ function backupNow(isAutomatic, backupName, callbackDone) { if (chrome.runtime.lastError) { console.log ("Error saving backups_list: " + chrome.runtime.lastError.message); updateBrowserActionIcon (1); - callbackDone(false); + callbackDone({success: false}); } else { console.log("Backups list saved successfully"); updateBrowserActionIcon (0); - callbackDone(true, backupName, fullBackup); + callbackDone({ + success: true, + backupName, + backupObj: fullBackup, + }); if (backupsList.length > items.prefs_max_backup_items) { deleteOldestBackup(); @@ -432,3 +436,32 @@ function restoreNow(backupName) { } }); } + +/** + * Callback from other pages (like the popup). + */ +chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { + console.log(`Got message from ${sender.id}: action=${request.action}`, request); + + let asyncResponse = false; + switch (request?.action) { + case 'initAlarm': + initAlarm(); + break; + + case 'restoreNow': + restoreNow(...request.args); + break; + + case 'deleteBackup': + deleteBackup(...request.args, sendResponse); + asyncResponse = true; + break; + + case 'backupNowManual': + backupNowManual(sendResponse); + asyncResponse = true; + break; + } + return asyncResponse; +}); diff --git a/options.js b/options.js index 629a882..a565f21 100644 --- a/options.js +++ b/options.js @@ -61,7 +61,7 @@ function saveOptions () { }); // Re-initialize the backup alarm - chrome.runtime.getBackgroundPage((bg) => bg.initAlarm()); + chrome.runtime.sendMessage({action: 'initAlarm'}); // Update status to let user know options were saved. var status = document.getElementById("statusDiv"); diff --git a/popup.js b/popup.js index c8b955b..176e068 100644 --- a/popup.js +++ b/popup.js @@ -140,7 +140,10 @@ function insertBackupItem (backupName, backupObj, insertAtBeginning, doAnimation return function(event) { bootbox.confirm("Open Windows & Tabs of backup '" + backupName + "'?", function(confirmed) { if (confirmed) { - chrome.runtime.getBackgroundPage((bg) => bg.restoreNow(backupName)); + chrome.runtime.sendMessage({ + action: 'restoreNow', + args: [backupName], + }); } }); @@ -157,9 +160,10 @@ function insertBackupItem (backupName, backupObj, insertAtBeginning, doAnimation bootbox.confirm("Delete backup '" + backupName + "'?", function(confirmed) { if (confirmed) { - chrome.runtime.getBackgroundPage((bg) => { - bg.deleteBackup(backupName, () => updateStorageInfo()); - }); + chrome.runtime.sendMessage({ + action: 'deleteBackup', + args: [backupName], + }, () => updateStorageInfo()); //if (elem.parentNode) { // elem.parentNode.removeChild(elem); @@ -321,7 +325,9 @@ function menu_backupNow() { lastTimeBackupNowClicked = new Date().getTime(); - chrome.runtime.getBackgroundPage((bg) => bg.backupNowManual(function(success, backupName, backupObj) { + chrome.runtime.sendMessage({ + action: 'backupNowManual', + }, function({success, backupName, backupObj}) { if (success) { //updateBackupsList(); insertBackupItem (backupName, backupObj, true /*insertAtBeginning*/, true /*doAnimation*/); @@ -331,10 +337,13 @@ function menu_backupNow() { } else { alert('An error occured while creating the backup..'); } - })); + }); } function menu_restoreNow() { - chrome.runtime.getBackgroundPage((bg) => bg.restoreNow('full_backup')); + chrome.runtime.sendMessage({ + action: 'restoreNow', + args: ['full_backup'], + }); } -- 2.39.5