From 14a426d56d9fefbbdaca520d5b48466e4c30039f Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Sat, 9 Dec 2023 20:53:23 -0700 Subject: [PATCH] switch chrome.extension.getViews to messages This API no longer works in MV3, so switch to message broadcasts. --- advanced.js | 30 ++++++++++++++++++------------ background.js | 32 ++++++++------------------------ popup.js | 21 +++++++++++++++++++++ 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/advanced.js b/advanced.js index 91e4843..969ed0e 100644 --- a/advanced.js +++ b/advanced.js @@ -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 = "BYTES IN USE: " + bytesInUse + "
"; - 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; +}); diff --git a/background.js b/background.js index 0add75b..5aff375 100644 --- a/background.js +++ b/background.js @@ -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], + }); } // diff --git a/popup.js b/popup.js index 176e068..15b1545 100644 --- 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; +}); -- 2.39.5