const fs = require('fs')
const jsdom = require('jsdom')

let request = (url, callback = () => {}) => require('request')({
    url,
    headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.148 Safari/537.36 Vivaldi/1.4.589.38'
    }
}, (err, response, body) => {
    if (err || response.statusCode != 200)
        return callback(err || new Error('Lyrics unavailable'))

    callback(null, response, body)
})

exports.searchFor = function(query, callback = () => {}) {
    let url = `https://musixmatch.com/search/${encodeURI(query)}/tracks`

    request(url, (err, response, body) => {
        if (err) return callback(err)
    })
}

exports.extractFrom = function(url, callback = () => {}) {
    request(url, (err, response, body) => {
        if (err) return callback(err)

        jsdom.env(body, (err, window) => {
            if (err) return callback(err)

            let $ = x => Array.from(window.document.querySelectorAll(x))
            let getTextNode = x => x ? Array.from(x.childNodes).find(y => y.nodeName == '#text').nodeValue : null

            let title = getTextNode($('.mxm-track-title__track')[0])
            let artists = $('.mxm-track-title__artist').map(x => getTextNode(x))
            let album = getTextNode($('.mxm-track-footer__album h2')[0])
            let albumCover = 'http:' + $('.banner-album-image img')[0].src
            let lyrics = $('.mxm-lyrics__content').map(x => getTextNode(x)).join('\n\n')

            callback(null, {title, artists, album, albumCover, lyrics})
        })
    })
}

@@ -480,221 +480,1030 @@
 %7D)%0A%0A
-exports.searchFor = function(query, callback = () =%3E %7B%7D) %7B%0A    let url = %60https://musixmatch.com/search/$%7BencodeURI(query)%7D/tracks%60%0A%0A    request(url, (err, response, body) =%3E %7B%0A        if (err) return callback(err
+let dom = (body, callback = () =%3E %7B%7D) =%3E jsdom.env(body, (err, window) =%3E %7B%0A    if (err) return callback(err)%0A%0A    let $ = x =%3E Array.from(window.document.querySelectorAll(x))%0A    let text = x =%3E x ? Array.from(x.childNodes).find(y =%3E y.nodeName == '#text').nodeValue : null%0A%0A    callback(null, window, $, text)%0A%7D)%0A%0Aexports.searchFor = function(query, callback = () =%3E %7B%7D) %7B%0A    let url = %60https://musixmatch.com/search/$%7BencodeURI(query)%7D/tracks%60%0A%0A    request(url, (err, response, body) =%3E %7B%0A        if (err) return callback(err)%0A%0A        dom(body, (err, window, $, text) =%3E %7B%0A            if (err) return callback(err)%0A%0A            callback(null, $('.tracks.list li').map(li =%3E li = %7B%0A                title: text(li.querySelector('.title span')),%0A                artist: text(li.querySelector('.artist')),%0A                url: %60https://musixmatch.com$%7Bli.querySelector('.title').href%7D%60,%0A                art: 'https:' + li.querySelector('img').srcset.split(',').splice(-1)%5B0%5D.trim().split(' ')%5B0%5D%0A            %7D))%0A        %7D
 )%0A  
@@ -1660,25 +1660,19 @@
         
-js
 dom
-.env
 (body, (
@@ -1674,32 +1674,41 @@
 dy, (err, window
+, $, text
 ) =%3E %7B%0A         
@@ -1757,184 +1757,28 @@
     
-let $ = x =%3E Array.from(window.document.querySelectorAll(x))%0A            let getTextNode = x =%3E x ? Array.from(x.childNodes).find(y =%3E y.nodeName == '#text').nodeValue : null%0A%0A
+callback(null, %7B%0A   
     
@@ -1789,31 +1789,20 @@
     
-let
  title
- = getTextNode
+: text
 ($('
@@ -1826,24 +1826,25 @@
 _track')%5B0%5D)
+,
 %0A           
@@ -1848,19 +1848,19 @@
     
-let
+   
  artists
  = $
@@ -1855,18 +1855,17 @@
  artists
- =
+:
  $('.mxm
@@ -1896,31 +1896,25 @@
 ap(x =%3E 
-getTextNode
+text
 (x))
+,
 %0A       
@@ -1922,31 +1922,23 @@
     
-let
+   
  album
- = getTextNode
+: text
 ($('
@@ -1970,16 +1970,17 @@
 h2')%5B0%5D)
+,
 %0A       
@@ -1988,30 +1988,23 @@
     
-let albumCover =
+    art:
  'http
+s
 :' +
@@ -2039,16 +2039,17 @@
 )%5B0%5D.src
+,
 %0A       
@@ -2057,20 +2057,19 @@
     
-let
+   
  lyrics
- =
+:
  $('
@@ -2104,19 +2104,12 @@
  =%3E 
-getTextNode
+text
 (x))
@@ -2122,17 +2122,16 @@
 '%5Cn%5Cn')%0A
-%0A
         
@@ -2138,65 +2138,8 @@
     
-callback(null, %7Btitle, artists, album, albumCover, lyrics
 %7D)%0A 

Add custom jsdom template & add searchFor()