-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadminUserInfo.js
More file actions
114 lines (99 loc) · 3.41 KB
/
adminUserInfo.js
File metadata and controls
114 lines (99 loc) · 3.41 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
const FILTERS_TEXT = "Active users\nPending invites\nRemoved users";
const FILTERS_CONTAINER_CLASS = "statusFilters--SMNEn98faE";
const CHECKBOX_CLASS = "ui-components-1avyp1d";
const CHECKBOX_STATE_ATTRIBUTE_KEY = 'data-state'
const CHECKBOX_STATE_ATTRIBUTE_VALUE_WHEN_CHECKED = 'checked'
const NAVIGATION_CONTAINER_CLASS = "container--biH1xFvLjC";
function isFiltersOrderStillCorrect(filters) {
return filters.innerText === FILTERS_TEXT;
}
function isCheckboxChecked(checkboxElement) {
const checkedAttributeValue = checkboxElement.getAttribute(CHECKBOX_STATE_ATTRIBUTE_KEY);
return CHECKBOX_STATE_ATTRIBUTE_VALUE_WHEN_CHECKED === checkedAttributeValue;
}
/**
* Setup filters to 'Active users' only.
*/
function setupFilters() {
const filterContainers = document.getElementsByClassName(
FILTERS_CONTAINER_CLASS
);
if (filterContainers.length === 0) return;
const filters = filterContainers[0];
if (!isFiltersOrderStillCorrect(filters)) return;
const checkboxCollection = filters.getElementsByClassName(
CHECKBOX_CLASS
);
const activeUsersCheckbox = checkboxCollection[0];
if (!isCheckboxChecked(activeUsersCheckbox)) {
activeUsersCheckbox.click();
}
const pendingInvitesCheckbox = checkboxCollection[1];
if (isCheckboxChecked(pendingInvitesCheckbox)) {
pendingInvitesCheckbox.click();
}
const removedUsersCheckbox = checkboxCollection[2];
if (isCheckboxChecked(removedUsersCheckbox)) {
removedUsersCheckbox.click();
}
}
function tryToNavigateToNextPage() {
const navigationContainers = document.getElementsByClassName(
NAVIGATION_CONTAINER_CLASS
);
if (navigationContainers.length === 0) {
console.error("Could not locate the navigation buttons.");
return false;
}
const topNavigation = navigationContainers[0];
const nextPageButton = topNavigation.getElementsByTagName("button")[1];
if (nextPageButton.disabled) {
console.log("This was already last page.");
return false;
}
nextPageButton.click();
console.log("Navigated to next page.");
return true;
}
function getCellsText(entry) {
return entry.innerText
.split("\t")
.flatMap((el) => el.split("\n"))
.flatMap((el) => (el !== "" ? el : []));
}
function scrapCurrentPageUsersInfo() {
const [_tableHeader, ...entries] = document.getElementsByTagName("tr");
const entriesTexts = entries.map(getCellsText);
console.log(`Found info for ${entries.length} users on this page.`);
return entriesTexts;
}
async function copyUsersToClipboard() {
let usersInfo = [];
let shouldScrapCurrentPage = true;
do {
usersInfo = usersInfo.concat(scrapCurrentPageUsersInfo());
const couldNavigateToNextPage = tryToNavigateToNextPage();
shouldScrapCurrentPage = couldNavigateToNextPage;
} while (shouldScrapCurrentPage);
const formattedEntries = usersInfo.map((e) => e.join("\t")).join("\n");
await navigator.clipboard.writeText(formattedEntries);
console.log(`Copied info for ${usersInfo.length} users`);
return usersInfo.length;
}
function copyDataWhenFocused() {
return new Promise((resolve, reject) => {
const _asyncCopyFn = async () => {
try {
await copyUsersToClipboard();
resolve();
} catch (e) {
reject(e);
}
window.removeEventListener("focus", _asyncCopyFn);
};
window.addEventListener("focus", _asyncCopyFn);
console.info("Click back into Dashlane users page NOW!");
});
}
setupFilters();
copyDataWhenFocused();