-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
183 lines (160 loc) · 5.86 KB
/
background.js
File metadata and controls
183 lines (160 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*By David wyatt, https://www.linkedin.com/in/wyattdave/ , under license: https://github.com/wyattdave/my-power-platform/blob/main/LICENSE*/
const sApiUrlFlowEnvir="https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments?$expand=properties%2Fpermissions&api-version=2016-11-01";
const sApiUrlApp="https://<tenantID>.tenant.api.powerplatform.com/powerapps/environments?api-version=1";
// All known Dynamics 365 region URL suffixes
const DYNAMICS_REGIONS = [
{ region: "USA", url: "crm.dynamics.com" },
{ region: "DEU", url: "crm.microsoftdynamics.de" },
{ region: "SAM", url: "crm2.dynamics.com" },
{ region: "CAN", url: "crm3.dynamics.com" },
{ region: "EUR", url: "crm4.dynamics.com" },
{ region: "APJ", url: "crm5.dynamics.com" },
{ region: "OCE", url: "crm6.dynamics.com" },
{ region: "JPN", url: "crm7.dynamics.com" },
{ region: "IND", url: "crm8.dynamics.com" },
{ region: "GCC", url: "crm9.dynamics.com" },
{ region: "GCC High", url: "crm.microsoftdynamics.us" },
{ region: "GBR", url: "crm11.dynamics.com" },
{ region: "FRA", url: "crm12.dynamics.com" },
{ region: "ZAF", url: "crm14.dynamics.com" },
{ region: "UAE", url: "crm15.dynamics.com" },
{ region: "GER", url: "crm16.dynamics.com" },
{ region: "CHE", url: "crm17.dynamics.com" },
{ region: "CHN", url: "crm.dynamics.cn" },
{ region: "NOR", url: "crm19.dynamics.com" },
{ region: "SGP", url: "crm20.dynamics.com" },
{ region: "KOR", url: "crm21.dynamics.com" }
];
// Default token state — persisted to chrome.storage.local so
// tokens survive Manifest V3 service-worker restarts.
// regionTokens is a map: { "crm11.dynamics.com": "Bearer ...", ... }
const DEFAULT_TOKENS = {
regionTokens: {},
flow: "",
app: "",
envirs: "",
url: "",
token: "",
user: "",
bEnvir: false,
lastUpdated: 0
};
// ---------- helpers ----------
function saveTokens(tokens) {
chrome.storage.local.set({ mppTokens: tokens });
}
function loadTokens() {
return new Promise((resolve) => {
chrome.storage.local.get("mppTokens", (result) => {
resolve(result.mppTokens || JSON.parse(JSON.stringify(DEFAULT_TOKENS)));
});
});
}
/** Match a full URL to one of the known region suffixes */
function detectRegion(requestUrl) {
for (const r of DYNAMICS_REGIONS) {
if (requestUrl.includes(r.url)) return r;
}
return null;
}
// ---------- open popup ----------
chrome.action.onClicked.addListener(() => {
chrome.windows.create({
url: chrome.runtime.getURL("index.html"),
type: "popup",
state: "maximized"
});
});
// ---------- respond to popup requests ----------
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
loadTokens().then((tokens) => {
sendResponse({
regionTokens: tokens.regionTokens || {},
flow: tokens.flow,
app: tokens.app,
envirs: tokens.envirs,
url: tokens.url,
token: tokens.token,
user: tokens.user
});
});
return true; // required for async sendResponse
});
// ---------- intercept requests & capture tokens ----------
chrome.webRequest.onBeforeSendHeaders.addListener(
(details) => {
const url = details.url;
const bData = url.includes(".dynamics.com/api/data/v9.") || url.includes(".microsoftdynamics.de/api/data/v9.") || url.includes(".microsoftdynamics.us/api/data/v9.") || url.includes(".dynamics.cn/api/data/v9.");
const bFlow = url.includes("api.flow.microsoft.com/") && !url.includes("scopes/admin/");
const bApp = url.includes("environment.api.powerplatform.com");
const bTenant = url.includes(".tenant.api.powerplatform.com/");
const bToken = url.includes("make.powerapps.com/api/exchangeToken");
if (!(bFlow || bData || bApp || bTenant || bToken)) return;
const authHeader = details.requestHeaders.find(
(h) => h.name.toLowerCase() === "authorization"
);
if (!authHeader) return;
const authValue = authHeader.value;
loadTokens().then((tokens) => {
let changed = false;
if (!tokens.regionTokens) tokens.regionTokens = {};
if (bFlow && tokens.flow !== authValue) {
tokens.flow = authValue;
tokens.envirs = authValue;
tokens.url = sApiUrlFlowEnvir;
tokens.bEnvir = true;
changed = true;
}
// Store per-region dataverse token
if (bData) {
const region = detectRegion(url);
if (region && tokens.regionTokens[region.url] !== authValue) {
tokens.regionTokens[region.url] = authValue;
changed = true;
}
}
if (bApp && tokens.app !== authValue) {
tokens.app = authValue;
changed = true;
}
if (bTenant) {
const sTenantId = url.split(".tenant.api")[0].split("https://")[1];
if (!tokens.bEnvir) {
tokens.bEnvir = true;
tokens.envirs = authValue;
tokens.url = sApiUrlApp.replace("<tenantID>", sTenantId);
changed = true;
}
}
if (bToken && tokens.token !== authValue) {
tokens.token = authValue;
changed = true;
}
if (url.includes("api/data/v9.2/systemusers(")) {
const newUser = url.split("/Microsoft.Dynamics")[0];
if (tokens.user !== newUser) {
tokens.user = newUser;
changed = true;
}
}
if (changed) {
tokens.lastUpdated = Date.now();
saveTokens(tokens);
}
});
},
{
urls: [
"https://api.flow.microsoft.com/*",
"https://*.api.flow.microsoft.com/*",
"https://*.dynamics.com/*",
"https://*.microsoftdynamics.de/*",
"https://*.microsoftdynamics.us/*",
"https://*.dynamics.cn/*",
"https://*.environment.api.powerplatform.com/*",
"https://*.tenant.api.powerplatform.com/*",
"https://make.powerapps.com/*"
]
},
["requestHeaders", "extraHeaders"]
);