-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbackground.js
More file actions
101 lines (82 loc) · 3.45 KB
/
Copy pathbackground.js
File metadata and controls
101 lines (82 loc) · 3.45 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
chrome.runtime.onInstalled.addListener(() => {
// Extension installed or updated logic here
console.log('Extension installed or updated.');
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// skip urls like "chrome://" to avoid extension error
if (tab.url?.startsWith('chrome://')) return undefined;
if (changeInfo.status === 'complete') {
chrome.scripting.executeScript({
target: { tabId: tabId },
function: checkAndLogin,
args: [tabId]
});
}
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'closeTab') {
chrome.tabs.remove(message.tabId);
}
});
async function checkAndLogin(tabId) {
try {
const currentURL = window.location.href;
// Check if the current tab URL contains 'awsapps.com' and 'start' or 'device.sso.*.awamazon.com'
if (isUserConsentPage(currentURL) || isSSOPage(currentURL)) {
const loginButton = await waitForElement(() => document.getElementById('cli_verification_btn'));
if (loginButton) {
// Click the 'cli_verification_btn'
loginButton.click();
console.log('Clicked on cli_verification_btn');
}
const loginButtonAfterRedirection = await waitForElement(() => document.getElementById('cli_login_button'));
// check if the 'cli_login_button' exists
if (loginButtonAfterRedirection) {
// Click the 'cli_login_button'
loginButtonAfterRedirection.click();
console.log('Clicked on cli_login_button new page');
chrome.runtime.sendMessage({ action: 'closeTab', tabId: tabId });
}
}
// Check if the current tab URL contains 'awsapps.com' and 'start'
// this function is used if the button id changes and will find the english text in the button to allow access
if (isNewUserConsentPage(currentURL)) {
const allowButton = await waitForElement(() => {
const button = document.querySelector('button[data-testid="allow-access-button"]');
if (button?.textContent.toLowerCase().includes('allow')) {
return button;
}
});
if (allowButton) {
allowButton.click();
console.log('Clicked on Allow access button');
await waitForElement(() => document.querySelector('.awsui-context-alert')?.textContent.toLocaleLowerCase().startsWith('request approved'));
chrome.runtime.sendMessage({ action: 'closeTab', tabId: tabId });
}
}
} catch (error) {
console.error('An error occurred:', error);
}
function isSSOPage(url) {
return /^https:\/\/device\.sso\.[^.]*\.amazonaws\.com/.test(url);
}
function isUserConsentPage(url) {
return /^https:\/\/[^.]+\.awsapps\.com\/start/.test(url);
}
function isNewUserConsentPage(url) {
return isUserConsentPage(url);
}
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function waitForElement(fn, timeout = 1500) {
const startTime = Date.now();
do {
const result = fn();
if (result) {
return result;
}
await wait(100);
} while (Date.now() - startTime < timeout);
}
}