-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontentScript.js
107 lines (90 loc) · 3.22 KB
/
contentScript.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
const wikipediaPageCache = new Map()
function checkWikipediaPage(twitterHandle) {
const cached = wikipediaPageCache.get(twitterHandle)
if (cached) return Promise.resolve(cached)
const safeTwitterHandle = encodeURIComponent(twitterHandle)
const apiUrl = `https://hub.toolforge.org/P2002:${safeTwitterHandle}?format=json&lang=en,sv,auto`
const promise = fetch(apiUrl)
.then((response) =>
response.ok ? response.json() : Promise.resolve(false)
)
.then((data) => {
const destination = data.destination
const url = destination?.url
const title = destination?.preferedSitelink?.title
return { url, title }
})
.catch((error) => {
console.error("Error fetching Wikidata:", error)
return false
})
wikipediaPageCache.set(twitterHandle, promise)
return promise
}
const observerOptions = {
root: null,
rootMargin: "100px",
threshold: 0.1,
}
const profileLinkObserver = new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const profileLink = entry.target
const twitterHandle = profileLink.getAttribute("href").replace("/", "")
if (
!profileLink.textContent ||
twitterHandle.includes("/") ||
twitterHandle.includes("?")
) {
return
}
checkWikipediaPage(twitterHandle).then(({ url, title }) => {
if (url) {
const icon = document.createElement("img")
icon.src = chrome.runtime.getURL("icon.svg")
icon.style.width = "16px"
icon.style.height = "16px"
icon.style.margin = "2px 4px"
if (!title.startsWith(profileLink.textContent)) {
icon.style.opacity = "0.5"
}
icon.style.verticalAlign = "text-bottom"
icon.title = `This account has a Wikipedia-page called ${title}.`
const link = document.createElement("a")
link.href = url
link.appendChild(icon)
const wrapper = document.createElement("span")
wrapper.style.display = "flex"
profileLink.parentNode.insertBefore(wrapper, profileLink)
wrapper.appendChild(profileLink)
wrapper.insertAdjacentElement("beforeend", link)
profileLink.setAttribute("data-wikipedia-icon-added", "true")
}
})
observer.unobserve(profileLink)
}
})
}, observerOptions)
function observeProfileLink(profileLink) {
profileLinkObserver.observe(profileLink)
}
function addWikipediaIconToProfile() {
const profileLinks = Array.from(
document.querySelectorAll(
'[data-testid="primaryColumn"] div[dir="auto"] a[href^="/"]:not([data-wikipedia-icon-added]), [data-testid="primaryColumn"] a[href^="/"][role="link"]:not([data-wikipedia-icon-added])'
)
).filter((el) => !el.textContent.includes("@"))
profileLinks.forEach((profileLink) => {
profileLink.setAttribute("data-wikipedia-icon-added", "true")
observeProfileLink(profileLink)
})
}
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === "childList") {
addWikipediaIconToProfile()
}
})
})
observer.observe(document.body, { childList: true, subtree: true })
addWikipediaIconToProfile()