General source code refactoring
This commit is contained in:
190
src/platform/background-api.js
Normal file
190
src/platform/background-api.js
Normal file
@@ -0,0 +1,190 @@
|
||||
function createPlatformBackgroundApi({
|
||||
runtimeApi,
|
||||
environment
|
||||
}) {
|
||||
const { browser, setTimeout } = environment;
|
||||
function supportsFirefoxCookieStoreTabs() {
|
||||
return Boolean(
|
||||
runtimeApi.runtime
|
||||
&& typeof runtimeApi.runtime.getBrowserInfo === "function"
|
||||
);
|
||||
}
|
||||
|
||||
function tabsCreate(details) {
|
||||
if (runtimeApi.tabs.create.length <= 1) {
|
||||
return runtimeApi.tabs.create(details);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
runtimeApi.tabs.create(details, (tab) => {
|
||||
const lastError = runtimeApi.runtime.lastError;
|
||||
|
||||
if (lastError) {
|
||||
reject(new Error(lastError.message));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(tab);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function tabsRemove(tabId) {
|
||||
if (runtimeApi.tabs.remove.length <= 1) {
|
||||
return runtimeApi.tabs.remove(tabId);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
runtimeApi.tabs.remove(tabId, () => {
|
||||
const lastError = runtimeApi.runtime.lastError;
|
||||
|
||||
if (lastError) {
|
||||
reject(new Error(lastError.message));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function tabsSendMessage(tabId, message, options) {
|
||||
if (typeof browser !== "undefined") {
|
||||
return options
|
||||
? runtimeApi.tabs.sendMessage(tabId, message, options)
|
||||
: runtimeApi.tabs.sendMessage(tabId, message);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const callback = (response) => {
|
||||
const lastError = runtimeApi.runtime.lastError;
|
||||
|
||||
if (lastError) {
|
||||
reject(new Error(lastError.message));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(response);
|
||||
};
|
||||
|
||||
if (options) {
|
||||
runtimeApi.tabs.sendMessage(tabId, message, options, callback);
|
||||
} else {
|
||||
runtimeApi.tabs.sendMessage(tabId, message, callback);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function storageLocalGet(key) {
|
||||
if (typeof browser !== "undefined") {
|
||||
return runtimeApi.storage.local.get(key);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
runtimeApi.storage.local.get(key, (result) => {
|
||||
const lastError = runtimeApi.runtime.lastError;
|
||||
|
||||
if (lastError) {
|
||||
reject(new Error(lastError.message));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function storageLocalSet(value) {
|
||||
if (typeof browser !== "undefined") {
|
||||
return runtimeApi.storage.local.set(value);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
runtimeApi.storage.local.set(value, () => {
|
||||
const lastError = runtimeApi.runtime.lastError;
|
||||
|
||||
if (lastError) {
|
||||
reject(new Error(lastError.message));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function wait(durationMs) {
|
||||
return new Promise((resolve) => setTimeout(resolve, durationMs));
|
||||
}
|
||||
|
||||
function cookiesGetAll(details) {
|
||||
if (runtimeApi.cookies.getAll.length <= 1) {
|
||||
return runtimeApi.cookies.getAll(details);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
runtimeApi.cookies.getAll(details, (cookies) => {
|
||||
const lastError = runtimeApi.runtime.lastError;
|
||||
|
||||
if (lastError) {
|
||||
reject(new Error(lastError.message));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(cookies);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function cookiesGet(details) {
|
||||
const cleanDetails = removeUndefinedValues(details);
|
||||
|
||||
if (runtimeApi.cookies.get.length <= 1) {
|
||||
return runtimeApi.cookies.get(cleanDetails);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
runtimeApi.cookies.get(cleanDetails, (cookie) => {
|
||||
const lastError = runtimeApi.runtime.lastError;
|
||||
|
||||
if (lastError) {
|
||||
reject(new Error(lastError.message));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(cookie);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function cookiesGetAllCookieStores() {
|
||||
if (!runtimeApi.cookies.getAllCookieStores) {
|
||||
return Promise.resolve([{ id: undefined }]);
|
||||
}
|
||||
|
||||
if (runtimeApi.cookies.getAllCookieStores.length === 0) {
|
||||
return runtimeApi.cookies.getAllCookieStores();
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
runtimeApi.cookies.getAllCookieStores((stores) => {
|
||||
const lastError = runtimeApi.runtime.lastError;
|
||||
|
||||
if (lastError) {
|
||||
reject(new Error(lastError.message));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(stores);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function removeUndefinedValues(details) {
|
||||
return Object.fromEntries(
|
||||
Object.entries(details).filter((entry) => entry[1] !== undefined)
|
||||
);
|
||||
}
|
||||
|
||||
return { supportsFirefoxCookieStoreTabs, tabsCreate, tabsRemove, tabsSendMessage, storageLocalGet, storageLocalSet, wait, cookiesGetAll, cookiesGet, cookiesGetAllCookieStores, removeUndefinedValues };
|
||||
}
|
||||
120
src/platform/content-messages.js
Normal file
120
src/platform/content-messages.js
Normal file
@@ -0,0 +1,120 @@
|
||||
function createPlatformContentMessages({
|
||||
messageTypes,
|
||||
environment
|
||||
}) {
|
||||
const { browser, chrome } = environment;
|
||||
function sendRuntimeMessage(message) {
|
||||
const runtimeApi = typeof browser !== "undefined" ? browser : chrome;
|
||||
|
||||
if (!runtimeApi || !runtimeApi.runtime || !runtimeApi.runtime.sendMessage) {
|
||||
return Promise.reject(new Error("Runtime messaging API unavailable."));
|
||||
}
|
||||
|
||||
if (typeof browser !== "undefined") {
|
||||
return runtimeApi.runtime.sendMessage(message);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
runtimeApi.runtime.sendMessage(message, (response) => {
|
||||
const lastError = runtimeApi.runtime.lastError;
|
||||
|
||||
if (lastError) {
|
||||
reject(new Error(lastError.message));
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(response);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getXsrfTokenFromExtensionCookies() {
|
||||
const runtimeApi = typeof browser !== "undefined" ? browser : chrome;
|
||||
|
||||
if (!runtimeApi || !runtimeApi.runtime || !runtimeApi.runtime.sendMessage) {
|
||||
return Promise.resolve({
|
||||
token: "",
|
||||
cookieName: "",
|
||||
source: "unavailable cookies API",
|
||||
error: "Runtime messaging API unavailable."
|
||||
});
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
runtimeApi.runtime.sendMessage({
|
||||
type: messageTypes.getXsrfToken
|
||||
}, (response) => {
|
||||
const lastError = runtimeApi.runtime.lastError;
|
||||
|
||||
if (lastError) {
|
||||
resolve({
|
||||
token: "",
|
||||
cookieName: "",
|
||||
source: "cookies API",
|
||||
matchedCookieNames: [],
|
||||
lookupDetails: [],
|
||||
error: lastError.message
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
resolve({
|
||||
token: response && response.ok ? response.token : "",
|
||||
cookieName: response && response.ok ? response.cookieName : "",
|
||||
source: "cookies API",
|
||||
matchedCookieNames: response && response.matchedCookieNames ? response.matchedCookieNames : [],
|
||||
lookupDetails: response && response.lookupDetails ? response.lookupDetails : [],
|
||||
error: response && response.error ? response.error : ""
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function refreshXsrfCookie(reason) {
|
||||
const runtimeApi = typeof browser !== "undefined" ? browser : chrome;
|
||||
|
||||
if (!runtimeApi || !runtimeApi.runtime || !runtimeApi.runtime.sendMessage) {
|
||||
return Promise.resolve({
|
||||
ok: false,
|
||||
refreshed: false,
|
||||
cookieName: "",
|
||||
durationMs: 0,
|
||||
reason,
|
||||
error: "Runtime messaging API unavailable."
|
||||
});
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
runtimeApi.runtime.sendMessage({
|
||||
type: messageTypes.refreshXsrfCookie
|
||||
}, (response) => {
|
||||
const lastError = runtimeApi.runtime.lastError;
|
||||
|
||||
if (lastError) {
|
||||
resolve({
|
||||
ok: false,
|
||||
refreshed: false,
|
||||
cookieName: "",
|
||||
durationMs: 0,
|
||||
reason,
|
||||
error: lastError.message
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
resolve({
|
||||
...(response || {
|
||||
ok: false,
|
||||
refreshed: false,
|
||||
cookieName: "",
|
||||
durationMs: 0,
|
||||
error: "Empty refresh response."
|
||||
}),
|
||||
reason
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return { sendRuntimeMessage, getXsrfTokenFromExtensionCookies, refreshXsrfCookie };
|
||||
}
|
||||
Reference in New Issue
Block a user