console.log('Background Script Loaded');

chrome.runtime.onConnect.addListener((port) => {
  var tab = port.sender.tab
  port.onMessage.addListener((info) => {
    console.log('info: ', info)
    chrome.storage.sync.get(info.video_id, function(result) {
      var theValue, store = {}
      if(chrome.runtime.lastError) {        
        console.log(chrome.runtime.lastError)
      } else {
        theValue = result[info.video_id] || []
        theValue.push({
          id: info.video_id,
          pos: info.position,
          t: info.title
        })
        store[info.video_id] = theValue
        chrome.storage.sync.set(store,() => {
          port.postMessage( {new: true} )
        })
      }
    })
  })
})
