<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>App.getProxyForURL</title>
</head>
<body>
  <button id="pac" onclick="requestPac()">Request pac</button>
  <button id="open-devtools" onclick="openDevTools()">Open DevTools</button>
  <script>
    nw.App.setProxyConfig('dummy:8080');
    var proxy = nw.App.getProxyForURL('http://www.example.com/');
    var expect = 'PROXY dummy:8080';
    if (proxy === expect) {
      document.write('<h1 id="result">success</h1>');
    } else {
      document.write('<h1 id="result">failure: expect "' + expect + '" but get ' + proxy +'</h1>');
    }

    // Reset Proxy
    nw.App.setProxyConfig("", "<direct>");

    function requestPac() {
      var xhr = new XMLHttpRequest();
      xhr.open('GET', `http://localhost:${nw.App.manifest.port}/`, true);
      xhr.send();
      xhr.onload = ()=>{
        var b = new Buffer(xhr.responseText);
        var encodedUrl = "data:application/x-ns-proxy-autoconfig;base64," + b.toString('base64');
        nw.App.setProxyConfig('dummy:8080', encodedUrl);
        var proxy = nw.App.getProxyForURL('http://www.port3128.com/');
        var expect = 'PROXY localhost:3128';
        if (proxy === expect) {
          document.write('<h1 id="result2">success</h1>');
        } else {
          document.write('<h1 id="result2">failure: expect "' + expect + '" but get ' + proxy +'</h1>');
        }
        var proxy = nw.App.getProxyForURL('http://www.port4040.com/');
        var expect = 'PROXY localhost:4040';
        if (proxy === expect) {
          document.write('<h1 id="result3">success</h1>');
        } else {
          document.write('<h1 id="result3">failure: expect "' + expect + '" but get ' + proxy +'</h1>');
        }
      };
    }

    function openDevTools() {
      chrome.developerPrivate.openDevTools({renderProcessId: -1, renderViewId: -1, extensionId: chrome.runtime.id});
    }
</script>
</body>
</html>
