summaryrefslogtreecommitdiffstats
path: root/misc/chrome/gophertool/popup.js
blob: 410d65120e6f067023f1c407b5dc76adfdae4e15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function openURL(url) {
  chrome.tabs.create({ "url": url })
}

function addLinks() {
  var links = document.getElementsByTagName("a");
  for (var i = 0; i < links.length; i++) {
    var url = links[i].getAttribute("url");
    if (url)
      links[i].addEventListener("click", function () {
        openURL(this.getAttribute("url"));
      });
  }
}

window.addEventListener("load", function () {
  addLinks();
  console.log("hacking gopher pop-up loaded.");
  document.getElementById("inputbox").focus();
});

window.addEventListener("submit", function () {
  console.log("submitting form");
  var box = document.getElementById("inputbox");
  box.focus();

  var t = box.value;
  if (t == "") {
    return false;
  }

  var success = function(url) {
    console.log("matched " + t + " to: " + url)
    box.value = "";
    openURL(url);
    return false;  // cancel form submission
  };

  var url = urlForInput(t);
  if (url) {
    return success(url);
  }

  console.log("no match for text: " + t)
  return false;
});