RemoveBgExtensionChrome/background_utils.js

36 lines
852 B
JavaScript

const UTILS = {
getStorage: async function (key) {
const storage = await chrome.storage.local.get();
return storage[key] ?? null;
},
setStorage: async function (key, value) {
chrome.storage.local.set({
[key]: value,
});
return await this.getStorage(key);
},
sendMessageToFirstTab: function (data = {}) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs.length) {
console.log({
tabs,
});
chrome.tabs.sendMessage(tabs[0].id, data);
}
});
},
sendMessageToTabByURL: function (url, data = {}) {
chrome.tabs.query({}, function (tabs) {
tabs
.filter((tab) => tab.url.includes(url))
.forEach((tab) => {
chrome.tabs.sendMessage(tab.id, data);
});
});
},
};
export default UTILS;