implement polyfilling, first-class firefox support coming soon

This commit is contained in:
Alphons
2023-12-02 22:17:34 +08:00
parent a9a4153c80
commit acba86d325
10 changed files with 172 additions and 118 deletions
+29 -23
View File
@@ -1,4 +1,5 @@
/*global chrome*/
import browser from 'webextension-polyfill'
import { onError } from './seqta/utils/onError.js';
export const openDB = () => {
return new Promise((resolve, reject) => {
const request = indexedDB.open('MyDatabase', 1);
@@ -56,22 +57,24 @@ export const readData = () => {
};
function reloadSeqtaPages() {
chrome.tabs.query({}, function (tabs) {
const result = browser.tabs.query({})
function open (tabs) {
for (let tab of tabs) {
if (tab.title.includes('SEQTA Learn')) {
chrome.tabs.reload(tab.id);
browser.tabs.reload(tab.id);
}
}
});
}
result.then(open, onError)
}
// Helper function to handle setting permissions
const handleAddPermissions = () => {
if (typeof chrome.declarativeContent !== 'undefined') {
chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {});
if (typeof browser.declarativeContent !== 'undefined') {
browser.declarativeContent.onPageChanged.removeRules(undefined, () => {});
}
chrome.permissions.request(
browser.permissions.request(
{ permissions: ['declarativeContent'], origins: ['*://*/*'] },
(granted) => {
if (granted) {
@@ -80,7 +83,7 @@ const handleAddPermissions = () => {
];
rules.forEach(rule => {
chrome.declarativeContent.onPageChanged.addRules([rule]);
browser.declarativeContent.onPageChanged.addRules([rule]);
});
alert('Permissions granted. Reload SEQTA pages to see changes. If this workaround doesn\'t work, please contact the developer. It will be an easy fix');
@@ -90,22 +93,22 @@ const handleAddPermissions = () => {
};
// Main message listener
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.type) {
case 'reloadTabs':
reloadSeqtaPages();
break;
case 'currentTab':
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, request, function (response) {
browser.tabs.query({ active: true, currentWindow: true }).then(function (tabs) {
browser.tabs.sendMessage(tabs[0].id, request).then(function (response) {
sendResponse(response);
});
});
return true;
case 'githubTab':
chrome.tabs.create({ url: 'github.com/SethBurkart123/EvenBetterSEQTA' });
browser.tabs.create({ url: 'github.com/SethBurkart123/EvenBetterSEQTA' });
break;
case 'setDefaultStorage':
@@ -218,12 +221,13 @@ const DefaultValues = {
function SetStorageValue(object) {
for (var i in object) {
chrome.storage.local.set({ [i]: object[i] });
browser.storage.local.set({ [i]: object[i] });
}
}
function UpdateCurrentValues() {
chrome.storage.local.get(null, function (items) {
const result = browser.storage.local.get()
function open (items) {
var CurrentValues = items;
const NewValue = Object.assign({}, DefaultValues, CurrentValues);
@@ -253,11 +257,13 @@ function UpdateCurrentValues() {
}
SetStorageValue(NewValue);
});
}
result.then(open, onError)
}
function migrateOldStorage() {
chrome.storage.local.get(null, function (items) {
const result = browser.storage.local.get()
function open (items) {
let shouldUpdate = false; // Flag to check if there is anything to update
// Check for the old "Name" field and convert it to "name"
@@ -283,18 +289,18 @@ function migrateOldStorage() {
// If there"s something to update, set the new values in storage
if (shouldUpdate) {
chrome.storage.local.set({ shortcuts: items.shortcuts }, function() {
console.log('Migration completed.');
});
const setting = browser.storage.local.set({ shortcuts: items.shortcuts })
setting.then(() => console.log('Migration Completed.'))
}
});
}
result.then(open, onError)
}
chrome.runtime.onInstalled.addListener(function (event) {
chrome.storage.local.remove(['justupdated']);
browser.runtime.onInstalled.addListener(function (event) {
browser.storage.local.remove(['justupdated']);
UpdateCurrentValues();
if ( event.reason == 'install', event.reason == 'update' ) {
chrome.storage.local.set({ justupdated: true });
browser.storage.local.set({ justupdated: true });
migrateOldStorage();
}
});