-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
86 lines (80 loc) · 2.43 KB
/
index.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
import { getReadTheDocsConfig } from "./readthedocs-config";
import { injectExternalVersionWarning } from "./external-version-warning";
import { injectNonLatestVersionWarning } from "./non-latest-version-warning";
import { injectFlyout, trackFlyoutEvents } from "./flyout";
import { registerPageView, injectAnalytics } from "./analytics";
import { injectEthicalAd } from "./sponsorship";
import { initializeHoverXRef } from "./hoverxref";
import { initializeSearchAsYouType } from "./search";
import { initializeDocDiff } from "./docdiff";
export function IsReadTheDocsEmbedPresent() {
const url = "/_/static/javascript/readthedocs-doc-embed.js";
return document.querySelectorAll(`script[src="${url}"]`).length > 0;
}
export function setup() {
if (IsReadTheDocsEmbedPresent()) {
console.debug("Read the Docs Embed is present. Skipping...");
return false;
}
const is_loaded = new Promise((resolve) => {
if (
document.readyState === "interactive" ||
document.readyState === "complete"
) {
return resolve();
} else {
document.addEventListener(
"DOMContentLoaded",
() => {
resolve();
},
{
capture: true,
once: true,
passive: true,
}
);
}
return undefined;
});
return new Promise((resolve) => {
is_loaded
.then(() => {
return getReadTheDocsConfig();
})
.then((config) => {
let promises = [];
const integrations = [
injectAnalytics,
injectFlyout,
initializeSearchAsYouType,
trackFlyoutEvents,
registerPageView,
injectEthicalAd,
initializeHoverXRef,
injectNonLatestVersionWarning,
injectExternalVersionWarning,
];
// Iterate over all the integration functions and create one Promise for each of them.
// They will be executed concurrently.
for (let fn of integrations) {
promises.push(
new Promise((resolve) => {
fn(config);
})
);
}
return Promise.all(promises).then(() => {
// FIXME: why this function is not called at all?
// I want to call it _after_ all the promises have been executed.
initializeDocDiff(config);
});
})
.then(() => {
resolve();
})
.catch((err) => {
console.error(err.message);
});
});
}