-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
69 lines (59 loc) · 2.55 KB
/
background.js
File metadata and controls
69 lines (59 loc) · 2.55 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
// Function to get user-configured or managed settings
async function getUserConfig() {
return new Promise((resolve) => {
chrome.storage.managed.get(['tenantID', 'policyGUID'], (managedData) => {
if (chrome.runtime.lastError) {
// Handle error, e.g., managed storage not available
resolve({});
return;
}
chrome.storage.sync.get(['tenantID', 'policyGUID'], (syncData) => {
if (chrome.runtime.lastError) {
// Handle error, e.g., sync storage not available
resolve({});
return;
}
// Priority: Use managed settings if available, else use user-configured settings
const userConfig = managedData.tenantID && managedData.policyGUID ? managedData : syncData;
resolve(userConfig);
});
});
});
}
// Function to create rules for the specified domains
async function createRules() {
const domains = ["login.microsoftonline.com", "login.microsoft.com", "login.windows.net", "login.live.com"];
const ids = [5001, 5002, 5003, 5004];
// Retrieve the user's configuration from storage.sync
const userConfig = await getUserConfig();
const accessHeaderValue = `${userConfig.tenantID}:${userConfig.policyGUID}`;
// Create or update rules for each domain with user-configured or managed settings
const newRules = domains.map((domain, index) => {
const id = ids[index];
// const accessHeaderValue = `${userConfig.tenantID}:${userConfig.policyGUID}`;
return {
id: id,
priority: 1,
action: {
type: "modifyHeaders",
requestHeaders: [
{ header: "sec-Restrict-Tenant-Access-Policy", operation: "set", value: accessHeaderValue },
],
},
condition: { urlFilter: domain, resourceTypes: ["main_frame"] },
};
});
// Retrieve existing rules
const existingRules = await chrome.declarativeNetRequest.getDynamicRules();
// Remove existing rules with the same IDs
const ruleIdsToRemove = existingRules.map((rule) => rule.id);
await chrome.declarativeNetRequest.updateDynamicRules({ removeRuleIds: ruleIdsToRemove });
if (accessHeaderValue !== ':' && accessHeaderValue !== 'undefined:undefined') {
// Add the new rules
await chrome.declarativeNetRequest.updateDynamicRules({ addRules: newRules });
}
}
// Call the function to create or update rules on startup
chrome.runtime.onStartup.addListener(createRules);
// Call the function to create or update rules on tab creation
chrome.tabs.onCreated.addListener(createRules);