Implementação da chamada para obtenção do Resource Current User

This commit is contained in:
Paulo Porto
2026-04-30 17:24:19 -03:00
parent ca839345bc
commit 655d60b5ff
3 changed files with 157 additions and 23 deletions

View File

@@ -7,6 +7,8 @@ const COMCIP_CLIENT_ID_PROBE_URL =
`${COMCIP_ORIGIN}/ic/builder/rt/oalset_semc/live;profile=PROD/services/auth/1.1/proxy/oalsetSeaaSOKECustomRestAPI/uri/https/gxpap.oracle.com/oalcrm/service/set/seaas/crm/countries`;
const COMCIP_TAB_URL_PATTERN =
`${COMCIP_ORIGIN}/ic/builder/rt/oalset_semc/live*`;
const COMCIP_TIME_ENTRY_TAB_URL_PATTERN =
`${COMCIP_ORIGIN}/ic/builder/rt/oalset_timeentrymobile/live*`;
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (
@@ -36,18 +38,35 @@ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
});
async function requestComcipFromPage({ url, method = "POST", payload, headers = {} }) {
const target = await getComcipTab();
const route = getComcipRoute(url);
const activeTab = await captureActiveTab();
const target = await getComcipTab(route, activeTab);
try {
await restoreActiveTab(activeTab);
await waitForComcipTabReady(target.tab.id);
await waitForComcipSessionReady(target.tab.id, headers);
await waitForComcipSessionReady(target.tab.id, headers, route);
let response = await executeComcipFetch(target.tab.id, url, method, payload, headers);
let response = await executeComcipFetch(
target.tab.id,
url,
method,
payload,
headers,
route
);
if (isAuthorizationFailure(response)) {
await delay(1500);
await waitForComcipSessionReady(target.tab.id, headers);
response = await executeComcipFetch(target.tab.id, url, method, payload, headers);
await waitForComcipSessionReady(target.tab.id, headers, route);
response = await executeComcipFetch(
target.tab.id,
url,
method,
payload,
headers,
route
);
}
return response;
@@ -55,11 +74,31 @@ async function requestComcipFromPage({ url, method = "POST", payload, headers =
if (target.created && target.tab.id) {
await chrome.tabs.remove(target.tab.id).catch(() => {});
}
await restoreActiveTab(activeTab);
}
}
async function getComcipTab() {
const tabs = await chrome.tabs.query({ url: COMCIP_TAB_URL_PATTERN });
function getComcipRoute(requestUrl) {
const normalizedUrl = String(requestUrl || "");
if (normalizedUrl.includes("/ic/builder/rt/oalset_timeentrymobile/live")) {
return {
appUrl: normalizedUrl,
clientIdProbeUrl: normalizedUrl,
tabPattern: COMCIP_TIME_ENTRY_TAB_URL_PATTERN,
};
}
return {
appUrl: COMCIP_APP_URL,
clientIdProbeUrl: COMCIP_CLIENT_ID_PROBE_URL,
tabPattern: COMCIP_TAB_URL_PATTERN,
};
}
async function getComcipTab(route, activeTab) {
const tabs = await chrome.tabs.query({ url: route.tabPattern });
const existing = tabs.find((tab) => tab.id && !tab.discarded);
if (existing) {
@@ -70,9 +109,11 @@ async function getComcipTab() {
}
const created = await chrome.tabs.create({
url: COMCIP_APP_URL,
url: route.appUrl,
active: false,
windowId: activeTab?.windowId,
});
await restoreActiveTab(activeTab);
return {
tab: created,
@@ -80,6 +121,31 @@ async function getComcipTab() {
};
}
async function captureActiveTab() {
const [activeTab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});
if (!activeTab?.id) {
return null;
}
return {
id: activeTab.id,
windowId: activeTab.windowId,
};
}
async function restoreActiveTab(activeTab) {
if (!activeTab?.id) {
return;
}
await chrome.windows.update(activeTab.windowId, { focused: true }).catch(() => {});
await chrome.tabs.update(activeTab.id, { active: true }).catch(() => {});
}
async function waitForComcipTabReady(tabId) {
const startedAt = Date.now();
@@ -98,12 +164,12 @@ async function waitForComcipTabReady(tabId) {
throw new Error("Timed out while loading the COMCIP origin page.");
}
async function waitForComcipSessionReady(tabId, headers = {}) {
async function waitForComcipSessionReady(tabId, headers = {}, route) {
const startedAt = Date.now();
let lastStatus = "";
while (Date.now() - startedAt < 60000) {
const probe = await executeComcipProbe(tabId, headers).catch((error) => ({
const probe = await executeComcipProbe(tabId, headers, route).catch((error) => ({
ok: false,
status: 0,
error: error instanceof Error ? error.message : String(error),
@@ -122,11 +188,11 @@ async function waitForComcipSessionReady(tabId, headers = {}) {
throw new Error(`Timed out while waiting for COMCIP authenticated session (${lastStatus}).`);
}
async function executeComcipProbe(tabId, headers = {}) {
async function executeComcipProbe(tabId, headers = {}, route) {
const [injectionResult] = await chrome.scripting.executeScript({
target: { tabId },
world: "MAIN",
args: [COMCIP_CLIENT_ID_PROBE_URL, headers || {}],
args: [route?.clientIdProbeUrl || COMCIP_CLIENT_ID_PROBE_URL, headers || {}],
func: async (probeUrl, sourceHeaders) => {
const APP_VERSION = "version_1754044416761";
const response = await fetch(probeUrl, {
@@ -160,15 +226,25 @@ async function executeComcipProbe(tabId, headers = {}) {
return injectionResult.result;
}
async function executeComcipFetch(tabId, requestUrl, method, payload, headers) {
async function executeComcipFetch(tabId, requestUrl, method, payload, headers, route) {
const [injectionResult] = await chrome.scripting.executeScript({
target: { tabId },
world: "MAIN",
args: [requestUrl, method || "POST", payload || null, headers || {}],
func: async (requestUrl, requestMethod, requestPayload, sourceHeaders) => {
args: [
requestUrl,
method || "POST",
payload || null,
headers || {},
route?.clientIdProbeUrl || COMCIP_CLIENT_ID_PROBE_URL,
],
func: async (
requestUrl,
requestMethod,
requestPayload,
sourceHeaders,
clientIdProbeUrl
) => {
const APP_VERSION = "version_1754044416761";
const CLIENT_ID_PROBE_URL =
`${window.location.origin}/ic/builder/rt/oalset_semc/live;profile=PROD/services/auth/1.1/proxy/oalsetSeaaSOKECustomRestAPI/uri/https/gxpap.oracle.com/oalcrm/service/set/seaas/crm/countries`;
function parseResponseText(text) {
if (!text) {
@@ -206,7 +282,7 @@ async function executeComcipFetch(tabId, requestUrl, method, payload, headers) {
async function resolveAppBuilderClientId() {
try {
const response = await fetch(CLIENT_ID_PROBE_URL, {
const response = await fetch(clientIdProbeUrl, {
method: "GET",
credentials: "include",
cache: "no-store",