]> git.wh0rd.org Git - chrome-ext/tabs-backup.git/commitdiff
switch chrome.extension.getViews to messages
authorMike Frysinger <vapier@gentoo.org>
Sun, 10 Dec 2023 03:53:23 +0000 (20:53 -0700)
committerMike Frysinger <vapier@gentoo.org>
Sun, 10 Dec 2023 04:01:55 +0000 (21:01 -0700)
This API no longer works in MV3, so switch to message broadcasts.

advanced.js
background.js
popup.js

index 91e4843693169a8ce6a407aa4b07b03744107a71..969ed0ed24c54c5bb6bc8675285c19556ad536ff 100644 (file)
@@ -675,15 +675,21 @@ function menu_restoreNow() {
        });
 }
 
-//document.onload(function () {
-//var a = document.getElementById("myid");
-//a.innerHTML = "ciaociao";
-//});
-
-/*
-var storageLocal = chrome.storage.local;
-storageLocal.getBytesInUse(null, function(bytesInUse) {
-       var elem = document.createElement("div");
-       elem.innerHTML = "<b>BYTES IN USE: " + bytesInUse + "</b><br />";
-       document.body.appendChild(elem);
-});*/
+/**
+ * Callback from other pages (like the background).
+ */
+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 'insertBackupItem':
+                       insertBackupItem(...request.args);
+                       break;
+
+               case 'removeBackupItemDiv':
+                       removeBackupItemDiv(...request.args);
+                       break;
+       }
+       return asyncResponse;
+});
index 0add75bb216c07b4f09f6510c0ac13c940a69e0a..5aff375df3e747389f1a761198e7dd80d6ba882d 100644 (file)
@@ -48,18 +48,10 @@ function onAlarm (alarm) {
        //      perform automatic backup
                backupNow(true, formattedDate, function({success, backupName, backupObj}) {
                        // automatic backup completed
-                       var popupViews = chrome.extension.getViews({type: "popup"});
-                       if (popupViews.length > 0) {
-                               for (var i = 0; i < popupViews.length; i++) {
-                                       var popupView = popupViews[i];
-                                       if (!popupView.insertBackupItem) {
-                                               continue;
-                                       }
-
-                                       popupView.insertBackupItem(backupName, backupObj, true /*insertAtBeginning*/, true /*doAnimation*/);
-                                       popupView.updateStorageInfo();
-                               }
-                       }
+                       chrome.runtime.sendMessage({
+                               action: 'insertBackupItem',
+                               args: [backupName, backupObj, true /*insertAtBeginning*/, true /*doAnimation*/],
+                       });
                });
 }
 
@@ -103,18 +95,10 @@ function deleteOldestBackup () {
                                //
                                if (i > 0) {
                                        var deletedBackupName = backupsList[i-1];
-                                       var popupViews = chrome.extension.getViews({type: "popup"});
-                                       if (popupViews.length > 0) {
-                                               for (var j = 0; j < popupViews.length; j++) {
-                                                       var popupView = popupViews[j];
-                                                       if (!popupView.removeBackupItemDiv) {
-                                                               continue;
-                                                       }
-
-                                                       popupView.removeBackupItemDiv(deletedBackupName);
-                                                       popupView.updateStorageInfo();
-                                               }
-                                       }
+                                       chrome.runtime.sendMessage({
+                                               action: 'removeBackupItemDiv',
+                                               args: [deletedBackupName],
+                                       });
                                }
                                //
 
index 176e068473e2d61073613974cfd73df05e582b76..15b154568332e9cc208c2106217c00c4cb1bafd0 100644 (file)
--- a/popup.js
+++ b/popup.js
@@ -347,3 +347,24 @@ function menu_restoreNow() {
                args: ['full_backup'],
        });
 }
+
+/**
+ * Callback from other pages (like the background).
+ */
+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 'insertBackupItem':
+                       insertBackupItem(...request.args);
+                       updateStorageInfo();
+                       break;
+
+               case 'removeBackupItemDiv':
+                       removeBackupItemDiv(...request.args);
+                       updateStorageInfo();
+                       break;
+       }
+       return asyncResponse;
+});