Skip to content

Commit 2411b0a

Browse files
authored
Merge pull request #2 from piraces/feature/dark-light-mode
Feature/dark light mode
2 parents 4a3c7a4 + d8bb630 commit 2411b0a

File tree

5 files changed

+156
-110
lines changed

5 files changed

+156
-110
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ open_collective: # Replace with a single Open Collective username
66
ko_fi: piraces
77
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
88
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9-
liberapay: tldr-pages
9+
liberapay: piraces
1010
issuehunt: # Replace with a single IssueHunt username
1111
otechie: # Replace with a single Otechie username
1212
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

.github/workflows/chrome-publish.yml

Lines changed: 0 additions & 36 deletions
This file was deleted.

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
# tldr-extension-browser
2-
[![Publish extension in Chrome Store](https://github.com/piraces/tldr-extension-browser/actions/workflows/chrome-publish.yml/badge.svg)](https://github.com/piraces/tldr-extension-browser/actions/workflows/chrome-publish.yml)
1+
# tldr pages browser extension
32
[![Generate artifacts to publish](https://github.com/piraces/tldr-extension-browser/actions/workflows/artifacts-zip.yml/badge.svg)](https://github.com/piraces/tldr-extension-browser/actions/workflows/artifacts-zip.yml)
43

54

65
📚 A browser extension for [tldr-pages](https://github.com/tldr-pages/tldr).
76

8-
🚧 **There is [work in progress](https://github.com/piraces/tldr-extension-browser/projects/1) for this extension** 🚧
7+
👷‍♂️ There is **[work in progress](https://github.com/piraces/tldr-extension-browser/projects/1) for this extension**
98

109
**Feel free to contribute 😀!!**
1110

1211
## Download
1312

14-
**Note:** The extension is being reviewed by the Chrome Store and Firefox Browser Add-ons and should be published in a near future.
15-
1613
**Get the extension:**
1714
- For Edge: [https://microsoftedge.microsoft.com/addons/detail/tldr-pages/hbplonhehblpcghgkhnjepdbohbpkoak](https://microsoftedge.microsoft.com/addons/detail/tldr-pages/hbplonhehblpcghgkhnjepdbohbpkoak)
18-
- 🚧 For Chrome (pending review): [in the Chrome Store](https://chrome.google.com/webstore/search/tldr-pages). 🚧
19-
- 🚧 For Firefox (pending review): [Firefox Browser Add-ons](https://addons.mozilla.org/es/firefox/addon/tldr-pages/) 🚧
15+
- For Chrome: [in the Chrome Store](https://chrome.google.com/webstore/detail/tldr-pages/fcccijijdgmmcjnifgdhcmepnkcdingf).
16+
- For Firefox: [Firefox Browser Add-ons](https://addons.mozilla.org/es/firefox/addon/tldr-pages/).
2017

2118
## Loading Extension for development
2219

main.js

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
// Define constants
22
const apiContentURL = 'https://api.github.com/repos/tldr-pages/tldr/contents/pages';
33

4+
const portCommunicationName = 'tldr-pages-comm';
5+
6+
const defaultContentEncoding = 'base64';
7+
8+
const defaultPlatform = 'common';
9+
const defaultErrorResponse = '404: Not Found';
10+
const defaultNotFoundMessageHTML = '<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>';
11+
12+
const tooltipId = 'tldr-chrome';
13+
const toooltipArrowId = 'tldr-chrome-arrow';
14+
15+
const tooltipOffset = 200;
16+
const arrowOffset = 25;
17+
const tooltipHeight = 195;
18+
19+
// Variables
20+
421
let tooltip = null
522
let arrow = null
623
let currentContent = null
724

8-
925
// For right-click tldr search
1026
var port = chrome.runtime.connect();
11-
port.postMessage({comm: 'tldr-pages-comm', data: true});
27+
port.postMessage({comm: portCommunicationName, data: true});
1228
port.onMessage.addListener(function (message) {
13-
if (message.comm === 'tldr-pages-comm') {
29+
if (message.comm === portCommunicationName) {
1430
getContent(message.data);
1531
}
1632
});
@@ -20,104 +36,106 @@ function getContent(word) {
2036
checkCode();
2137
}
2238

23-
function searchTLDR (command, platform = 'common') {
39+
function searchTLDR (command, platform = defaultPlatform) {
2440
// Fetch the content from TLDR github repo
2541
fetch(`${apiContentURL}/${platform}/${command}.md`)
2642
.then((response) => {
2743
return response.json();
2844
})
2945
.then(responseText => {
30-
if (responseText.content && responseText.encoding == 'base64') {
46+
if (responseText.content && responseText.encoding == defaultContentEncoding) {
3147
return atob(responseText.content);
3248
} else {
33-
return '404: Not Found';
49+
return defaultErrorResponse;
3450
}
3551
})
3652
.then(data => {
37-
createTooltip(data)
53+
createTooltip(data);
3854
})
3955
}
4056

4157
function createTooltip (content, isMarked = false) {
42-
let selection = window.getSelection()
43-
let range = selection.getRangeAt(0)
44-
let rect = range.getBoundingClientRect()
45-
let newtop = null
58+
let selection = window.getSelection();
59+
let range = selection.getRangeAt(0);
60+
let rect = range.getBoundingClientRect();
61+
let newtop = null;
4662

4763
if (rect.width >= 0) {
4864

4965
if (tooltip) {
50-
tooltip.parentNode.removeChild(tooltip)
66+
tooltip.parentNode.removeChild(tooltip);
5167
}
5268

53-
tooltip = document.createElement('div')
54-
tooltip.id = 'tldr-chrome'
55-
newtop = rect.top - 200 + window.scrollY
69+
tooltip = document.createElement('div');
70+
tooltip.id = tooltipId;
71+
newtop = rect.top - tooltipOffset + window.scrollY;
5672

5773
Object.assign(
5874
tooltip.style,
5975
{
6076
top: `${newtop}px`,
6177
left: `${rect.left}px`
6278
}
63-
)
79+
);
6480

65-
document.body.appendChild(tooltip)
81+
document.body.appendChild(tooltip);
6682

6783
// Create Arrow
68-
arrow = document.createElement('div')
69-
arrow.id = 'tldr-chrome-arrow'
70-
document.body.appendChild(arrow)
84+
arrow = document.createElement('div');
85+
arrow.id = toooltipArrowId;
86+
document.body.appendChild(arrow);
7187

7288
Object.assign(
7389
arrow.style,
7490
{
75-
left: `${(rect.left) + 5}px`,
76-
top: `${newtop + 195}px` // newtop + height of tooltip
91+
left: `${(rect.left) + arrowOffset}px`,
92+
top: `${newtop + tooltipHeight}px`
7793
}
78-
)
94+
);
7995

8096
// Create markdown and append to tooltip
81-
let markdown = null
82-
if (content.trim() === '404: Not Found') {
83-
markdown = '<div class="not-found"><p class="large">😱</p><p>Page Not Found!</p><p>Submit a pull request to: <a target="_blank" href="/tldr-pages/tldr">https://github.com/tldr-pages/tldr</a></p></div>'
97+
let markdown = null;
98+
if (content.trim() === defaultErrorResponse) {
99+
markdown = defaultNotFoundMessageHTML;
84100
} else {
85101
if (isMarked) {
86-
markdown = content
102+
markdown = content;
87103
} else {
88-
markdown = marked(content)
104+
markdown = marked(content);
89105
}
90106
}
91107

92-
currentContent = markdown
108+
currentContent = markdown;
93109

94-
let markdownContent = document.createElement('div')
95-
markdownContent.innerHTML = markdown
96-
markdownContent.className += 'tldr-chrome'
97-
tooltip.appendChild(markdownContent)
110+
let markdownContent = document.createElement('div');
111+
markdownContent.innerHTML = markdown;
112+
markdownContent.className += tooltipId;
113+
tooltip.appendChild(markdownContent);
98114
}
99115
}
100116

101117
// removes the tooltip, arrow and content
102118
function removeTooltip () {
103119
if (tooltip != null) {
104-
tooltip.parentNode.removeChild(tooltip)
105-
tooltip = null
106-
arrow.parentNode.removeChild(arrow)
107-
arrow = null
108-
currentContent = null
120+
tooltip.parentNode.removeChild(tooltip);
121+
tooltip = null;
122+
arrow.parentNode.removeChild(arrow);
123+
arrow = null;
124+
currentContent = null;
109125
}
110126
}
111127

112128
// Ensures the tldr tooltip does not close if clicked on
113129
window.onmousedown = (mouseDownEvent) => {
114130
let isPopup = false
115131
mouseDownEvent.path.forEach((elementInPath) => {
116-
if (elementInPath.id === 'tldr-chrome') isPopup = true
132+
if (elementInPath.id === tooltipId) {
133+
isPopup = true;
134+
}
117135
})
118136

119137
if (!isPopup) {
120-
removeTooltip()
138+
removeTooltip();
121139
}
122140
}
123141

@@ -129,24 +147,24 @@ function generateCommandList (callback) {
129147

130148
fetch(apiContentURL)
131149
.then((response) => {
132-
return response.json()
150+
return response.json();
133151
})
134152
.then(data => {
135-
let doc
153+
let doc;
136154
for (doc of data) {
137-
commandList.push(doc.name.split('.')[0])
155+
commandList.push(doc.name.split('.')[0]);
138156
}
139157

140-
callback()
158+
callback();
141159
})
142160
}
143161

144162
// Checks if a command in pre tags is available in the TLDR github repo
145163
function checkCode () {
146164
generateCommandList(() => {
147-
let tag
148-
let word
149-
let preTags = document.getElementsByTagName('pre')
165+
let tag;
166+
let word;
167+
let preTags = document.getElementsByTagName('pre');
150168
for (tag of preTags) {
151169
for (word of tag.innerText.split(' ')) {
152170
if (commandList.includes(word.toLowerCase())) {
@@ -159,10 +177,10 @@ function checkCode () {
159177

160178
// Deletes the tooltip and resizes when window is resized.
161179
window.onresize = event => {
162-
let oldContent
180+
let oldContent;
163181
if (tooltip) {
164-
oldContent = currentContent
165-
removeTooltip()
166-
createTooltip(oldContent, true)
182+
oldContent = currentContent;
183+
removeTooltip();
184+
createTooltip(oldContent, true);
167185
}
168186
}

0 commit comments

Comments
 (0)