var browser = browser || chrome

let filters = []

browser.runtime.sendMessage({ type: 'get-filters' }, getFilters)
browser.runtime.onMessage.addListener(getFilters)

function getFilters(upToDateFilters) {
  filters = upToDateFilters
  hideVideos()
}

function hideVideos() {
  const noFilter = filters.length
  const lis = document.getElementsByClassName('yt-shelf-grid-item')
  const videosToHide = filters.map(title => title.toUpperCase())
  const regex = new RegExp(videosToHide.join('|'))
  const displayedVideos = []
  const hiddenVideos = []

  for (const li of lis) {
    const title = li.querySelector('h3 > a').innerHTML
    const upperCaseTitle = title.toUpperCase()
    if (noFilter && regex.test(upperCaseTitle)) {
      if (li.style.display !== 'none') {
        hiddenVideos.push(title)
        li.style.display = 'none'
      }
    } else {
      if (li.style.display === 'none') {
        displayedVideos.push(title)
        li.style.display = 'inline-block'
      }
    }
  }

  logVideosStateChange('Hiding', hiddenVideos)
  logVideosStateChange('Displaying', displayedVideos)
}

function logVideosStateChange(verb, videos) {
  if (videos.length > 0) {
    console.groupCollapsed(`${verb} ${videos.length} videos`)

    for (const title of videos) {
      console.info(title)
    }

    console.groupEnd()
  }
}

@@ -1162,17 +1162,19 @@
 gth 
-%3E
+===
  0) %7B%0A  
   co
@@ -1165,24 +1165,38 @@
  === 0) %7B%0A  
+  return%0A  %7D%0A%0A
   console.gr
@@ -1244,18 +1244,16 @@
 deos%60)%0A%0A
-  
   for (c
@@ -1280,18 +1280,16 @@
 ) %7B%0A    
-  
 console.
@@ -1302,23 +1302,19 @@
 itle)%0A  
- 
- 
 %7D%0A%0A
-  
   consol
@@ -1326,14 +1326,10 @@
 upEnd()%0A
-  %7D%0A
 %7D%0A

Improve code