-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
injected.js
162 lines (140 loc) · 4.39 KB
/
injected.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
'use strict';
tryCatch(main);
function main() {
const stylesheet = document.createElement('style');
document.body.append(stylesheet);
const profileData = tryCatch(scrapeProfileData);
console.log("Here's your profile data. Click to expand:");
console.log(profileData);
const regions = tryCatch(() => extractRegions(profileData));
console.log("Here's your regions. Click to expand:");
console.log(regions);
tryCatch(() => updateVisibleUI(regions, stylesheet));
document.addEventListener('click', ({ target: el }) => {
tryCatch(() => {
if (el.matches('.accordion-trigger *')) {
updateHiddenUI(regions, stylesheet);
}
});
});
}
/**
* Greps an object literal representing profile data
* from a function body, converts it to an actual
* object with `eval()`, and returns it.
*/
function scrapeProfileData() {
const { _callbacks: cbs } = window.TTAM;
const cb = cbs.find((cb) =>
cb
.toString()
.includes(
'new exports.reports.ancestryCompositionHD.ACOneWorldMapCoordinator'
)
);
const fnBody = cb.toString();
const regex = /(?<=new exports\.reports\.ancestryCompositionHD\.ACOneWorldMapCoordinator).+(?=;)/s;
const objLiteral = fnBody.match(regex)[0];
const obj = eval(objLiteral);
return obj;
}
/**
* @returns {Regions}
*/
function extractRegions(profileData) {
return profileData.populationTree.children
.map((child) =>
child.children.map((child) => {
// Eastern European regions
const children1 = child.countries.map(
(country) => country.aggregations
);
// Northwestern European regions
const children2 = child.children.map((child) =>
child.countries.map((country) => country.aggregations)
);
return children1.concat(children2);
})
)
.flat(Infinity);
}
/**
* @param {Regions} regions
* @param {HTMLElement} stylesheet
*/
function updateVisibleUI(regions, stylesheet) {
const regionElementSelector =
'.country-name.js-country-name-group.js-country-group.b3';
const regionElements = [...document.querySelectorAll(regionElementSelector)];
regionElements.forEach((el) => {
tryCatch(() => {
const regionText = el.querySelector('strong')?.textContent;
if (regionText === undefined) {
return;
}
el.id = regionText.replace(/\s/g, '-').toLowerCase();
const num_relatives = regions.find(
(region) => region.state === regionText
)?.num_relatives;
const selector = `#${el.id}::after`;
const rule = `${selector} { content: '(${num_relatives} relatives)'; }`;
updateStylesheet(stylesheet, selector, rule);
});
});
}
/**
* @param {Regions} regions
* @param {HTMLElement} stylesheet
*/
function updateHiddenUI(regions, stylesheet) {
const regionElementSelector = 'button[data-subregion-id]';
const regionElements = [...document.querySelectorAll(regionElementSelector)];
regionElements.forEach((el) => {
tryCatch(() => {
const { subregionId } = el.dataset;
const num_relatives = regions.find(
(region) => region.subregion_id === subregionId
)?.num_relatives;
if (num_relatives === undefined) {
return;
}
const selector = `button[data-subregion-id="${subregionId
.replace(/ /g, '_')
.toLowerCase()}"]::after`;
const rule = `${selector} { content: '(${num_relatives} relatives)'; }`;
updateStylesheet(stylesheet, selector, rule);
});
});
}
/**
*
* @param {HTMLElement} stylesheet
* @param {String} selector
* @param {String} rule
*/
function updateStylesheet(stylesheet, selector, rule) {
tryCatch(() => {
const { innerHTML } = stylesheet;
const regex = new RegExp(escapeRegExp(`${selector}.+$`));
const match = innerHTML.match(regex)?.[0];
if (match === undefined) {
stylesheet.innerHTML += `${rule}\n`;
} else {
stylesheet.innerHTML = innerHTML.replace(regex, rule);
}
});
}
/** https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions */
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
function tryCatch(fn) {
try {
return fn();
} catch (e) {
console.log("Caught an error! Here's the function body where it occured:");
console.log(fn.toString());
console.log("Here's the native error message:");
console.log(e);
}
}