-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
323 lines (275 loc) · 9.03 KB
/
utils.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Utils
export const $ = (s) => document.querySelector(s);
export function anonymize(shouldAnonymize = false) {
console.log("anonymize", shouldAnonymize);
return new Promise(async (resolve, reject) => {
try {
let { url } = await getActiveTab();
if (url.includes("azure")) {
await anonymizeAzureInfo(shouldAnonymize);
} else if (url.includes("github")) {
await anonymizeGitHubInfo();
}
// defer microtask
setTimeout(() => resolve(), 10);
} catch (error) {
reject(error);
}
});
}
export async function DOMsave() {
console.log("save DOM");
let code = function () {
if (document.querySelector("#MS_MOONSHOT_CONTAINER")) return;
let parent = document.body;
let azureScrollTopContainer = document.querySelector(".fxs-blade-content-wrapper:not(.fxs-menu-show-search)");
let azureScrollLeftContainer = document.querySelector(
".fxs-journey-layout.fxs-stacklayout.fxs-stacklayout-horizontal"
);
let scrollY = azureScrollTopContainer ? azureScrollTopContainer.scrollTop : 0;
let scrollX = azureScrollLeftContainer ? azureScrollLeftContainer.scrollLeft : 0;
let div = document.createElement("div");
div.id = "MS_MOONSHOT_CONTAINER";
div.style.cssText = `
display: block;
position: absolute;
left: 0;
top: 0;
right: 0;
height: 100%;
width: 100%;
z-index: 9999999;`;
div.appendChild(parent.cloneNode(true));
parent.appendChild(div);
if (azureScrollTopContainer) {
document
.querySelector("#MS_MOONSHOT_CONTAINER")
.querySelector(".fxs-blade-content-wrapper:not(.fxs-menu-show-search)").scrollTop = scrollY;
}
if (azureScrollLeftContainer) {
document
.querySelector("#MS_MOONSHOT_CONTAINER")
.querySelector(".fxs-journey-layout.fxs-stacklayout.fxs-stacklayout-horizontal").scrollLeft = scrollX;
}
};
return await exec(code);
}
export function delay(ms) {
console.log("wait", ms, "ms");
return new Promise((resolve) => setTimeout(resolve, ms));
}
export async function DOMrestore() {
console.log("restore DOM");
let code = function () {
let bodyElement = document.body;
bodyElement.removeChild(document.querySelector("#MS_MOONSHOT_CONTAINER"));
};
return await exec(code);
}
export async function anonymizeAzureInfo(shouldAnonymize) {
console.log("anonymizeAzureInfo", shouldAnonymize);
let code = function () {
let cssFilter = $$shouldAnonymize ? "blur(2.5px)" : "blur(0)";
let $ = (s) => document.querySelector(s);
let $$ = (s) => [...document.querySelectorAll(s)];
// user info
$(".fxs-avatarmenu-tenant-image").src =
"https://portal.azure.com/Content/static/MsPortalImpl/AvatarMenu/AvatarMenu_defaultAvatarSmall.png";
$(".fxs-avatarmenu-username").innerText = "user@contoso.com";
// subscription ID
[...$$(".fxc-essentials-value, .fxc-summary-item-value")]
.filter((el) => /\w{8}\-\w{4}\-\w{4}\-\w{4}\-\w{12}/.test(el.textContent))
.map((el) => (el.style.filter = cssFilter));
// secrets to hide
let selectors = [
// subscription names
`[aria-label="Subscription selector"]`,
`.fxs-part-assetname`,
`[aria-labelledby$="siteSubscriptionValue"]`,
// SAS
`input[title*="&sig="]`,
// Storage Access keys
`input[aria-label*="Key"]`,
// Storage connection string
`input[aria-label*="Connection string"]`,
];
$$(selectors.join(",")).map((el) => (el.style.filter = cssFilter));
// subscription names in details screen
try {
[
$('label[title="Subscription"]').parentElement.parentElement.querySelector("a.msportalfx-text-primary"),
...$$(".fxc-pill-content-value"),
].map((el) => (el.style.filter = cssFilter));
} catch (error) {}
};
return await exec(code, [{ $$shouldAnonymize: shouldAnonymize }]);
}
export async function anonymizeGitHubInfo() {
console.log("anonymizeGitHubInfo");
let code = function () {
let $ = (s) =>
document.querySelector("#MS_MOONSHOT_CONTAINER").querySelector(s) || document.createElement("script");
let $$ = (s) =>
[...document.querySelector("#MS_MOONSHOT_CONTAINER").querySelectorAll(s)] || document.createElement("script");
let ghAvatar =
"https://avatars3.githubusercontent.com/u/62345156?s=460&u=885c79b21d3f01e7d2caff6d721aff9c304257c4&v=4";
let ghUsername = "staticwebdev";
// set all user's profile avatars with staticwebdev's avatar
[
...$$(
[
".avatar.avatar-user.width-full",
".avatar.avatar-user",
"a.u-photo img",
"a.avatar-user > img",
".Header-link > img",
"a.author > img",
"a.commit-author > img",
"a[rel='author'] > img",
].join(",")
),
].map((el) => {
el.src = ghAvatar;
});
// set all user's github usernames with staticwebdev's username
[...$$("a.author, a.commit-author, a[rel='author']")].map((el) => {
el.innerText = ghUsername;
});
// hide personal info
[
...$$(
[
// user's bio
".user-profile-bio",
// user's readme
".Box-body.p-4:nth-child(1)",
// the Sponsors dashboard button on the profile page
"#sponsorships-profile-button",
// the Edit profile button on the profile page
".js-profile-editable-area button.js-profile-editable-edit-button",
".js-profile-editable-area .user-profile-bio",
".js-profile-editable-area li[itemprop]:not([itemprop='homeLocation'])",
// The Organizations section on the profile page
// sponsors container on the profile page
"div.border-top.pt-3.mt-3.clearfix.hide-sm.hide-md",
// User's status on the profile page under the avatar
".user-status-container",
].join(",")
),
].map((el) => {
el.style.cssText = `display: none !important;`;
});
// set user's location
$(".js-profile-editable-area li[itemprop='homeLocation'] .p-label").innerHTML = "The Internet";
// set user's github username
[...$$("h1.vcard-names")].map((el) => {
el.innerHTML =
`
<span class="p-nickname vcard-username d-block" itemprop="additionalName">` +
ghUsername +
`</span>`;
});
};
return await exec(code);
}
export async function captureScreenshot(canDownload = false) {
console.log("capture screenshot");
return new Promise(async (resolve, reject) => {
let { windowId } = await getActiveTab();
chrome.tabs.captureVisibleTab(
windowId,
{
format: "png",
quality: 100,
},
function (dataUrl) {
if (canDownload) {
let a = document.createElement("a");
a.href = dataUrl;
a.setAttribute("download", `screenshot-${Date.now()}.png`);
a.click();
}
resolve(dataUrl);
}
);
});
}
export function getActiveTab() {
return new Promise((resolve, reject) => {
try {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
resolve({
tabId: tabs[0].id,
url: tabs[0].url,
windowId: tabs[0].windowId,
});
});
} catch (error) {
reject(error);
}
});
}
export async function makeTextNodesEditable() {
let code = function () {
let n;
let nodes = [];
let walk = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
while ((n = walk.nextNode())) nodes.push(n);
nodes.forEach((el) => {
// make all text nodes editable
if (el.parentNode.nodeName !== "BODY") {
el.parentNode.setAttribute("contenteditable", true);
}
});
};
return await exec(code);
}
export function xpath(xpathToExecute) {
let result = [];
let nodesSnapshot = document.evaluate(xpathToExecute, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (let i = 0; i < nodesSnapshot.snapshotLength; i++) {
result.push(nodesSnapshot.snapshotItem(i));
}
return result;
}
export function exec(code, args = []) {
code = code.toString();
code = code.slice(code.indexOf("{") + 1, code.lastIndexOf("}"));
// substitute placeholder vars ($$) with their values
args.forEach((argsObj) => {
for (const key in argsObj) {
code = code.replaceAll(key, `/*${key}*/${argsObj[key]}`);
}
});
code = "(function() {" + code + " } ())";
return new Promise(async (resolve, reject) => {
try {
let { tabId } = await getActiveTab();
chrome.tabs.executeScript(
tabId,
{
code,
},
function (a) {
resolve();
}
);
} catch (error) {
reject(error);
}
});
}
export async function setSetting(obj) {
return new Promise((resolve, reject) => {
chrome.storage.sync.set(obj, function () {
resolve();
});
});
}
export async function getSetting(key) {
return new Promise((resolve, reject) => {
chrome.storage.sync.get([key], function (result) {
resolve(result[key] || false);
});
});
}