summaryrefslogtreecommitdiff
path: root/vimperator/plugin/noscript.js
blob: 35582057e77352947e15d5155489ea055476f3e8 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
 * Integration plugin for noscript extension
 *
 * Usage:
 *   :noscript info        Shows noscript info for the current page.
 *   :noscript popup       Opens the noscript popup.
 *   :noscript toggletemp  Toggles scripts temporarily on current page.
 *   :noscript toggleperm  Toggles scripts permanently on current page.
 *
 * Note: when the noscript popup is open, you can scroll through the items
 * using the following standard vimperator key bindings:
 *   j  move down one
 *   k  move up one
 *   l  open the sub-menu
 *   h  close the sub-menu
 *   g  jump to the first item in the menu
 *   G  jump to the last item in the menu
 * For implementation simplicity, only single character bindings are supported,
 * so you cannot supply counts to any of the above.
 *
 * Tested against NoScript 1.8.6
 *
 * @author Eric Van Dewoestine (ervandew@gmail.com)
 * @version 0.4
 *
 * First two versions written by Martin Stubenschrott, but now maintained by
 * Eric.  Please direct all correspondence regarding this plugin to Eric.
 */

/**
 * Class which provides support for noscript commands and hooks into the
 * noscript popup to provide vimperator scrolling bindings (j,k,l,h,g,G).
 *
 * Note: There appears to be a bug in firefox's menupopup key bindings, where
 * if you navigate to a sub popup (l or right arrow), and navigate back to the
 * main popup (h or left arrow), then the menupopup seems to lose the ability
 * to navigate back to the sub menu, and loses the ability to close the main
 * popup via esc (an alt-tab should still close it).  This behavior is
 * reproducable with or without this plugin.
 */
function NoscriptVimperator() {
  var popup = (
    noscriptOverlay.stickyUI &&
    noscriptOverlay.ns.getPref("stickyUI.onKeyboard") &&
    (popup = noscriptOverlay.stickyUI)
  ) || document.getElementById("noscript-status-popup");
  popup.addEventListener('popupshown', popupshown, true);
  popup.addEventListener('popuphidden', popuphidden, true);

  function popupshown(event){
    if (event.target == popup){
      window.addEventListener("keypress", keypress, true);
    }
  }

  function popuphidden(event){
    if (event.target == popup){
      window.removeEventListener("keypress", keypress, true);
    }
  }

  function keypress(event){
    var keyCode = null;
    switch(String.fromCharCode(event.which)){
      case "j":
        keyCode = 40;
        break;
      case "k":
        keyCode = 38;
        break;
      case "l":
        keyCode = 39;
        break;
      case "h":
        keyCode = 37;
        break;
      case "G":
        keyCode = 35;
        break;
      case "g":
        keyCode = 36;
        break;
      default:
        break;
    }

    if (keyCode){
      var newEvent = window.document.createEvent('KeyboardEvent');
      newEvent.initKeyEvent(
        "keypress", true, true, null, false, false, false, false, keyCode, 0);
      popup.dispatchEvent(newEvent);
    }
  }

  return {
    info: function(){
      liberator.echo(util.objectToString(noscriptOverlay.getSites(), true));
    },

    popup: function(){
      noscriptOverlay.showUI();
    },

    toggletemp: function(){
      noscriptOverlay.toggleCurrentPage(3);
    },

    toggleperm: function(){
      const ns = noscriptOverlay.ns;
      const url = ns.getQuickSite(content.document.documentURI, /*level*/ 3);
      noscriptOverlay.safeAllow(url, !ns.isJSEnabled(url), false);
    },

    _execute: function(args){
      var name = args.shift();
      var cmd = nsv[name];
      if (!cmd){
        liberator.echoerr('Unsupported noscript command: ' + name);
        return false;
      }
      return cmd(args);
    },

    _completer: function(context){
      var commands = [];
      for (var name in nsv){
        if (name.indexOf('_') !== 0 && nsv.hasOwnProperty(name)){
          commands.push(name);
        }
      }
      context.completions = [[c, ''] for each (c in commands)];
    }
  };
}

var nsv = NoscriptVimperator();

commands.addUserCommand(["nosc[ript]"],
  "Execute noscript commands",
  function(args) { nsv._execute(args); },
  { argCount: '1', completer: nsv._completer }
);