Skip to content
This repository has been archived by the owner on Jul 20, 2022. It is now read-only.

Commit

Permalink
Converted XMLHTTPRequest to Fetch()
Browse files Browse the repository at this point in the history
  • Loading branch information
hill committed Jan 28, 2018
1 parent 7c633e0 commit 4c690af
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@ chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
console.log(request.word)
searchTLDR(request.word.toLowerCase())
checkCode()
}
)

function searchTLDR (command, platform = 'common') {
let xhr = new XMLHttpRequest()
xhr.addEventListener('load', reqListener)
xhr.open(
'GET',
`${tldrURL}/${platform}/${command}.md`
)
xhr.send()

function reqListener () {
createTooltip(this.responseText)
}
// 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) {
Expand Down Expand Up @@ -103,28 +101,25 @@ window.onmousedown = removeTooltip

let commandList = []

// Creates a list of all commands available in the TLDR repo
function generateCommandList (callback) {
commandList = []
let xhr = new XMLHttpRequest()
xhr.addEventListener('load', reqListener)
xhr.open(
'GET',
apiContentURL
)
xhr.send()

function reqListener () {
let doc
let arr = JSON.parse(this.responseText)

for (doc of arr) {
commandList.push(doc.name.split('.')[0])
}

callback()
}
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
Expand Down

1 comment on commit 4c690af

@hill
Copy link
Owner Author

@hill hill commented on 4c690af Jan 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes #4

Please sign in to comment.