125 lines
3.5 KiB
JavaScript
125 lines
3.5 KiB
JavaScript
function createPlatformContentMessages({
|
|
messageTypes,
|
|
requestCache,
|
|
environment
|
|
}) {
|
|
const { browser, chrome } = environment;
|
|
function sendRuntimeMessage(message) {
|
|
return requestCache ? requestCache.send(message, sendUncached) : sendUncached(message);
|
|
}
|
|
function sendUncached(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 };
|
|
}
|