// background.js
// coded by azrafe7

// manifest object
var manifest = {};

// store current tab info
var currentTab;

var VLC_HOST = "http://localhost";
var VLC_PORT = "8080";
var VLC_INTERFACE = VLC_HOST + ":" + VLC_PORT + "/requests/";


init();

// read manifest file and put parsed object in manifest global variable
function readManifestFile() {
  xhr = new XMLHttpRequest();
  xhr.open("GET", chrome.extension.getURL("manifest.json"));
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      manifest = JSON.parse(xhr.responseText);
    } 
  }
  xhr.send();
}

function isValidUrl(url) {
  return (url.indexOf("watch?") >= 0 || url.indexOf("/v/") >= 0);
}

// show extension icon if on a video page
function checkForValidUrl(tabId, changeInfo, tab) {
  if (tab.url && isValidUrl(tab.url)) {
    console.log("TAB:", tab);
    currentTab = tab;
    chrome.pageAction.show(tabId);
  }
}

// listen for responses
function responseListener(details) {
  if (!details.url.startsWith(VLC_INTERFACE)) return details;
  
  var rules = [
    {
      "name": "Access-Control-Allow-Origin",
      "value": "chrome-extension://nofmfopnoamalaifhaklkpnonoaacfmk"
    },
    {
      "name": "Access-Control-Allow-Credentials",
      "value": "true"
    }
  ];
  for (rule of rules) details.responseHeaders.push(rule);
  console.log("RESPONSE:", details);
  return {responseHeaders: details.responseHeaders};
}

// listen for requests
function requestListener(details) {
  if (!details.url.startsWith(VLC_INTERFACE)) return details;
  
  console.log("REQUEST:", details);
  return details;
}

// initialize
function init() {
  readManifestFile();

  // listen for any changes to the URL of any tab
  chrome.tabs.onUpdated.addListener(checkForValidUrl);
  
  // authorize requests/responses from extension to http://localhost:8080/requests
  chrome.webRequest.onHeadersReceived.addListener(responseListener,
     {urls: [VLC_INTERFACE + "*"]},
     ["blocking", "responseHeaders"]);
  chrome.webRequest.onBeforeRequest.addListener(requestListener,
     {urls: [VLC_INTERFACE + "*"]},
     []);
}

@@ -233,16 +233,176 @@
 ests/%22;%0A
+//var EXTENSION_ID = %22nofmfopnoamalaifhaklkpnonoaacfmk%22; // debug extension id%0Avar EXTENSION_ID = %22jldiailifbdkepgpcojllmkbakleicab%22; // web store extension id%0A
 %0A%0Ainit()
@@ -1299,41 +1299,24 @@
 n://
-nofmfopnoamalaifhaklkpnonoaacfmk%22
+%22 + EXTENSION_ID
 %0A   

use extension id