This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
158 lines (134 loc) · 3.78 KB
/
main.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
// Define constants
const apiContentURL = 'https://api.github.com/repos/tldr-pages/tldr/contents/pages/common'
const tldrURL = 'https://raw.githubusercontent.com/tldr-pages/tldr/master/pages'
let tooltip = null
let arrow = null
let currentContent = null
// For right-click tldr search
chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
console.log(request.word)
searchTLDR(request.word.toLowerCase())
checkCode()
}
)
function searchTLDR (command, platform = 'common') {
// Fetch the content from TLDR github repo
fetch(`${tldrURL}/${platform}/${command}.md`)
.then((response) => {
return response.text()
})
.then(data => {
createTooltip(data)
})
}
function createTooltip (content, isMarked = false) {
let selection = window.getSelection()
let range = selection.getRangeAt(0)
let rect = range.getBoundingClientRect()
let newtop = null
if (rect.width >= 0) {
if (tooltip) {
tooltip.parentNode.removeChild(tooltip)
}
tooltip = document.createElement('div')
tooltip.id = 'tldr-chrome'
newtop = rect.top - 200 + window.scrollY
Object.assign(
tooltip.style,
{
top: `${newtop}px`,
left: `${rect.left}px`
}
)
document.body.appendChild(tooltip)
// Create Arrow
arrow = document.createElement('div')
arrow.id = 'tldr-chrome-arrow'
document.body.appendChild(arrow)
Object.assign(
arrow.style,
{
left: `${(rect.left) + 5}px`,
top: `${newtop + 195}px` // newtop + height of tooltip
}
)
// Create markdown and append to tooltip
let markdown = null
if (content.trim() === '404: Not Found') {
markdown = '<div class="not-found"><p class="large">😱</p><p>Page Not Found!</p><p>Submit a pull request to: <a target="_blank" href="https://github.com/tldr-pages/tldr">https://github.com/tldr-pages/tldr</a></p></div>'
} else {
if (isMarked) {
markdown = content
} else {
markdown = marked(content)
}
}
currentContent = markdown
let markdownContent = document.createElement('div')
markdownContent.innerHTML = markdown
markdownContent.className += 'tldr-chrome'
tooltip.appendChild(markdownContent)
}
}
// removes the tooltip, arrow and content
function removeTooltip () {
if (tooltip != null) {
tooltip.parentNode.removeChild(tooltip)
tooltip = null
arrow.parentNode.removeChild(arrow)
arrow = null
currentContent = null
}
}
// Ensures the tldr tooltip does not close if clicked on
window.onmousedown = (mouseDownEvent) => {
let isPopup = false
mouseDownEvent.path.forEach((elementInPath) => {
if (elementInPath.id === 'tldr-chrome') isPopup = true
})
if (!isPopup) {
removeTooltip()
}
}
let commandList = []
// Creates a list of all commands available in the TLDR repo
function generateCommandList (callback) {
commandList = []
fetch(apiContentURL)
.then((response) => {
return response.json()
})
.then(data => {
let doc
for (doc of data) {
commandList.push(doc.name.split('.')[0])
}
callback()
})
}
// Checks if a command in pre tags is available in the TLDR github repo
function checkCode () {
generateCommandList(() => {
let tag
let word
let preTags = document.getElementsByTagName('pre')
for (tag of preTags) {
for (word of tag.innerText.split(' ')) {
if (commandList.includes(word.toLowerCase())) {
// if word is in commandList
console.log(`FOUND COMMAND: ${word}`)
}
}
}
})
}
// Deletes the tooltip and resizes when window is resized.
window.onresize = event => {
let oldContent
if (tooltip) {
oldContent = currentContent
removeTooltip()
createTooltip(oldContent, true)
}
}