-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwitterEntrypoint.js
58 lines (44 loc) · 1.49 KB
/
twitterEntrypoint.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
import "chrome-extension-async";
import "@webcomponents/custom-elements";
import elementReady from "element-ready";
import WidgetOrchestrator from "./widgetOrchestrator";
import TwitterProfilePage from "../pages/twitterProfilePage";
const initializeWidget = () => {
const pages = [new TwitterProfilePage()];
const orchestrator = new WidgetOrchestrator();
const page = orchestrator.detectPage(pages);
if (!page) return;
orchestrator.addWidgetElements(page);
};
async function setupObserver() {
const titleElement = await elementReady("head > title", {
stopOnDomReady: false,
timeout: 5000,
});
if (!titleElement) {
return;
}
const observer = new MutationObserver((mutationList, _observer) => {
// We only want to initialize the widget once the page is fully loaded.
// We can detect this using the <title> as it shows the username (@something) when the
// page has loaded, but not before
mutationList.forEach((mutation) => {
if (!mutation.type === "childList") {
return;
}
const newTitle = mutation.addedNodes[0]?.textContent;
if (newTitle === undefined || newTitle.indexOf("@") === -1) {
return;
}
// Adding a small timeout so that page transitions can complete (most of the time),
// which helps with detecting profile pages.
setTimeout(initializeWidget, 500);
});
});
observer.observe(titleElement, {
subtree: true,
childList: true,
characterData: true,
});
}
setupObserver();