Skip to content

Instantly share code, notes, and snippets.

@Radivarig
Last active May 12, 2018 18:55
Show Gist options
  • Select an option

  • Save Radivarig/878c368d75e9c0ba45c5559c99958e8a to your computer and use it in GitHub Desktop.

Select an option

Save Radivarig/878c368d75e9c0ba45c5559c99958e8a to your computer and use it in GitHub Desktop.
var setContentByUrl = (url, content) => localStorage[url] = JSON.stringify (content)
var getContentByUrl = (url) => localStorage[url] && JSON.parse(localStorage[url])
var getHttpStringByUrl = (url) => {
var xhr = new XMLHttpRequest()
xhr.open( "GET", url, false)
xhr.send(null)
return xhr.responseText
}
var headersAndAnchors = Array.from(document.querySelectorAll('h2,a'))
var linkNameUrlDict = {}
var currentHeader
headersAndAnchors.forEach (el => {
if (el.tagName === 'H2') {
currentHeader = el
return
}
if (!currentHeader)
return
var key = (currentHeader.innerHTML + ": " + el.innerHTML).replace(/\s\s+/g, ' ')
linkNameUrlDict[key] = el.href
})
var linkNameUrlDictNames = window.linkNameUrlDictNames = Object.keys (linkNameUrlDict)
var typeVacuumsDict = window.typeVacuumsDict = {}
var getTypesByUrl = (url) => {
var el = document.createElement('html')
el.innerHTML = getHttpStringByUrl (url)
var divTip = el.querySelector('#divTip')
var text = divTip && divTip.innerHTML || ""
text = text.replace ("TIP:", "")
text = text.replace (/<br>/g,"")
return text.split(",").map (t => t.trim())
}
var fetchTypes = () => {
var currentIndex = 0
var recursiveCall = () => {
if (currentIndex >= linkNameUrlDictNames.length) {
setContentByUrl ('typeVacuumsDict', typeVacuumsDict)
console.log ("typeVacuumsDict", typeVacuumsDict)
window.alert(`
Enter: typeVacuumsDict
-> Click result to drop down
If you close the tab or browser, restore from cache:
typeVacuumsDict = JSON.parse(localStorage.typeVacuumsDict)
`)
return
}
var didFetch = false
var name = linkNameUrlDictNames[currentIndex]
var url = linkNameUrlDict[name]
var types = getContentByUrl (url)
if (!types) {
types = getTypesByUrl (url)
setContentByUrl (url, types)
didFetch = true
}
types.forEach ((type) => {
if (!typeVacuumsDict[type])
typeVacuumsDict[type] = []
typeVacuumsDict[type].push(name)
})
currentIndex++
if (didFetch)
console.log (currentIndex, types, url)
else console.log ("using cached value")
setTimeout (recursiveCall, didFetch ? 50 : 0)
}
recursiveCall ()
}
fetchTypes ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment