aboutsummaryrefslogtreecommitdiffstats
path: root/background_page.html
blob: e3193a59a48e7306bb65a9fd68e0c9eaadc1a7e9 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<html>
<head>
<script type="text/javascript" charset="utf-8">
  var tabQueue = [];
  var selectionChangedHandlers = [];
  var keyQueue = "";

  chrome.extension.onConnect.addListener(function(port, name) {
    if (port.name == "keyDown")
      port.onMessage.addListener(handleKeyDown);
  });

  chrome.tabs.onSelectionChanged.addListener(function (tabId, selectionInfo) {
    if (selectionChangedHandlers.length > 0)
    {
      selectionChangedHandlers.pop().call();
    }
  });

  function registerSelectionChangedHandler(handler) {
    selectionChangedHandlers.push(handler);
  }

  function repeatFunction(func, totalCount, currentCount) {
    if (currentCount < totalCount)
    {
      func(function () { repeatFunction(func, totalCount, currentCount + 1); });
    }
  }

  // Start action functions
  function createTab(callback) {
    chrome.tabs.create({}, function (tab) { callback(); });
  }

  function removeTab(callback) {
    chrome.tabs.getSelected(null, function(tab) {
                                      tabQueue.push(tab.url);
                                      chrome.tabs.remove(tab.id);
                                      // We can't just call the callback here because we actually need to wait
                                      // for the selection to change to consider this action done.
                                      registerSelectionChangedHandler(callback);
                                  });
  }

  function restoreTab(callback) {
    if (tabQueue.length > 0) {
      chrome.tabs.create({url: tabQueue.pop()}, function (tab) { callback(); });
    }
  }
  // End action functions

  var keyToCommandRegistry = {};
  keyToCommandRegistry['j']  = 'scrollDown';
  keyToCommandRegistry['k']  = 'scrollUp';
  keyToCommandRegistry['h']  = 'scrollLeft';
  keyToCommandRegistry['l']  = 'scrollRight';
  keyToCommandRegistry['gg'] = 'scrollToTop';
  keyToCommandRegistry['G']  = 'scrollToBottom';
  keyToCommandRegistry['r']  = 'reload';
  keyToCommandRegistry['t']  = createTab;
  keyToCommandRegistry['d']  = removeTab;
  keyToCommandRegistry['u']  = restoreTab;

  function handleKeyDown(key) {
    keyQueue = keyQueue + key;
    console.log("current keyQueue: [", keyQueue, "]");
    checkKeyQueue();
  }

  function checkKeyQueue() {
    var match = /([0-9]*)(.*)/.exec(keyQueue);
    var count = parseInt(match[1]);
    var command = match[2];

    if (command.length == 0) { return; }
    if (isNaN(count)) { count = 1; }

    if (keyToCommandRegistry[command])
    {
      registryEntry = keyToCommandRegistry[command];
      console.log("command found for [", keyQueue, "],", registryEntry);

      if (typeof(registryEntry) == "string")
      {
        chrome.tabs.getSelected(null, function (tab) {
          var port = chrome.tabs.connect(tab.id, {name: "executePageCommand"});
          port.postMessage({command: registryEntry, count: count});
        });
      }
      else { repeatFunction(registryEntry, count, 0); }
      
      keyQueue = "";
    }
    else if (command.length > 1)
      keyQueue = "";
  }
</script>

</head>

<body>
  howdy
</body>

</html>