]> git.wh0rd.org Git - chrome-ext/tabs-backup.git/commitdiff
message: convert from getBackgroundPage calls to messages
authorMike Frysinger <vapier@gentoo.org>
Fri, 7 Oct 2022 19:04:09 +0000 (00:49 +0545)
committerMike Frysinger <vapier@gentoo.org>
Fri, 7 Oct 2022 19:07:04 +0000 (00:52 +0545)
Since MV3 doesn't support getBackgroundPage, switch to runtime message
passing instead.  It should largely be straight forward.

advanced.js
background.js
options.js
popup.js

index 22faca0bbdd02ffdd01b61a3374c3f3ce475fa4e..62f332c623f829d5e1607cc95fc0ae4a8007549a 100644 (file)
@@ -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 () {
index 1771059d1022129b2ceec595ba60860beb0c2b60..1bae0182fbe043a06d820f0dcbb1999a3b614ac1 100644 (file)
@@ -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;
+});
index 629a882da6880949b916febeaaf751e3d0d18716..a565f214b13f78b30cf40d854a5e565d32ea0ea6 100644 (file)
@@ -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");
index c8b955b00de72b4ae839aa1ea6027ef55d8d431e..176e068473e2d61073613974cfd73df05e582b76 100644 (file)
--- 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'],
+       });
 }